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);
                 }
          }
    }
  • 相关阅读:
    【转】NSArray,NSSet,NSDictionary总结
    dequeueReusableCellWithIdentifier
    可任意自定义的UITableViewCell
    contentSize、contentInset和contentOffset区别
    Cocoa的MVC架构分析 delegate
    WP7 Toolkit ExpanderView 控件 介绍 02
    ObjectiveC中一种消息处理方法performSelector: withObject:
    [转]HTML5多点触摸演示源码(Canvas绘制演示)
    Matlab 积分图的快速计算
    测试
  • 原文地址:https://www.cnblogs.com/gemeiyi/p/10845312.html
Copyright © 2011-2022 走看看