The method of reading text file by Java IO stream

From , 4 Years ago, written in Java, viewed 52 times.
URL https://pastebin.vip/view/a67f0968
  1. /**
  2.          * 读取指定路径的文本
  3.          *
  4.          * @param path
  5.          *            文本路径
  6.          */
  7.         public static void readFileByPath(String path) {
  8.                 if (null == path) {
  9.                         return;
  10.                 }
  11.                 File file = new File(path);
  12.                 if (file.exists() && file.isFile()) {
  13.                         InputStreamReader reader = null;
  14.                         BufferedReader br = null;
  15.                         try {
  16.                                 reader = new InputStreamReader(new FileInputStream(file), "gbk");
  17.                                 br = new BufferedReader(reader);
  18.                                 String lineText = null;
  19.                                 while ((lineText = br.readLine()) != null) {
  20.                                         System.out.println(lineText);
  21.                                 }
  22.                         } catch (Exception e) {
  23.                                 e.printStackTrace();
  24.                                 System.out.println("文件读取错误");
  25.                         } finally {
  26.                                 // 关闭流
  27.                                 if (null != br) {
  28.                                         try {
  29.                                                 br.close();
  30.                                         } catch (IOException e) {
  31.                                                 e.printStackTrace();
  32.                                         }
  33.                                 }
  34.                                 if (null != reader) {
  35.                                         try {
  36.                                                 reader.close();
  37.                                         } catch (IOException e) {
  38.                                                 e.printStackTrace();
  39.                                         }
  40.                                 }
  41.                         }
  42.                 } else {
  43.                         System.out.println("文件错误!");
  44.                 }
  45.         }

Reply to "The method of reading text file by Java IO stream"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website