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);
                 }
          }
    }
  • 相关阅读:
    docker入门——centos安装
    NET应用——你的数据安全有必要升级
    mysql事件机制——定时任务
    是时候升级你的Js工具了-分页【基于JQ】
    优美库图片系统
    爬虫之蜂鸟网图片爬取
    壁纸提取
    CSDN刷阅读数
    tkinter基础-输入框、文本框
    数据结构与算法之选择排序
  • 原文地址:https://www.cnblogs.com/gemeiyi/p/10845312.html
Copyright © 2011-2022 走看看