I have to read a text file into the console, and then write it back in it. I was able to read the file to the console, but when I write back to it and try to read it next time, the file is empty. Can anyone tell me where I'm making mistake?
Here Is The Code For Reading the Original File
public static ArrayList<Sales> readSaleData(ArrayList<Sales> sale) {
System.out.println();
ArrayList<Sales> sales = new ArrayList<Sales>();
Frame f = new Frame();
FileDialog saveBox = new FileDialog(f, "Reading text file", FileDialog.LOAD);
saveBox.setVisible(true);
String salePrice = saveBox.getFile();
String fileSavePlace = saveBox.getDirectory();
File inFile = new File(fileSavePlace + salePrice);
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(inFile));
String line;
int weekCount = 0;
while (((line = in.readLine()) != null)) {
weekCount++;
System.out.println("Week " + weekCount + "\t$" + line.replace(",", " $"));
String[] saleData = line.split(",");
for (String data : saleData) {
double price = Double.parseDouble(data);
Sales s = new Sales(price);
sales.add(s);
}
}
} catch (IOException io) {
System.out.println("There Was An Error Reading The File");
io.printStackTrace();
} finally {
try {
in.close();
} catch (Exception e) {
}
}
return sales;
}
And Here Is The Code For Writing It To A Text File
public static void writeSaleToTextFile(ArrayList<Sales> s) {
Frame f = new Frame();
FileDialog foBox = new FileDialog(f, "Saving invoice information", FileDialog.SAVE);
foBox.setVisible(true);
String saleName = foBox.getFile();
String dirPath = foBox.getDirectory();
File outFile = new File(dirPath + saleName);
PrintWriter out = null;
try {
out = new PrintWriter(new BufferedWriter(new FileWriter(outFile)));
System.out.println("Going in");
for (int i = 0; i < s.size(); i++) {
Sales sale = s.get(i);
out.println(sale.toString());
System.out.println("This is the file size" + sale);
}
System.out.println("Coming Out");
}
catch (IOException io) {
System.out.println("An IO Exception occurred");
io.printStackTrace();
} finally {
try {
out.close();
} catch (Exception e) {
}
}
}