如何用java语言将数组中的数据写入文件??急!求好心人帮忙!!

如题所述

把数据按照你需要的格式拼接成字符串,然后写入文件,下面的方法是写入的同时同时创建文件:
filepath:文件路径,file:写入的内容,encode:编码格式。
public static boolean createFile(String filepath, String file, String encode)
{
filepath = filepath.replace("\r", "");
filepath = filepath.replace("\n", "");
try
{
File f1 = new File(filepath);
if(!f1.exists())
f1.createNewFile();
FileOutputStream fos = new FileOutputStream(filepath);
Writer pw = new OutputStreamWriter(fos, encode);
pw.write(file);
pw.close();
}
catch(IOException e)
{
e.printStackTrace();
return false;
}
return true;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2018-04-06
用FileWrite写方便

给你个例子

String []arr = {"a","b","c"};
FileWrite fw = new FileWrite("d:/a.txt");
String str = "";
for(int i=0;i<arr.length;i++){
str += arr[i] + ",";
}
fw.write(str);
fw.flush();
fw.close();

写入文件的格式(分隔符,或换行等,可以在循环里,拼接str的时候加上去,我这里实现的是用逗号分隔)本回答被网友采纳
相似回答