【问题标题】:Needing to update my outfile after items are changed项目更改后需要更新我的输出文件
【发布时间】:2018-09-20 12:08:03
【问题描述】:

对于我的程序,我设置了可以编辑和更改存储在程序本身的 outfile 中的项目的值。但是,他们更改为仅在程序本身中更新的数字。例如,如果我卖 10 个番茄酱,而不是在我的程序中,我会得到 0,但我的 outfile 仍然会说我有 10 个。我需要我的 outfile 来更新我的程序。我想出了一个覆盖方法,但它目前所做的只是在输出文件中的新行上添加内容,我不确定我将如何实际更新存储在输出文件中的任何信息,任何帮助都会很棒。

代码:

public class Driver {

public static ArrayList<Item> list = new ArrayList<Item>();
static double myBalance = 100;
/*static ArrayList<Item> list = new ArrayList<Item>();*/


/**
 * @param args
 * @throws IOException 
 * @throws FileNotFoundException 
 */



public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub


    ArrayList<String> inventoryList = new ArrayList<String>();

    BufferedReader readIn = null;
    try {
        readIn = new BufferedReader(new FileReader("inventory.out"));
        readIn.lines().forEach(inventoryList::add);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if(readIn != null) {
            readIn.close();
        }
    }


    for (int i = 0; i < 4; i++) {

        String item = inventoryList.get(i);// input String like the one you would read from a file

        String delims = "[,]"; //delimiter - a comma is used to separate your tokens (name, qty,cost, price)

        String[] tokens = item.split(delims); // split it into tokens and place in a 2D array.

        String name = tokens[0]; System.out.println(name);

        double cost = Double.parseDouble(tokens[1]);System.out.println(cost);

        int qty = Integer.parseInt(tokens[2]);System.out.println(qty);

        double price = Double.parseDouble(tokens[3]);System.out.println(price);

        list.add(new Item(name, cost, qty, price));
    }

    sell("Mayo", 10);
    buy("Ketchup", 20);
    remove_item("Ketchup");
    add_item("Tums", 20, 10, 5);

    overwrite("New line");

    PrintAll();


}




// Method to sell items from the arraylist
public static void sell(String itemName, int amount) {


    for (int i = 0; i < list.size(); i++) {
        if (list.get(i).getName().equals(itemName)) {
            int number = i;
            list.get(number).qty -= amount;
            myBalance += list.get(number).getPrice() * amount; 
        }
    }

}




// Method to buy more of the items in our array list
public static void buy(String itemName, int amount) {


    for (int i = 0; i < list.size(); i++) {
        if (list.get(i).getName().equals(itemName)) {
            int number = i;
            list.get(number).qty += amount;
            myBalance -= list.get(number).getPrice() * amount; 
        }
    }
}



// Method to remove an item completely from our inventory
public static void remove_item(String itemName) {

    for (int i = 0; i < list.size(); i++) {
        if (list.get(i).getName().equals(itemName)) {
            int number = i;
            list.remove(number);
        }
    }
}


public static void add_item(String itemName, double itemCost, int qty, double itemPrice) {
    list.add(new Item(itemName, itemCost, qty, itemPrice));

    }


public static void PrintAll() {

    String output = "";
    for(Item i : list) {
        int everything = i.getQty();
        String everything2 = i.getName().toString();

        output += everything +" "+ everything2 + "\n";       
    }
    JOptionPane.showMessageDialog(null, "Your current balance is: $" + myBalance + "\n" + "Current stock:" + "\n" + output);

}


public static void overwrite(String update) {


    try
    {
        String filename= "inventory.out";
        FileWriter fw = new FileWriter(filename,true); //the true will append the new data
        fw.write("\n"+"add a line");//appends the string to the file
        fw.close();
    }
    catch(IOException ioe)
    {
        System.err.println("IOException: " + ioe.getMessage());
    }


}

}

输出文件内容:

Ketchup,1,10,2
Mayo,2,20,3
Bleach,3,30,4
Lysol,4,40,5

【问题讨论】:

    标签: java append overwrite


    【解决方案1】:

    如果您知道输出文件的名称,请在需要更新时清除输出文件,然后再次写入。您可以使用以下代码擦除文件的内容。

    PrintWriter writer = new PrintWriter(file);
    writer.print("");
    writer.close();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-02
      • 1970-01-01
      • 2015-11-09
      • 1970-01-01
      相关资源
      最近更新 更多