简体   繁体   English

用 Java 读取和写入 TXT 文件

[英]Reading and Writing TXT Files in Java

I know this is probably entry level stuff but I need some help.我知道这可能是入门级的东西,但我需要一些帮助。 Im working on this for a class I've exhausted my class resources so I'm in need of some help我正在为一个班级工作,我已经用完了我的班级资源,所以我需要一些帮助

Also I am using Eclipse and it's not showing any errors另外我正在使用 Eclipse,它没有显示任何错误

I have an array that has been written to a txt file.我有一个已写入 txt 文件的数组。 I am having trouble reading it.我在阅读时遇到问题。 Basically the program forces the customer to order three times, the writer writes it three times, exactly like it should, but I'm not sure how to get the reader to read it three times, it's only reading the first.基本上程序强迫客户订购三遍,作者写了三遍,完全符合它应该的,但我不知道如何让读者阅读它三遍,它只是阅读第一遍。

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

// reading // 读

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

//so the String k is the answer //所以字符串k是答案

//writing : //写作 :

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM