0

As you can tell by the title, What I simply want to do is to either update the file by removing or adding objects from an ArrayList.

My code is pretty big and you don't really have to see it all(it works as intended). All you have to know is that I have a method called writetoFile(There is nothing in that method because that is where I am currently) that is suppose to be called every time I add or remove a object from the arrayList. I've tried using the BufferedWriter method but that class doesn't have any methods that can write objects.

If you really need to see my code, here is a link to pastebin. http://pastebin.com/eFXYtAw2

The file is also a CSV file.

ChronoTrigger
  • 35
  • 1
  • 8

2 Answers2

0

Since your Product class has a custom toString() method, you should use a PrintWriter instead of a BufferedWriter since it has some fun and convenient methods, like println(), that output a string representation of an object.

Something like this:

public static void writeFile(ArrayList<Product> p, String file) throws IOException {
    PrintWriter pw = new PrintWriter(new FileWriter(file));

    for (Product pro : p){
        pw.println(pro);

        /*
        // And of course something less fancy like this would work too
        pw.println(pro.getProductName() 
            + "," + pro.getQuantity()
            + "," + pro.getPrice()
            + "," + pro.getUPC());
        */
    }
    pw.close();
}
Kilreaux
  • 258
  • 2
  • 5
  • What you suggested simply outputs whatever the arraylist has. Sure I can see that the arrayList has all the information that I inputted, but the file doesn't reflect that and that is the the purpose of the writefile method; To update the file by writing back to it. – ChronoTrigger Mar 02 '15 at 03:16
  • I'm sorry but I'm afraid I don't understand. A PrintWriter can output to a file. If your entire database is already loaded into the ArrayList, what is wrong with clearing the file and dumping it all back in? – Kilreaux Mar 02 '15 at 03:38
  • Well maybe I'm not understanding(this is still pretty new to me). Here is what I'm saying. Let say you have a file like this: Sony Headphones,15,3.99,123456 Motorola Tablet,55,499,987654 This mean that the file has two items of interest. If I add HDMI, 10, 500, 945632, it will add it to the arrayList but not the file and that is what I'm trying to do. – ChronoTrigger Mar 02 '15 at 03:40
  • @ChronoTrigger either call the writeFile method every time you change the list of wrap the list in a custom list which allows you to register events/write to file on change: http://stackoverflow.com/questions/16529273/how-to-add-listener-on-arraylist-in-java – Adrian Leonhard Mar 02 '15 at 03:51
  • It's at that point that you would call this writeFile method, every time you change the ArrayList. If you wanted it to happen automatically, you would have to make your own class that extends ArrayList and overrides the add() and remove() methods so that in addition to doing what a normal ArrayList does (super.add() and super.remove()), it would write to a file. – Kilreaux Mar 02 '15 at 03:55
  • I've added an answer up above. – ChronoTrigger Mar 02 '15 at 04:15
  • I have new updated answer that is close to what I want but there are still a couple of problems. – ChronoTrigger Mar 02 '15 at 16:03
0

So I've looked up a class that I thought might solved my problem but it really didn't(It's called ObjectOutputStream). When I used it, it removed all of my data from the file(at first I thought it did, but in my excel file, some of the data looked like $%#@$@%12sony headphones or something like that but this didn't appear in my actual file).

Here is my method of writeFile()

public static void writeToFile(ArrayList<Product> p)
{
    try 
    {
        BufferedWriter writer = new BufferedWriter(new FileWriter("product.csv"));

        for (Product p1 : p) 
        {
            writer.write(p1.toString());
        }

        writer.close();
    } 
    catch (IOException ex) 
    {
        System.out.println("Input/Output format error");
    }
}
ChronoTrigger
  • 35
  • 1
  • 8