J'ai l'instruction principale ci-dessous. L'auteur tamponné génère un nouveau fichier .txt mais n'écrit rien. Une idée pourquoi? cela pourrait-il avoir à voir avec le scanner ne pas être fermé correctement? Je ne suis pas aussi familier avec l'écrivain tamponné, mais après quelques recherches, je pense que je l'appelle correctement. Aucune suggestion?L'enregistreur en mémoire tampon n'écrit pas dans le fichier
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] + ", ");
}
Avez-vous essayé de rincer et de fermer votre BufferedWriter? – Aakash
Votre code s'arrête à la fin. – shmosel