2012-04-07 10 views
1

アルファベット順に並んでいる2つのファイルから名前とID番号を読み込み、アルファベット順に完全に3番目のファイルに結合するプログラムを作成する必要があります。私のプログラムがファイルに情報を書き込まないのはなぜですか?

私のファイルには、2つのファイルを読み込み、第三が作成されますが、それは3番目のファイルには何も書き込まれません何らかの理由で姓、名 ID(例:rbb091020) 姓、名 ID

。ここで

あなたはPrintWriterのようなライタークラスを使用すると、あなたがflush()方法あなたがSTDOUTまたはファイルに何かを印刷するたびに呼び出すことを確認する必要があり

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()を呼び出すとバッファが適切な出力ストリームに送信されます。

 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

または、単純に 'printWriter(OutputStream out、boolean autoFlush) 'を呼び出すときに' autoFlush'にしてください。 –

+0

最初の疑いがあったのですが、ハンドルを閉じてVMを終了したときにファイルをフラッシュしてください。このプログラムには他の問題があります。 – aaron

+0

@ Eng.Fouadドキュメントによると、autoflushオプションは 'println'、' printf'、 'format'に対してのみ有効です。ポスターは 'println'と混在した' print() 'を使用していますので、手動で' flush() 'を呼び出すようにしました –

0

このプログラムには論理エラーがあります。不均一な長さのファイルでテストすると、ループの最後の2つのうちの1つで無限ループに陥り、実際にプログラムが強制終了するまで出力ファイルに書き込まれます。ファイルの長さが同じであれば、scannerはif/else節のいずれかの行を読み取ることができません。

+0

どうすれば修正できますか? – rahme24

+0

また、どのようにして3番目のファイルに書き込むことができましたか?何らかの理由で空のファイルを第3のファイルに書き込むだけです。 – rahme24

+0

私は頭の上から出力ファイルに書き込まない理由はわかりませんが、動作はおそらく入力によって異なります。私の2つのファイルには、単に "a" "b" "c"と "d" "e" "f"が含まれていました。このプログラムをデバッグする必要があります。出力(System.err.println( "xxxxをファイルyyyに書き込む"))またはステップデバッガを介してデバッグする必要があります。 – aaron

関連する問題