java中读取某个文件中的数据内容

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

import java.util.HashSet;

import java.util.Set;

   public class ReadText {

  //读取文件中的所有内容

     public static void main(String[] args) {

      String path = "D:\\新建文件夹\\1.txt";

      String s = toArrayByFileReader1(path);

      System.out.println(s); }

     public static String toArrayByFileReader1(String name) {

    // 使用ArrayList来存储每行读取到的字符串

    StringBuilder s = new StringBuilder();

    String txt = "";

    try {

      FileReader fr = new FileReader(name);

      BufferedReader bf = new BufferedReader(fr);

      String str;

      // 按行读取字符串

      while ((str = bf.readLine()) != null) {

          s.append(str); }

      txt = s.toString();

      } catch (IOException e) { e.printStackTrace(); }

      return txt;

      }

}

posted on 2021-01-14 18:01  小泥爪子  阅读(531)  评论(0)    收藏  举报

导航