采取以下静态方法:
public static String fileToString(String filename) throws Exception {
FileInputStream fis = new FileInputStream(filename);
byte[] buffer = new byte[8192];
StringBuffer sb = new StringBuffer();
int bytesRead; // unused? weird compiler messages...
while((bytesRead = fis.read(buffer)) != -1) { // InputStream.read() returns -1 at EOF
sb.append(new String(buffer));
}
return new String(sb);
}
如您所见,一切看起来都不错,非常适合小型文本文件。但是,一旦您处理包含数千行的大文件,就会遇到重复文本的问题。根据我的直觉,我认为byte[] buffer
是“不洁”,可以这么说。所以我在方法中添加了以下行:
buffer = new byte[8192];
所以现在是:
public static String fileToString(String filename) throws Exception {
FileInputStream fis = new FileInputStream(filename);
byte[] buffer = new byte[8192];
StringBuffer sb = new StringBuffer();
int bytesRead; // unused? weird compiler messages...
while((bytesRead = fis.read(buffer)) != -1) { // InputStream.read() returns -1 at EOF
sb.append(new String(buffer));
buffer = new byte[8192]; // added new line here
}
return new String(sb);
}
它是完美的,除了在静态方法返回的字符串末尾,我得到很多空字符(取决于缓冲区大小)。这里发生了什么?