有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

文件读取器在Java中读取和写入TXT文件

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

另外,我使用的是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); 
        }

共 (1) 个答案

  1. # 1 楼答案

    //阅读

        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();