当前位置:   article > 正文

Java如何读取文件文本内容的几种方式汇总_java读取本地文件内容

java读取本地文件内容

本文为joshua317原创文章,转载请注明:转载自joshua317博客 Java如何读取文件文本内容的几种方式汇总 - joshua317的博客

  1. package com.joshua317;
  2. import java.io.*;
  3. import java.nio.charset.StandardCharsets;
  4. import java.nio.file.Files;
  5. import java.nio.file.Path;
  6. import java.nio.file.Paths;
  7. import java.util.Iterator;
  8. import java.util.List;
  9. import java.util.Scanner;
  10. import java.util.stream.Stream;
  11. public class FileDemo {
  12. static String filePath = "E:\\1.txt";
  13. public static void main(String[] args) throws IOException {
  14. readFile1();
  15. // readFile1_1();
  16. // readFile2();
  17. // readFile3();
  18. // readFile4();
  19. // readFile5();
  20. // readFile6();
  21. // readFile6_1();
  22. // readFile7();
  23. // readFile7_1();
  24. }
  25. /**
  26. * 字节流>字符流>字符串
  27. * 使用File,FileInputStream,InputStreamReader,BufferedReader
  28. * 使用FileInputStream,InputStreamReader,BufferedReader
  29. * @throws IOException
  30. */
  31. public static void readFile1() throws IOException {
  32. // 使用File
  33. // File file = new File(filePath);
  34. // FileInputStream fileInputStream = new FileInputStream(file);
  35. // FileInputStream(字节流) 实现了InputStream接口,用来读取文件中的字节流,参数是文件或者文件路径+文件名称
  36. FileInputStream fileInputStream = new FileInputStream(filePath);
  37. // 将 fileInputStream(字节流) 流作为参数,转为InputStreamReader(字符流)
  38. InputStreamReader reader = new InputStreamReader(fileInputStream);
  39. // 将 字符流(参数)转为字符串流,带缓冲的流读取,默认缓冲区8k
  40. BufferedReader bufferedReader = new BufferedReader(reader);
  41. String line;
  42. while ((line = bufferedReader.readLine()) != null) {
  43. System.out.println(line);
  44. }
  45. fileInputStream.close();
  46. bufferedReader.close();
  47. }
  48. public static void readFile1_1() throws IOException {
  49. // 使用File
  50. // File file = new File(filePath);
  51. // FileInputStream fileInputStream = new FileInputStream(file);
  52. // FileInputStream(字节流) 实现了InputStream接口,用来读取文件中的字节流,参数是文件或者文件路径+文件名称
  53. FileInputStream fileInputStream = new FileInputStream(filePath);
  54. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  55. int i;
  56. while ((i = fileInputStream.read()) != -1) {
  57. byteArrayOutputStream.write(i);
  58. }
  59. System.out.println(byteArrayOutputStream.toString());
  60. fileInputStream.close();
  61. byteArrayOutputStream.close();
  62. }
  63. /**
  64. * 使用File,FileReader,BufferedReader
  65. * 使用FileReader,BufferedReader
  66. * @throws IOException
  67. */
  68. public static void readFile2() throws IOException {
  69. // 使用File
  70. // File file = new File(filePath);
  71. // FileReader fileReader = new FileReader(file);
  72. FileReader fileReader = new FileReader(filePath);
  73. BufferedReader bufferedReader = new BufferedReader(fileReader);
  74. String line;
  75. while ((line = bufferedReader.readLine()) != null) {
  76. System.out.println(line);
  77. }
  78. fileReader.close();
  79. bufferedReader.close();
  80. }
  81. /**
  82. * 使用FileReader
  83. * @throws IOException
  84. */
  85. public static void readFile3() throws IOException {
  86. // 使用File
  87. // File file = new File(filePath);
  88. // FileReader fileReader = new FileReader(file);
  89. FileReader fileReader = new FileReader(filePath);
  90. int i;
  91. while ((i = fileReader.read()) != -1) {
  92. System.out.print((char)i);
  93. }
  94. fileReader.close();
  95. }
  96. /**
  97. * 使用Scanner
  98. * @throws IOException
  99. */
  100. public static void readFile4() throws IOException {
  101. File file = new File(filePath);
  102. Scanner sc = new Scanner(file);
  103. while (sc.hasNextLine()) {
  104. System.out.println(sc.nextLine());
  105. }
  106. sc.close();
  107. }
  108. /**
  109. * 读取整个文件
  110. * 使用Files.readAllBytes,Paths
  111. * @throws IOException
  112. */
  113. public static void readFile5() throws IOException {
  114. Path path = Paths.get(filePath);
  115. byte[] bytes = Files.readAllBytes(path);
  116. String data = new String(bytes);
  117. System.out.println(data);
  118. }
  119. /**
  120. * 读取整个文件
  121. * 使用Files.readAllLines,Paths,迭代器
  122. * @throws IOException
  123. */
  124. public static void readFile6() throws IOException {
  125. Path path = Paths.get(filePath);
  126. List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
  127. Iterator<String> itr = lines.iterator();
  128. while (itr.hasNext()) {
  129. System.out.println(itr.next());
  130. }
  131. }
  132. /**
  133. * 使用Files.lines
  134. * @throws IOException
  135. */
  136. public static void readFile6_1() throws IOException {
  137. Path path = Paths.get(filePath);
  138. Stream<String> lines = Files.lines(path);
  139. lines.forEach(e -> {
  140. System.out.println(e);
  141. });
  142. }
  143. /**
  144. * 使用Files.newBufferedReader
  145. * @throws IOException
  146. */
  147. public static void readFile7() throws IOException {
  148. Path path = Paths.get(filePath);
  149. BufferedReader bufferedReader= Files.newBufferedReader(path);
  150. String str;
  151. while ((str = bufferedReader.readLine())!=null){
  152. System.out.println(str);
  153. }
  154. bufferedReader.close();
  155. }
  156. public static void readFile7_1() throws IOException {
  157. Path path = Paths.get(filePath);
  158. BufferedReader bufferedReader= Files.newBufferedReader(path);
  159. String str;
  160. StringBuilder stringBuilder = new StringBuilder();
  161. while ((str = bufferedReader.readLine())!=null){
  162. stringBuilder.append(str + "\n");
  163. }
  164. System.out.println(stringBuilder.toString());
  165. bufferedReader.close();
  166. }
  167. }

本文为joshua317原创文章,转载请注明:转载自joshua317博客 Java如何读取文件文本内容的几种方式汇总 - joshua317的博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/663927
推荐阅读
相关标签
  

闽ICP备14008679号