public static StringBuilder reader(String filePath) {
try {
File file = new File(filePath);
if (file.isFile() && file.exists()) {
FileInputStream fileInputStream=new FileInputStream(file);
byte[] b = new byte[fileInputStream.available()];
StringBuilder sb = new StringBuilder();
int len = -1;
while( -1 != (len =fileInputStream.read(b)))
{
byte[] temp = null;
temp = Arrays.copyOf(b, len); // 针对最后一次数据读入,防止从流中读入的数据中包含空格。
sb.append(new String(temp,"GBK"));
}
return sb;
}
} catch (UnsupportedEncodingException | FileNotFoundException e) {
System.out.println("Cannot find the file specified!");
e.printStackTrace();
} catch (IOException e) {
System.out.println("Error reading file content!");
e.printStackTrace();
}
return null;
}
byte[] b = new byte[fileInputStream.available()];
sb.append(new String(temp,“GBK”));
防止乱码