• 读取文件夹下面的文件(文件夹套文件夹格式)


    读取一个文件路径下的所有文件

    package Segdictionary;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.util.ArrayList;
    
    /**
     * 构造删除的词文件
     * @author ywf
     *
     */
    public class createDeleteWords {
    
        public static String path = "e:\Users\ywf\Desktop\删除的词";
        public static String outpath = "e:\Users\ywf\Desktop\删除.txt";
        public static void main(String[] args) throws Exception{
            File files = new File(path);
            FileWriter writer = new FileWriter(outpath, true);
            ArrayList<File> dirs = new ArrayList();//存储文件夹
            ArrayList <File> fileAll = new ArrayList<File>();//存储文件
            dirs.add(files);
            int index = 0;
            while(index<dirs.size()){
                File file1 = dirs.get(index);
                if(file1.isDirectory()){
                    File[] fileList = file1.listFiles();
                    for(int i= 0 ;i<fileList.length;i++){
                        File ff = fileList[i];
                        if(ff.isFile()){
                            fileAll.add(ff);
                        }else{
                            dirs.add(ff);
                        }
                    }
                }else{
                    fileAll.add(file1);
                }
                index++;
            }
            for(File fff :fileAll){
                InputStream in = new FileInputStream(fff);
                InputStreamReader inReader= new InputStreamReader(in);
                BufferedReader buf = new BufferedReader(inReader);                        
                String content = buf.readLine();
    //            content = new String(content.getBytes("gbk"),"utf-8");
                System.out.println(content);
                while(content!=null){
                    writer.write(content);
                    writer.write("
    ");
                    content = buf.readLine();
    //                content = new String(content.getBytes("gbk"),"utf-8");
                }
            }
            writer.close();
                    
        }
    }
  • 相关阅读:
    could not read data from '/Users/lelight/Desktop/ViewControllerLife/ViewControllerLife/Info.plist': The file “Info.plist” couldn’t be opened because there is no such file.
    NSNotification 消息通知的3种方式
    按钮点击播放音效
    字符串变枚举变量
    Flutter的使用教学笔记
    UI控件的位置
    博客园大佬主页跳转
    retain, copy, assign区别
    OC自定义文档头部注释
    OC语言自定义打印
  • 原文地址:https://www.cnblogs.com/yuwenfeng/p/3258859.html
Copyright © 2020-2023  润新知