小弟需要这样的一个函数,在一个目录下 如D:\NEW 
这样一个目录下有很多txt文件  如1.txt,2.txt.........
我需要按照文件名称的序列将这些文件里面的内容全部 写入一个新的txt文件  如new.txt
请问怎么实现,需要能直接编译通过的代码 谢谢,急在线等

解决方案 »

  1.   


    import java.io.*;
    import java.util.regex.*;public class ReadFile {
    public static String read(String filename) throws IOException {
        // Reading input by lines:
        BufferedReader in = new BufferedReader(
          new FileReader(filename));
        String s;
        StringBuilder sb = new StringBuilder();
        while((s = in.readLine())!= null)
          sb.append(s + "\n");
        in.close();
        return sb.toString();
      }
    public static void main(String[] args) throws IOException {
    File file = new File("D:\\NEW");
    String[] list ;
    list = file.list(new FilenameFilter() {
            private Pattern pattern = Pattern.compile(".*\\.txt");//只匹配.txt的文件
            public boolean accept(File dir, String name) {
              return pattern.matcher(name).matches();
            }
          });
          
         FileWriter fos = new FileWriter("new.txt",true); //假设new.txt在当前目录下
        
        
          for(String s : list) 
           fos.write(read(s));
          
          fos.close();
              



    }
    }
      

  2.   

    1楼,可否想过如果某个文件过大,会直接让你的jvm当掉?
      

  3.   

    for(String s : list) 
              fos.write(read(s));->
    for(String s : list) {
      String temp = null;
      BufferedReader in = new BufferedReader(new FileReader(s));
      while((temp = in.readLine())!= null)
          fos.write(temp + "\n");
      in.close();
    }thenfos.flush();
      

  4.   

    MS 我的JDK版本 不支持
    StringBuilder 和String s : list这样的句子。。
      

  5.   


    import java.io.*;public class TestTimer
    {
    public void write(File oldFile, File newFile) throws IOException
    {
    if(oldFile==null || !oldFile.exists()){
    System.out.println("读取文件不存在!");
    return;
    }
    if(newFile==null){
    System.out.println("写入文件为null");
    }
    if(!newFile.exists()){
    System.out.println("写入文件不存在,创建文件!");
    newFile.createNewFile();
    }
    String nextLine="\r\n";
    FileReader in=new FileReader(oldFile);
    BufferedReader read=new BufferedReader(in);
    FileWriter out=new FileWriter(newFile,true);
    out.write(nextLine);
    out.write(oldFile.getPath());
    out.write(":");
    out.write(nextLine);
    String line=read.readLine();
    while(line!=null){
    out.write(line);
    out.write(nextLine);
    line=read.readLine();
    }
    read.close();
    in.close();
    out.close();
    }
     
    public void writeDir(File dir,File writeFile) throws IOException{
    if(dir==null || !dir.exists()){
    System.out.println("目录不存在!");
    return;
    }
    if(!dir.isDirectory()){
    System.out.println(dir.getPath()+dir.getName()+"不是一个目录文件!");
    return;
    }
    File[] files=dir.listFiles();
    for(int i=0;i<files.length;i++){
    File oldFile=files[i];
    String name=oldFile.getName();
    if(oldFile.isDirectory()){
    this.writeDir(oldFile, writeFile);
    }else if(name.endsWith(".txt")){
    this.write(oldFile, writeFile);
    }
    }
    }

    public static void main(String args[])
    {
    TestTimer test = new TestTimer();
    try
    {
    test.writeDir(new File("d:/txt"), new File("d:/new.txt"));
    }
    catch (IOException e)
    {
    e.printStackTrace();
    }
    }
    }