-1

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;   
}

Here Is a Image Of It After It Was Read To The Console

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) {
        }
    }
}
Kainix
  • 1,186
  • 3
  • 21
  • 33
user5517779
  • 33
  • 2
  • 10

2 Answers2

1

Based on this ...

The Only OutPut I get Is "Going In", and "Coming Out". I Don't Get The Other OutPut, "This is the file size"

... the problem is that you are calling writeSaleToTextFile on an empty list. That is the only plausible explanation for that console output ... assuming that the code is as you have shown us.

Look at the code where you are calling readSaleData and writeSaleToTextFile and check that they are really reading / writing the same list.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

Although not directly clear, your problem seems to be in the calling code and in the reading method.

Your reading method's signature is that:

public static ArrayList<Sales> readSaleData(ArrayList<Sales> sale)

This seems to be wrong, as you expect a list to be provided to the method but you return another one from it.

So I now suspect the code that calls your read and write method to be something like this:

ArrayList<Sales> someList = new ArrayList<>();
readSaleData(someList); // This will disregard the return value
writeSaleToTextFile(someList);

As the call to the read method disregards the result list, your original list is still empty when calling the writing method. Hence, the output, which clearly indicates that you call the writing method on an empty list.

You should correct the reading method's signature to

public static ArrayList<Sales> readSaleData()

Then you can't call it like I mentioned, and you must change the calling code to

ArrayList<Sales> someList = readSaleData();
writeSaleToTextFile(someList);
Seelenvirtuose
  • 20,273
  • 6
  • 37
  • 66