需要在更改项目后更新我的outfile

时间:2018-04-10 18:49:05

标签: java append overwrite

对于我的程序,我设置了可以编辑和更改程序本身存储在outfile中的项的值。但是,它们更改的数字仅在程序本身中更新。例如,如果我出售10个番茄酱而不是我的程序,我会有0但是我的outfile仍然会说我有10个。我需要我的outfile用我的程序更新。我提出了一种覆盖方法,但它目前所做的只是在outfile中的新行上添加内容,我不确定如何实际更新存储在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());
    }


}

}

Outfile内容:

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

1 个答案:

答案 0 :(得分:0)

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

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