我正嘗試在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