0
public static void main(String[] args) throws IOException, ClassNotFoundException {
    String file = "C:\\Users\\RaviKiran Reddy\\Desktop\\JBNR\\NewBankAccounts.csv";
    List<String[]> newAccounts = Csv.read(file);
    List<Account> opaccounts = new LinkedList<Account>();
    for (String[] accountholdersdata : newAccounts) {
        String name = accountholdersdata[0];
        String ssn = accountholdersdata[1];
        String acctype = accountholdersdata[2];
        double initialDeposit = Double.parseDouble(accountholdersdata[3]);
        if (acctype == "Checking") {
            opaccounts.add(new Checking(name, ssn, acctype, initialDeposit));
        }
        else {
            opaccounts.add(new Savings(name, ssn, acctype, initialDeposit));
        }
    }
    ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("records.txt"));
    out.writeObject(opaccounts);
    ObjectInputStream in = new ObjectInputStream(new FileInputStream("records.txt"));
    LinkedList<Account> a = new LinkedList<Account>();
    a = (LinkedList<Account>) in.readObject();
}

I wanted to build a real world bank application, so I stored the objects in a ".txt" file with the help of LinkedList and ObjectOutputStream. But the problem is whenever I perform operations on a object and close the project, and after when I again open the project and try to retrieve the data, the data regarding the previous operations is not being stored.

How to overcome this and store each and every operation performed on the objects?

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
ravikiran
  • 33
  • 6
  • 1
    Consider using a database instead of text files. – Paul Benn Sep 18 '18 at 06:43
  • 4
    *a real world bank application. so I stored the objects in a ".txt" file with the help of LinkedList and ObjectOutputStream.* you made my day :-) We can't help you find where the problem is in your code if you don't post your code. We can't see your screen. – JB Nizet Sep 18 '18 at 06:44
  • Who’s “we”? I, for one, can see his screen :p – Roy Shahaf Sep 18 '18 at 06:49
  • i have added it @ JB Nizet – ravikiran Sep 18 '18 at 06:56
  • It's barely readable because it's not indented. But read your code: it always starts by writing to the file, and then reads what it just wrote. So of course, if you restart the app, it ill start by overwriting the content of the file, and not read what the previous execution wrote. – JB Nizet Sep 18 '18 at 06:59
  • There are some problems with your code. In Java, we follow the Java Naming Conventions, that means that variable names are written in camelCase (starting with lowercase, and no underscores). Second, you are mixing arrays and `List`s, which is discouraged. Third, you are comparing strings with the `==` operator. You should [use the `equals` method](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). – MC Emperor Sep 18 '18 at 07:13
  • if you want to store stuff in a file I suggest XML instead of text. Then you can search and add stuff to it. a little more overhead of course.. – Jack Flamp Sep 18 '18 at 07:15
  • ok, but could you explain how to stop overwriting and get what I wanted(to store the operations performed on the object) @ JB Nizet – ravikiran Sep 18 '18 at 07:15
  • Do not use standard deserialization. Do implement your own. – Oleg Cherednik Sep 18 '18 at 07:29
  • I don't exactly know what is meant by own deserialization is. Could you tell me what it is @ oleg cherednik – ravikiran Sep 18 '18 at 08:30

1 Answers1

-1

You need to close or flush "out" the object before reading it again immediately. And better use hashmap for key-value mapping for such database

 ObjectOutputStream out = new ObjectOutputStream(new 
 FileOutputStream("records.txt"));
 out.writeObject(opaccounts);
 out.close(); // use this or flush()
Pandey Amit
  • 657
  • 6
  • 19
  • if we close the file using out.close, how can we write anything to the file again!! – ravikiran Sep 18 '18 at 08:28
  • if we close the file using out.close how can we write to the file again, because the link to the file gets collapsed if we run out.close. – ravikiran Sep 18 '18 at 08:34
  • I mentioned close or flush, it’s depends on your use case if you want to write it again with same object. Better use flush then read back – Pandey Amit Sep 18 '18 at 14:41