Ich habe die folgende Hauptaussage. Der gepufferte Writer generiert eine neue TXT-Datei, schreibt aber nichts. Irgendeine Idee warum? könnte das damit zu tun haben, dass der scanner nicht richtig geschlossen wird? Ich bin mit dem gepufferten Schreiber nicht so vertraut, aber nach einigem Nachdenken denke ich, dass ich es richtig nenne. Irgendwelche Vorschläge?Buffered Writer nicht in Datei schreiben
public class Lab4Main
{
static QuickSort QuickMethod = new QuickSort();
static HeapSort HeapMethod = new HeapSort();
static MedianOfThree MedianMethod = new MedianOfThree();
public static void main(String[] args) throws IOException {
BufferedWriter bw = null;
int arraySize = 0;
int len = 0;
Scanner input = new Scanner(System.in);
System.out.print("Enter the size of the file to sort: ");
arraySize = input.nextInt();
System.out.println("Application has been set up with size: " + arraySize +"\n");
//initializes what user just entered in
int Array[] = new int[arraySize];
len = arraySize;
try{
Scanner input2 = new Scanner(System.in);
//ask for file path from user
System.out.print("Please enter the file name with extension: " + "\n");
File file = new File(input2.nextLine());
input2 = new Scanner(file);
for (int i = 0 ; input2.hasNext();i++)
{
// System.out.println(input);
int number = input2.nextInt();
Array[i] = number;
}input2.close();
} catch(Exception ex2) {
System.out.println(
"Error reading file path");
System.exit(0);
}
//make copies of array to sort
int quickArray [] = new int[arraySize];
int heapArray [] = new int[arraySize];
int medianOfThreeArray [] = new int[arraySize];
System.arraycopy(Array, 0, quickArray, 0, Array.length);
System.arraycopy(Array, 0, heapArray, 0, Array.length);
System.arraycopy(Array, 0, medianOfThreeArray, 0, Array.length);
// The name of the file to open.
String fileName = "/_trial2.txt";
// Assume default encoding.
FileWriter fileWriter = new FileWriter(fileName);
// Always wrap FileWriter in BufferedWriter.
bw = new BufferedWriter(fileWriter);
try{
bw.write("\nUnsorted Quick: ");
System.out.println("\nUnsorted Quick: ");
for (int i = 0; i < arraySize; i++){
System.out.print(quickArray[i] + ", ");
//bufferedWriter.write(quickArray[i] + ", ");
}
Haben Sie versucht, den BufferedWriter zu spülen und zu schließen? – Aakash
Ihr Code schneidet am Ende ab. – shmosel