2012-04-07 95 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

相關問題