/**

  • 读取指定目录下的文件
  • @param path 文件的路径
  • @return 文件内容
  • @throws IOException
    */

public static String readFile(File path) throws IOException{
//创建一个输入流对象

// String str ;
// StringBuilder stringBuilder = new StringBuilder();
// BufferedReader bufferedReader= new BufferedReader(
// new InputStreamReader(
// new FileInputStream(path),"UTF-8"));
//
// while ((str=bufferedReader.readLine())!=null){
// stringBuilder.append( "n");
// stringBuilder.append(str);
// }
InputStream is=new FileInputStream(path);
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int n ;
while ((n = is.read(buffer)) != -1) {
out.write(buffer, 0, n);
}
//释放资源
is.close();
return out.toString();
}

参考

https://blog.csdn.net/u010389391/article/details/95508722
https://blog.csdn.net/cslucifer/article/details/76595449

最后修改:2021 年 01 月 11 日 10 : 54 AM