2012-04-07 134 views
1

我必须创建一个程序,从两个已按字母顺序排列的文件中读取名称和ID号,并将它们按第三个文件按字母顺序组合。姓氏,名字 ID(前。rbb091020) 姓氏,名字 ID为什么我的程序不能将信息写入文件?

出于某种原因,我的文件中读取两个文件,并创建一个第三,但它不写什么到第三个文件。

这里是我的代码...

import java.io.*; 
import java.util.Scanner; 

public class Combine_Files 
{ 
public static void main(String[]args) throws IOException 
{ 
    String filename1 = ""; 
    String filename2 = ""; 
    String combinedFileName = ""; 
    String response = ""; 
    String nextName1 = ""; 
    String nextName2 = ""; 
    String nextIDNumber1 = ""; 
    String nextIDNumber2 = ""; 
    boolean okToOverwrite = false; 
    Scanner keyboard = new Scanner(System.in); 

    System.out.println("What is the name of the first file?"); 
    filename1 = keyboard.nextLine(); 

    File file1 = new File(filename1); 

    if (!file1.exists()) 
    { 
     System.out.println(file1 + " does not exist.\nTerminating Program."); 
     System.exit(0); 
    } 

    System.out.println("What is the name of the second file?"); 
    filename2 = keyboard.nextLine(); 

    File file2 = new File (filename2); 
    if (!file2.exists()) 
    { 
     System.out.println(file2 + " does not exist.\nTerminating Program."); 
     System.exit(0); 
    } 

    System.out.println("What is the name of the file that you wish to create?"); 
    combinedFileName = keyboard.nextLine(); 
    File combinedFile = new File (combinedFileName); 

    while (combinedFile.exists() && !okToOverwrite) 
    { 
     System.out.println("\nError, a file named " + 
      combinedFileName + " already exists." + 
      "\nWould you like to overwrite this file?" + 
      "\nEnter Y for Yes or N for No."); 
     response = keyboard.nextLine(); 

     // Add input validation on response 

     if (response.equalsIgnoreCase("Y")) 
     { 
      okToOverwrite = true; 
     } 
     else 
     { 
      System.out.println("Enter a new filename."); 
      combinedFileName = keyboard.nextLine(); 
      combinedFile = new File(combinedFileName); 
     } 
    } 

    if (file1.exists() && file2.exists()) 
    { 

     Scanner list1 = new Scanner(file1); 
     Scanner list2 = new Scanner(file2); 
     PrintWriter outputFile = new PrintWriter(combinedFile); 

      while (list1.hasNext() && list2.hasNext()) 
      { 
       nextName1 = list1.nextLine(); 
       nextName2 = list2.nextLine(); 

       if(nextName1.compareToIgnoreCase(nextName2) <0) 
       { 
        outputFile.print(nextName1); 
        nextName1 = list1.nextLine(); 
        outputFile.print(nextName1); 
       } 
       else if(nextName1.compareToIgnoreCase(nextName2) >0) 
       { 
        outputFile.println(nextName2); 
        nextName2 = list2.nextLine(); 
        outputFile.println(nextName2); 
       } 
       else 
       { 
        outputFile.println(nextName1); 
        nextName1 = list1.nextLine(); 
        outputFile.println(nextName1); 
        outputFile.println(nextName2); 
        nextName2 = list2.nextLine(); 
        outputFile.println(nextName2); 
       }  
      } 

      while (list1.hasNext() && !list2.hasNext()) 
      { 
       outputFile.println(nextName1); 
      } 

      while (list2.hasNext() && !list1.hasNext()) 
      { 
       outputFile.println(nextName2); 
      }  
    } 
} 
}   

回答

1

当你使用一个作家像类你PrintWriter需要确保您致电flush()方法要打印的东西到标准输出或文件中的每个时间。所以基本上每当你调用PrintWriter打印方法中的任何一种时,它都会写入内部缓冲区,调用flush()会将缓冲区发送到相应的输出流。

 while (list1.hasNext() && list2.hasNext()) 
     { 
      nextName1 = list1.nextLine(); 
      nextName2 = list2.nextLine(); 

      if(nextName1.compareToIgnoreCase(nextName2) <0) 
      { 
       outputFile.print(nextName1); 
       nextName1 = list1.nextLine(); 
       outputFile.print(nextName1); 
      } 
      else if(nextName1.compareToIgnoreCase(nextName2) >0) 
      { 
       outputFile.println(nextName2); 
       nextName2 = list2.nextLine(); 
       outputFile.println(nextName2); 
      } 
      else 
      { 
       outputFile.println(nextName1); 
       nextName1 = list1.nextLine(); 
       outputFile.println(nextName1); 
       outputFile.println(nextName2); 
       nextName2 = list2.nextLine(); 
       outputFile.println(nextName2); 
      } 
      outputFile.flush(); // <--- flush added here  
     } 

     while (list1.hasNext() && !list2.hasNext()) 
     { 
      outputFile.println(nextName1); 
      outputFile.flush(); // <--- flush added here 
     } 

     while (list2.hasNext() && !list1.hasNext()) 
     { 
      outputFile.println(nextName2); 
      outputFile.flush(); // <--- flush added here 
     }  

这些调用flush()应写入文件。

+0

或简单地使用'autoFlush'来调用构造函数'PrintWriter(OutputStream out,boolean autoFlush)' – 2012-04-07 17:19:49

+0

冲洗问题是我第一个怀疑,但即使如此,文件应该在关闭句柄并且VM退出时被刷新。这个程序还有其他问题。 – aaron 2012-04-07 17:23:05

+0

@ Eng.Fouad根据文档,autoflush选项仅适用于'println','printf'和'format'。海报使用'print()'与'println'混合使用,所以我选择了手动调用'flush()' – 2012-04-07 17:23:08

0

该程序有逻辑错误。当我测试长度不均匀的文件时,它会在最后两个while循环中的一个循环中陷入无限循环,并确实写入输出文件,直到程序被强制终止。如果文件长度相同,则扫描器无法读取if/else子句中的一行。

+0

我该如何解决它? – rahme24 2012-04-07 18:28:29

+0

另外,你是如何得到它写入第三个文件的?由于某种原因,它仅向第三个文件写入空白文件。 – rahme24 2012-04-07 18:29:04

+0

我不知道为什么它不会将输出文件写入我的头顶,但行为可能因输入而异。我的两个文件分别只包含“a”“b”“c”和“d”“e”“f”,每个文件都包含在一行当中。您需要通过打印输出(System.err.println(“将xxxx写入文件yyy”))或通过步调试器来调试此程序,以便了解程序正在执行的操作。 – aaron 2012-04-09 20:59:17

相关问题