我正在写文件上的数据,我正在使用ArrayList
,但我的主要问题是如何在文本文件中添加新行。是否可以这样做。
这样一种方式,当ArrayList
的第一个数据写入文本文件而不是在新行中自动写入ArrayList
的下一个数据时
final String FILES = "/MY_FILE_FOLDER";
String path= Environment.getExternalStorageDirectory().getPath()+FILES; // Folder path
File folderFile = new File(path);
if (!folderFile.exists()) {
folderFile.mkdirs();
}
File myFile = new File(folderFile, fileName+".doc");
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
for (int i = 0; i < getdata.size(); i++) {
myOutWriter.append(getdata.get(i));
}
myOutWriter.close();
fOut.close();
Toast.makeText(getBaseContext(),
"Done writing SD 'mysdfile.txt'",
Toast.LENGTH_SHORT).show();
答案 0 :(得分:9)
更改,
for (int i = 0; i < getdata.size(); i++) {
myOutWriter.append(getdata.get(i));
}
到
for (int i = 0; i < getdata.size(); i++) {
myOutWriter.append(getdata.get(i));
myOutWriter.append("\n\r");
}
我希望这会有所帮助!!
答案 1 :(得分:6)
尝试这种方式,
FileOutputStream fOut = new FileOutputStream(myFile);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fOut));
for (int i = 0; i < getdata.size(); i++) {
bw.write(getdata.get(i));
bw.newLine();
}
bw.close();
fOut.close();
答案 2 :(得分:2)
for (int i = 0; i < getdata.size(); i++) {
myOutWriter.append(getdata.get(i));
myOutWriter.append("\n\r");
}
答案 3 :(得分:0)
以下代码肯定适用于任何人..
FileOutputStream writer = new FileOutputStream(gpxfile);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(writer));
String str ="Apple,Fruit : Cabbage,Vegetable";
StringTokenizer strr = new StringTokenizer(str, ":");
while (strr.hasMoreTokens()) {
bw.write(strr.nextToken()+"\r\n");
}
bw.flush();
writer.flush();
bw.close();
writer.close();