0

So the code I first used to attempt this goes as follows

public static void writeArrayToFile( String fileName) 
{ 
 int [] data = {1,2,3,4,5,6}; 

 try 
 { 
  // wrap fileWritter in bufferedWriter 
  FileWriter fileWriter = new FileWriter(fileName); 
  BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); 

  for(int i=0;i<data.length;i++) 
  { 
   bufferedWriter.write(data[i]); 
   bufferedWriter.newLine(); 
  } 
 } 

 catch(IOException ex) 
 { 
  System.out.println("Error writing to file '" + fileName + "'"); 
 }

}

This code executes and does not throw errors while creating the file specified however does not fill the file with the array. What is it that is wrong with the following code?

I was able to find working code that I implemented which solved the issue however don't understand why this code doesn't work. Also if I run the program twice why does the array not appear twice and only once while making changes to the array updates the existing file with the changes but the old array is gone?

Cameron D
  • 13
  • 2

0 Answers0