读取文件内容demo

我会带着你远行 2022-10-17 13:48 327阅读 0赞

读取文件内容代码:fileName为文件夹路径

  1. //读取json文件
  2. public static String readJsonFile(String fileName) {
  3. String jsonStr = "";
  4. try {
  5. File jsonFile = new File(fileName);
  6. FileReader fileReader = new FileReader(jsonFile);
  7. Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
  8. int ch = 0;
  9. StringBuffer sb = new StringBuffer();
  10. while ((ch = reader.read()) != -1) {
  11. sb.append((char) ch);
  12. }
  13. fileReader.close();
  14. reader.close();
  15. jsonStr = sb.toString();
  16. return jsonStr;
  17. } catch (IOException e) {
  18. e.printStackTrace();
  19. return null;
  20. }
  21. }

发表评论

表情:
评论列表 (有 0 条评论,327人围观)

还没有评论,来说两句吧...

相关阅读