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);
                 }
          }
    }
  • 相关阅读:
    php RSA公钥私钥加解密和验证用法
    php格式化RSA公钥私钥字符串
    你的周末时光是什么样的?
    php-redis 消息订阅笔记
    php redis 常用操作
    MySql索引笔记
    yii1框架,事务使用方法
    python 项目打包成exe可执行程序
    Linux修改默认端口
    C++字符串作为参数的传递
  • 原文地址:https://www.cnblogs.com/gemeiyi/p/10845312.html
Copyright © 2011-2022 走看看