我正尝试在android中向文本文件写入新行。Android不会在文本文件中写入新行
这里是我的代码:
FileOutputStream fOut;
try {
String newline = "\r\n";
fOut = openFileOutput("cache.txt", MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.write(data);
osw.write(newline);
osw.flush();
osw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
我已经试过\n
,\r\n
我也还试图让系统性能的一个新行,他们都没有工作。
数据变量包含之前来自同一文件的数据。
String data = "";
try {
FileInputStream in = openFileInput("cache.txt");
StringBuffer inLine = new StringBuffer();
InputStreamReader isr = new InputStreamReader(in, "ISO8859-1");
BufferedReader inRd = new BufferedReader(isr,8 * 1024);
String text;
while ((text = inRd.readLine()) != null) {
inLine.append(text);
}
in.close();
data = inLine.toString();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
你是什么意思“不工作”?你如何检查它?还可以尝试用'foo'等简单的东西替换'data'并检查变量内容 – 2012-02-16 13:46:17
当我在textedit,word或其他程序中打开它时,新行不会显示 – NikolajSvendsen 2012-02-16 13:47:57
尝试使用虚拟文本,它不会显示w扫。 – NikolajSvendsen 2012-02-16 13:59:20