Last active
April 21, 2020 21:48
-
-
Save benjjo/1928ada91e5acdc8c7e73b3aece292bc to your computer and use it in GitHub Desktop.
Example of a buffered file writer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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