我有一个简单的应用程序,它读取一个 txt 文件并将其放入 EditText View 中。 一切都很好,除了我无法从 txt 文件中检测到换行符并将其替换为“\n”。
for example : I have this text in my file
this is a file with newline but this is what I get in the Edittext view: thisisafilewithnewline.
这是我的代码:
private String onFileClick(Option o){
StringBuilder text = new StringBuilder();//--read text from file---
String filePath = o.getPath();
String CHARSET = "ISO-8859-1";
String fileType = MimeTypeMap.getFileExtensionFromUrl(filePath);
String NOT_TXT = "NOT TXT FILE";
try{
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), CHARSET));
String line;
while((line = br.readLine()) != null){
text.append(line.replaceAll("\r\n", "\n"));
}
br.close();
}catch (FileNotFoundException e) {
Log.e("login activity", "File not found: " + e.toString());
} catch (IOException e) {
Log.e("login activity", "Can not read file: " + e.toString());
}
if(fileType.equals("txt"))
return text.toString();
else
return NOT_TXT;
}
我需要帮助
最佳答案
text.append(line+"\n");// this will add new line into your string after reading
这就是你想要做的吗?
关于java - 读取文本文件时检测新行 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22836299/