Ich habe ein Programm erstellen, die Namen und die ID-Nummern von zwei Dateien liest, die bereits in alphabetischer Reihenfolge sind, und sie auf eine dritte Datei vollständig in alphabetischer Reihenfolge kombinieren .. Die Dateien werden wie so organisiert ...Warum schreibt mein Programm keine Informationen in die Datei?
Nachname, Vorname ID (ex. rbb091020) Nachname, Vorname ID
Aus irgendeinem Grund meine Datei die beiden Dateien liest und erzeugt eine dritte, aber es schreibt nicht alles in die dritte Datei.
Hier ist mein Code ...
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);
}
}
}
}
oder einfach "autoFlush" beim Aufruf des Konstruktors 'PrintWriter (OutputStream out, boolean autoFlush)' –
Spülungsprobleme waren meine erste verdächtige, aber selbst dann sollte die Datei geleert werden, wenn das Handle geschlossen ist und die VM beendet . Dieses Programm hat andere Probleme. – aaron
@ Eng.Fouad Laut den Dokumenten funktioniert die Autoflush-Option nur für 'println',' printf' und 'format'. Das Poster verwendet 'print()' gemischt mit 'println', also habe ich mich für manuelle' flush() 'Anrufe entschieden –