/** * @param filePath 文件的全路径 * 返回我们读取到的文件内容 * **/ public static String readFile(String filePath) { FileInputStream fis = null; StringBuffer sb = new StringBuffer(); // 保存读取的数据 try { File file = new File(filePath); // 首先得到文件的位置 fis = new FileInputStream(file); // 将文件加载到内存中 InputStreamReader isr = new InputStreamReader(fis, "big5"); BufferedReader reader = new BufferedReader(isr); String line; while ((line = reader.readLine()) != null) { sb.append(line); //一次读取一行的数据 sb.append(" "); //在一行的末尾添加标记方便分隔,或进行其他的处理操作 } } catch (Exception e) { e.printStackTrace(); } finally { try { if (fis != null) { fis.close(); } } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); }
对于文件的读取操作网上的资料一大堆,自己也是最近用到这个东西,翻着查了一遍,并不算太难,挺简单的,今天有空记录下来方便以后的查询