zoukankan      html  css  js  c++  java
  • java读取文件夹下文件及txt内容

    public class PositionController {
        // 读取txt内容
        public static String txt2String(File file) {
            StringBuilder result = new StringBuilder();
            try {
                BufferedReader br = new BufferedReader(new FileReader(file));// 构造一个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();
        }

        // 读取文件夹下所有文件名
        public static List getFile(File file) {
            List listLocal = new ArrayList<>();
            if (file != null) {
                File[] f = file.listFiles();
                if (f != null) {
                    for (int i = 0; i < f.length; i++) {
                        getFile(f[i]);
                        listLocal.add(f[i]);
                    }
                } else {
                    // System.out.println(file);
                }
            }
            return listLocal;
        }

        public String getTxt(ModelMap map) throws FileNotFoundException {
                // 文件夹下每一个txt名
                String path = "D:/wode/TestReallyData/txt2/txt_all/";
                File all = new File(path);
                //将全部txt存到list中
                List allPath = getFile(all);
                 // 读取txt内容 并转换成List
                 for(int i = 0 ;i <allPath.size();i++){
                    File file = new File(i);
                    String content = txt2String(file);
                 }
          }
    }
  • 相关阅读:
    51 nod 1181 质数中的质数(质数筛法)
    Just oj 2018 C语言程序设计竞赛(高级组)F:Star(结构体排序+最小生成树)
    欧拉函数+费马小定理拓展
    ZOJ 3785 What day is that day?(数论:费马小定理)
    Just oj 2018 C语言程序设计竞赛(高级组)H: CBT?
    树链剖分(入门学习)
    bitset用法
    链式前向星
    Nearest Common Ancestors(LCA板子)
    LCA(最近公共祖先)
  • 原文地址:https://www.cnblogs.com/gemeiyi/p/10845312.html
Copyright © 2011-2022 走看看