读取一个文件路径下的所有文件
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(); } }