使用指定编码读取文件
public static String toString(File file, String charset){
StringBuilder result = new StringBuilder();
BufferedReader br = null;
try{
br=new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));//构造一个BufferedReader类来读取文件
String s = null;
while((s = br.readLine())!=null){//使用readLine方法,一次读一行
result.append(System.lineSeparator()+s);
}
}catch(Exception e){
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result.toString();
}