curiousX
curiousX

Reputation: 119

Buffered writer not writing to file

I have the below main statement. The buffered writer generates a new .txt file but doesn't write anything. Any idea why? could this have to do with the scanner not being closed correctly? I am not as familiar with the buffered writer, but after some research think i am calling it correctly. Any suggestions?

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] + ", ");
  }

Upvotes: 0

Views: 211

Answers (1)

JackDaniels
JackDaniels

Reputation: 1071

Almost every time I have seen someone complain about BufferedWriter or PrintWriter not writing, it is always because of not flushing.

Important Rule: Always bw.close() Input/Output streams.

bw.close();

Other things you can consider depending on the need:

  1. (Generally not recommended) Enable autoflush when creating BufferedReader: It will be useful in cases like when writing to sockets and you expect a real time communication. Not generally useful when writing to files. bw = new BufferedWriter(fileWriter, true /* autoflush */);
  2. bw.flush(); whenever you think it is appropriate to actually write to disk.

Upvotes: 2

Related Questions