【发布时间】:2014-01-13 02:59:47
【问题描述】:
String mystring="Hello"+"\n"+ "World" ;
writeToFile(mystring);
String newstring = readFromFile();
mytextview.setText(newstring);
我的文本视图只显示没有换行符的“HelloWorld”
我不明白为什么它不能识别“\n”
这些是我的 writetofile 和 readfromfile 函数;
private void writeToFile(String data) {
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("myfilename", Context.MODE_PRIVATE));
outputStreamWriter.write(data);
outputStreamWriter.close();
}
catch (IOException e) {
// Log.e(TAG, "File write failed: " + e.toString());
}
}
//////////////////////////////////////////////////
private String readFromFile() {
String ret = "";
try {
InputStream inputStream = openFileInput("myfilename");
if ( inputStream != null ) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ( (receiveString = bufferedReader.readLine()) != null ) {
stringBuilder.append(receiveString);
}
inputStream.close();
ret = stringBuilder.toString();
}
}
catch (FileNotFoundException e) {
//Log.e(TAG, "File not found: " + e.toString());
} catch (IOException e) {
// Log.e(TAG, "Can not read file: " + e.toString());
}
return ret;
}
我要做的是将一个字符串保存到手机的内部存储中并读回相同的字符串。
【问题讨论】:
-
试试
System.getProperty("line.separator")而不是\n -
根据他与编辑一起阅读的内容可能不代表“\n” ...
-
您的问题是,“如果我连接文件的所有行,为什么我的文件的所有行都连接?”
标签: java android string file command-line