java - 无法使用java追加到文件?

标签 java file append

我目前正在尝试将字符串“testing”添加到 inventory.txt,只是为了查看我的文件是否已成功 append 。由于某种原因,它不会 append !它编译并成功运行,但“测试”从未出现在我的文件中。我在网上搜索了一堆,我认为我做得对......有人可以尝试帮助我找到我的错误吗?谢谢!

/**
Add in javadoc comments
*/


//import statements
import java.io.*;
import java.util.*;

public class Try { 
    public static void main(String[] args){
        //variables
        Scanner kb = new Scanner(System.in);
        boolean valid = false;
        int mainSelect = 0;
        int itemOption = 0;
        boolean valid2 = false;
        boolean returnToMain = false;
        String theName = "";
        double thePrice = 0;
        int theQuantity = 0;
        double currBal;
        String lampName;
        double lampPrice;
        int lampQuantity;
        String chairName;
        double chairPrice;
        int chairQuantity;
        String deskName;
        double deskPrice;
        int deskQuantity;
        int sellAmnt;
        boolean valid3 = false;

        //create file
        try{
            PrintWriter outputFile = new PrintWriter("inventory.txt");
            outputFile.println("3000.0");
            outputFile.println("Lamps 15.3 400");
            outputFile.println("Chairs 19.95 250");
            outputFile.println("Desks 95.0 300");
            outputFile.close();
        }
        catch(IOException e){
            System.out.println("File cannot be created.");
        }

        //read data in from file
        try{
            File file = new File("inventory.txt");
            Scanner inFile = new Scanner(file);
            currBal = inFile.nextDouble();
            lampName = inFile.next();
            lampPrice = inFile.nextDouble();
            lampQuantity = inFile.nextInt();
            chairName = inFile.next();
            chairPrice = inFile.nextDouble();
            chairQuantity = inFile.nextInt();
            deskName = inFile.next();
            deskPrice = inFile.nextDouble();
            deskQuantity = inFile.nextInt();
            inFile.close();

            //present user with main menu
            do{
            System.out.println("Current Balance: $" + currBal);
            System.out.println("\t1. " + lampName  + "\t\t(" + lampQuantity +  " at $" + lampPrice + ")");
            System.out.println("\t2. " + chairName + "\t\t(" + chairQuantity +  " at $" + chairPrice + ")");
            System.out.println("\t3. " + deskName + "\t\t(" + deskQuantity +  " at $" + deskPrice + ")");
            System.out.println("\t0. Exit");

            while(valid == false){
                System.out.print("\nPlease enter choice: ");
                try{
                    mainSelect = kb.nextInt();
                    if(0 <= mainSelect || mainSelect >=  3){
                        valid = true;
                    }
                    else{
                        System.out.println("That is not a valid selection. Try again.");
                    }
                }
                catch(InputMismatchException ime){
                    System.out.println("That is not a valid selection. Try again.");
                    kb.next();
                }
            }
            //present user with second menu
            switch(mainSelect){
                case 1:
                    theQuantity = lampQuantity;
                    thePrice = lampPrice;
                    theName = lampName;
                    break;
                case 2:
                    theQuantity = chairQuantity;
                    thePrice = chairPrice;
                    theName = chairName;
                    break;
                case 3:
                    theQuantity = deskQuantity;
                    thePrice = deskPrice;
                    theName = deskName;
                    break;
                case 0:
                    System.exit(0);
                    break;
            }
            System.out.println("\nCurrent balance: $" + currBal);
            System.out.println("Current Quantity: " + theQuantity);
            System.out.println("Current price: $" + thePrice);
            System.out.println("1. Sell " + theName);
            System.out.println("2. Buy " + theName);
            System.out.println("3. Change price");
            System.out.println("0. Return to main menu");

            while(valid2 == false){
                System.out.print("\nPlease enter choice: ");
                try{
                    itemOption = kb.nextInt();
                    if(0 <= itemOption || itemOption >=  3){
                        valid2 = true;
                    }
                    else{
                        System.out.println("That is not a valid selection. Try again.");
                    }
                }
                catch(InputMismatchException ime){
                    System.out.println("That is not a valid selection. Try again.");
                    kb.next();
                }
            }
            //Action: sell
            if(itemOption == 1){
                do{
                    System.out.print("Amount to sell (current quantity: " + theQuantity + "): ");
                    sellAmnt = kb.nextInt();
                    returnToMain = true;
                    try{
                        sellAmnt = kb.nextInt();
                        if(0 <= sellAmnt ||  sellAmnt >= theQuantity){
                            valid3 = true;
                            try{
                                //append file
                                FileWriter fw = new FileWriter("inventory.txt", true);
                                PrintWriter pw = new PrintWriter(fw);
                                pw.write("testing"); //not working!!!!!!!!!!
                                pw.close();
                            }
                            catch(IOException e){
                                System.out.println("File not found.");
                            }
                        }
                        else{
                            System.out.println("\nThat amount is not within quantity bounds. Please enter a number within bounds.");
                        }
                    }
                    catch(InputMismatchException ime){
                        System.out.println("That is not a valid number. Try again.");
                        kb.next();
                    }
                }while(valid3 == false);
            }
            //Action: buy
            if(itemOption == 2){
                returnToMain = true;
            }
            //Action: change price
            if(itemOption == 3){
                returnToMain = true;
            }

            }while(returnToMain == true);
        }
        catch(FileNotFoundException e){
            System.out.println("Cannot find file.");
        }
    }
}

最佳答案

这里:

//create file
try{
    PrintWriter outputFile = new PrintWriter("inventory.txt");
    outputFile.println("3000.0");
    outputFile.println("Lamps 15.3 400");
    outputFile.println("Chairs 19.95 250");
    outputFile.println("Desks 95.0 300");
    outputFile.close();
}
catch(IOException e){
    System.out.println("File cannot be created.");
}

您总是重新创建文件并向其中添加内容,然后保存它。无论您如何处理该文件,每次执行应用程序时都会重写该文件。

作为建议,只有当您的文件不存在时才应执行这段代码。

关于java - 无法使用java追加到文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28441223/

相关文章:

java - 对应于 C# 中的匿名接口(interface)实现

Golang 从 slice append 函数 "evaluated but not used"中删除重复整数

java - 是否可以获取对 Java 方法的引用以便稍后调用它?

node.js - 如何使用 socket.io 处理 Node 服务器上的并发文件写入请求

c - 从平面文件中读取而不输出整个文件

ruby-on-rails - 如何在不调用依赖 : :destroy on associations 的情况下销毁 Rails 模型

c - append 在链表的末尾

python - 将列表添加到空的 python 列表问题

java - JMS处理期间apachemq异常

java - 如何调整 Swing 进度指示器上的图形?