直接上代码:
如果出现乱码:请改一下编码:我这里使用utf-8是会乱码的,改GBK就好了
// 读取文件内容 public static String readFile(String path) {//路径 File file = new File(path); StringBuilder result = new StringBuilder(); try { BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "GBK"));//构造一个BufferedReader类来读取文件 String s = null; while ((s = br.readLine()) != null) {//使用readLine方法,一次读一行 result.append(System.lineSeparator() + s); } br.close(); } catch (Exception e) { e.printStackTrace(); } return result.toString(); }