#File reading & Writing

4 messages · Page 1 of 1 (latest)

coarse fable
#

I've got my code to read and write to text files, but when I try to save to the file, it just makes a blank one. Not sure what's causing this. Any help would be appreciated, as I'm sure I've overlooked something really simple. I know my code is messy and not nice, but I tried to work with as many new things as I could on this project and I've already learned a ton, so the next one will be better. I also know there are many improvements to be made, but I want to keep my original base so I can see how I improve from project to project.

here is full code: https://pastebin.run/gn-h-5njjdh7

and here are the snippets that are used to read/write.

    static void ReadFromFile(ArrayList<String> contactList) {
        try {
            FileReader readFileContact = new FileReader("ContactList.txt");
            BufferedReader readContact = new BufferedReader(readFileContact);

            String tempContact;

            while ((tempContact = readContact.readLine()) != null) {
                contactList.add(tempContact);
            }

        }catch (IOException e) {
            e.printStackTrace();
        }

    }```
```java
    static void WriteToFile(ArrayList<String> contactList) {
        try {
            FileWriter writeFileContact = new FileWriter("ContactList.txt");
            BufferedWriter writeContact = new BufferedWriter(writeFileContact);

            for (String contact : contactList) {
                writeContact.write(contact);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }```

cant paste the rest but I use `WriteToFile` at the end of add,edit,delete and exit. Meaning it first runs my normal code to perform the action and then I added `WriteToFile();`.

Like this.
```java
                    case 1:
                        addContact(contactList, scanner);
                        WriteToFile(contactList);
                        break;```
inland minnowBOT
#

This post has been reserved for your question.

Hey @coarse fable! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

coarse fable
#

Figured it out. I was using try instead of try-with-resource when writing meaning it never closed for anybody ever having the same issue.

Old Code

    static void WriteToFile(ArrayList<String> contactList) {
        try {
            FileWriter writeFileContact = new FileWriter("ContactList.txt");
            BufferedWriter writeContact = new BufferedWriter(writeFileContact);

            for (String contact : contactList) {
                writeContact.write(contact);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }```

Fixed Code
```java
    static void WriteToFile(ArrayList<String> contactList) {
        try (FileWriter writeFileContact = new FileWriter("ContactList.txt");
            BufferedWriter writeContact = new BufferedWriter(writeFileContact)) {

            for (String contact : contactList) {
                writeContact.write(contact);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }```