Java写入文本文件

时间:2016-04-24 05:02:16

标签: java readfile writefile

我必须读取一个文本文件到控制台,然后回来。我能够将文件读取到控制台,但是当我回写它并下次尝试读取它时,该文件为空。谁能告诉我哪里出错了?

这是阅读原始文件的代码

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

以下是将其写入文本文件的代码

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) {
        }
    }
}

2 个答案:

答案 0 :(得分:1)

基于此...

  

我得到的唯一输出是&#34;进入&#34;和&#34;出来&#34;。我没有得到其他OutPut,&#34;这是文件大小&#34;

...问题是你在空列表上调用writeSaleToTextFile。这是该控制台输出的唯一合理解释......假设代码正如您向我们展示的那样。

查看您调用readSaleDatawriteSaleToTextFile的代码,并检查它们是否正在读取/写入相同的列表。

答案 1 :(得分:1)

虽然不是很清楚,但问题似乎出现在调用代码和阅读方法中。

您的阅读方法的签名是:

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

这似乎是错误的,因为您希望向该方法提供一个列表,但是从该方法返回另一个列表。

所以我现在怀疑调用你的读写方法的代码是这样的:

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

由于对read方法的调用忽略了结果列表,因此在调用write方法时,原始列表仍为空。因此,输出,清楚地表明您在空列表上调用写入方法。

您应该将阅读方法的签名更正为

public static ArrayList<Sales> readSaleData()

然后你不能像我提到的那样调用它,你必须将调用代码更改为

ArrayList<Sales> someList = readSaleData();
writeSaleToTextFile(someList);