去掉换行符的读取方法:
public static String readFileWithoutN(File path,String filename)
{
StringBuffer sb = null;
try {
File file = new File(path,filename);
BufferedReader br = new BufferedReader(new FileReader(file));
String readline = "";
sb = new StringBuffer();
while ((readline = br.readLine()) != null) {
System.out.println("readline:" + readline);
sb.append(readline);
}
br.close();
Log.i(TAG, sb.toString());
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
原样读取的方法:
public static String readFile(File path,String filename) {
String result = null;
try {
File file = new File(path, filename);
if (!file.exists()) {
return null;
}
FileInputStream inputStream = new FileInputStream(file);
byte[] b = new byte[inputStream.available()];
inputStream.read(b);
result = new String(b);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
高效文件读取:去除换行与原样读取的Java实现对比
本文档介绍了两种Java方法,一种是使用`BufferedReader`逐行读取文件并去掉换行符,另一种是直接读取文件内容到字符串。通过比较,展示了两种方法在实际操作中的应用场景和优劣。
3769

被折叠的 条评论
为什么被折叠?



