2017-08-17 62 views
1

我试图合并一个特定文件夹中的所有.txt文件并创建一个output.txt文件。 我是新来的Java学习Java.IO包。 这里是我的程序,编译得很好,并创建一个输出文件,但不写任何东西。 我验证了我的输入文本文件并且它有数据。使用Java.IO合并文件夹中的所有.txt文件

import java.io.*; 

class Filemerger 
{ 
    public static void main(String[] args) throws IOException 
    { 
     PrintWriter pw = new PrintWriter("true1.txt");  
     File f = new File("E:\\A"); 
     String[] s = f.list(); 
     for (String s1 : s) 
     { 
      File f1 = new File(f, s1); 
      BufferedReader br = new BufferedReader(new FileReader(f1)); 
      String line = br.readLine(); 
      while (line != null) 
      { 
       pw.println(line); 
       line = br.readLine(); 
      } 
     } 
     pw.flush(); 
     pw.close(); 
    } 
} 
+0

我不会推荐阅读和写作的同时,我想以后我读完成了原始文件的写入。 –

+0

您应该使用名为'NIO'的新文件API,它更健壮,并且提供更多功能。核心类有'Paths'和'Files'。 – Zabuza

+0

你能确认你的中间文件指向了正确的路径吗?我的意思是这个'文件f1 =新文件(f,s1);'。也许路径是错误的,打印出来并看看它。 – Zabuza

回答

1

我认为你的错误是最小的,也许有些路径是错误的或类似的东西。无论如何,您应该使用名为NIO的新IO API与文件进行交互。关键类别为PathsFiles,位于包装java.nio

class Filemerger 
{ 
    public static void main(String[] args) throws IOException 
    { 
     Path output = Paths.get("true1.txt"); 
     Path directory = Paths.get("E:\\A"); 
     Stream<Path> filesToProcess = Files.list(directory); 

     // Iterate all files 
     filesToProcess.forEach(path -> { 
      // Get all lines of that file 
      Stream<String> lines = Files.lines(path); 
      // Iterate all lines 
      lines.forEach(line -> { 
       // Append the line separator 
       String lineToWrite = line + System.lineSeparator(); 

       // Write every line to the output file 
       Files.write(output, lineToWrite.getBytes(StandardCharsets.UTF_8)); 
      }); 
     }); 
    } 
} 

或者你也可以使用Stream#flatMap方法,如果你只是想收集所有文件中的所有行没有映射到它们属于哪个文件:

Path output = Paths.get("true1.txt"); 
Path directory = Paths.get("E:\\A"); 

Files.list(directory) 
    .flatMap(Files::lines) 
    .forEach(line -> { 
     Files.write(output, (line + System.lineSeparator()) 
      .getBytes(StandardCharsets.UTF_8)); 
    }); 

注意Files#write和其他方法也接受一堆你可以设置的选项。例如,如果文件如果还不存在,应该创建它。或者,如果它已经存在,则应该添加内容还是应该删除旧内容。

因此看看文档网页:

0

你的代码没有任何内容写入文件。也许println()方法。请根据您的要求调整输入和输出文件的路径。

经过稍微修改:

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 
import java.io.IOException; 
import java.io.PrintWriter; 
import java.util.Arrays; 

public class MergeTextFiles { 
    public static void main(String[] args) throws IOException { 
     File f = new File("./src/main/resources"); 

     File merged = new File("./src/main/resources/merged.txt"); 
     if (!merged.exists()) { 
      merged.createNewFile(); 
     } 

     PrintWriter pw = new PrintWriter(merged); 

     String[] s = f.list(); 
     System.out.println("list of files-> " + Arrays.asList(s)); 

     for (String s1 : s) { 
      File f1 = new File(f, s1); 
      BufferedReader br = new BufferedReader(new FileReader(f1)); 

      String line = ""; 
      while ((line = br.readLine()) != null) { 
       System.out.println(line); 
       pw.println(line); 
      } 
     } 
     pw.flush(); 
     pw.close(); 
    } 
} 

更新:这不是 '调用println'。

+0

虽然你应该去@ Zabuza的解决方案,更优雅的方式来做IO操作。 –

0

您应该始终处理main方法中的异常,否则将忽略异常。如果目录不存在,f.list()返回null(!);如果目录不存在,则返回null(!);如果目录不存在,

如果目录包含子目录,那些子目录的名称也将被返回,所以你必须测试一个文件确实是一个文件;

当你打开一个文件,你应该将其关闭或(更好)使用与资源try块:

public static void main(String[] args) 
{ 
    try(PrintWriter pw = new PrintWriter("true1.txt")) { 
     File f = new File("E:\\A"); 
     String[] s = f.list(); 
     if (s == null) { 
      throw new IOException("Directory doesn't exist: " + f); 
     } 
     for (String s1 : s) 
     { 
      File f1 = new File(f, s1); 
      if (!f1.isFile()) { 
       continue; 
      } 
      try (Reader reader = new FileReader(f1); 
        BufferedReader br = new BufferedReader(reader)) { 
       String line = br.readLine(); 
       while (line != null) 
       { 
        pw.println(line); 
        line = br.readLine(); 
       } 
      } 
     } 
    } catch (Throwable e) { 
     Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 
相关问题