Skip to content

Instantly share code, notes, and snippets.

@benjjo
Last active April 21, 2020 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benjjo/1928ada91e5acdc8c7e73b3aece292bc to your computer and use it in GitHub Desktop.
Save benjjo/1928ada91e5acdc8c7e73b3aece292bc to your computer and use it in GitHub Desktop.
Example of a buffered file writer
/**
* Writes the Object data from the Objects ArrayList into a CSV file.
* Data is written to the file in the following format:
*
* String,int,String,int
* String,int,String,int
*/
public void writeObjectData()
{
String pathName = FileChooser.getFilename(); // pick a file chooser and change this.
File aFile = new File(pathName);
BufferedWriter bufferedFileWriter = null;
try
{
bufferedFileWriter = new BufferedWriter(new FileWriter(aFile));
for(Object eachObject : this.getObjectsList())
{
bufferedFileWriter.write(eachObject.getName() + "," + eachObject.getYears() + ","
+ eachObject.getProductCode() + "," +eachObject.getSales() + "\n");
}
}
catch (Exception anException)
{
System.out.println("Error: " + anException);
}
finally
{
try
{
bufferedFileWriter.close();
}
catch (Exception anException)
{
System.out.println("Error: " + anException);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment