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


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

    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();
                    
        }
    }
  • 相关阅读:
    赫夫曼树编码
    根据先序和中序实现后序
    C++语言实现开心消消乐
    C语言风格实现的开心消消乐
    动态规划
    leetcode dp wordbreakII
    欧拉回路
    欧拉通路是否存在
    Python|多任务:线程、进程、协程--你想要的都在这里
    网络通信:socket、udp与tcp
  • 原文地址:https://www.cnblogs.com/yuwenfeng/p/3258859.html
Copyright © 2020-2023  润新知