直接上代码:
/** * 读取指定目录下的文件 * @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(); }
微信扫码查看本文
发表评论