用Java读写TXT文件

用Java读写TXT文件,java,filereader,readline,Java,Filereader,Readline,我知道这可能是入门级的东西,但我需要一些帮助。 我正在为一个班级做这个 我已经耗尽了我的课堂资源,所以我需要一些帮助 另外,我使用的是Eclipse,它没有显示任何错误 我有一个数组已写入txt文件。我读起来有困难。 基本上,程序强迫客户订购三次,作者写了三次,就像它应该做的那样,但我不确定如何让读者阅读三次,它只是阅读第一次 public static void writeOrderHeader(String name, String returning) throws Exception

我知道这可能是入门级的东西,但我需要一些帮助。 我正在为一个班级做这个 我已经耗尽了我的课堂资源,所以我需要一些帮助

另外,我使用的是Eclipse,它没有显示任何错误

我有一个数组已写入txt文件。我读起来有困难。 基本上,程序强迫客户订购三次,作者写了三次,就像它应该做的那样,但我不确定如何让读者阅读三次,它只是阅读第一次

 public static void writeOrderHeader(String name, String returning) throws Exception
        {
            File file;
            file = new File("order.txt");
            PrintWriter pw = new PrintWriter(file);
            pw.println(name);
            pw.println(returning);
            pw.close();
        }

        public static void writeOrderFile(String product, String size, String type, int qty, double total) throws Exception
        {
            String file = "order.txt";
            PrintWriter pw = new PrintWriter(new FileWriter(file, true));
            pw.println(product);
            pw.println(type);
            pw.println(size);
            pw.println(qty);
            pw.println(total);
            pw.close();
        }
        public static void confirmation() throws IOException
        {
            File file;
            BufferedReader bf = null;
            String name, returning, product, type, size, qty, total;
            int intQty;
            double dblTotal;
            file = new File("order.txt");
            FileReader fr = new FileReader(file);
            bf = new BufferedReader(fr);
            name = bf.readLine();
            returning = bf.readLine();
            product = bf.readLine();
            size = bf.readLine();
            type = bf.readLine();
            qty = bf.readLine();
            total = bf.readLine();
            fr.close();
            intQty = Integer.parseInt(qty);
            dblTotal = Double.parseDouble(total);
            String nameOutputMsg = "Welcome " + name + ".\n";
            String returnOutputMsg = "Your returning customer status is " + returning + ".\n";
            String productOutputMsg = "Your first choice to buy a/n size " + type + " " + size + " " + product + " with the quantity of " + intQty + ".\n";
            String totalOutputMsg = "Your first Order total is $" + String.format("%.2f", dblTotal) + ".\n";
            String goodbyeOutputMsg = "Thanks for ordering at ThinkGeek!";
            String outputMsg = nameOutputMsg + returnOutputMsg + productOutputMsg + totalOutputMsg + goodbyeOutputMsg;
            JOptionPane.showMessageDialog(null, outputMsg); 
        }
//阅读

    File f = new File(Address);
    FileReader r = new FileReader(f);
    BufferedReader b = new BufferedReader(r);
    String out = "";
    String k="";
    while( (out=b.readLine()) != null) {
        k+=out;
    }
    b.close();
//所以字符串k就是答案

//写作:

    File file = new File(Address);
    FileWriter fw = new FileWriter(file, true);
    BufferedWriter bw = new BufferedWriter(fw);
    bw.write(YourString);  
    bw.close();

这里的代码太多,我们无法帮助您。正如您使用Eclipse一样,您可以进行出色的调试。如果有必要,可以向控制台添加日志记录或输出,并确定哪里出了问题。然后使用调试器逐步完成代码。一旦您确定了确切的问题,您可能已经知道如何解决它-如果您不知道,请创建一个新的问题并在此处发布。您未来在此处编码和发布的另一个注意事项是:坚持Java命名约定-类始终在PascalCase中。这些约定是使用整个Java社区编译的,不这样做会使代码很难阅读。此外,虽然您的格式是OK的,但您对空格的使用是可怕的——这使得代码更难理解。删除不必要的空白,让Eclipse自动格式化来完成其余的工作。我刚刚阅读了上面的一些代码,但是先将订单存储在中央数组或列表中,然后将其写入文件难道不更容易吗?这样做可以避免在当前会话中添加新订单时的读取操作。我已经在Eclipse中调试并运行了代码和ect,没有出现错误…很抱歉,我像一个超级noob,这个类几乎没有解释,字符串K在做什么?首先字符串K的值是,然后我逐字读取文件并将其添加到字符串K中