Monte Scott Barber
Monte Scott Barber

Reputation: 11

My File Clears Everytime I Run My Code - Java

I have Server-Client messenger - not important - and I have a settings file to store the settings, but for some reason when I run the code, the settings file clears. Here is the code that makes the settings file and directory:

boolean exists = new File(System.getProperty("user.home")+"\\Documents\\Messenger Server").mkdir();
        File directory = new File(System.getProperty("user.home")+"\\Documents\\Messenger Server");
        File settingsFile = new File(System.getProperty("user.home")+"\\Documents\\Messenger Server\\settings.txt");
        if(exists){
            try {
                directory.createNewFile();
                System.out.println("Created");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
if(!settingsFile.exists()){
            try {
                settingsFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("Created Settings.txt");
            System.out.println(settingsFile.getAbsolutePath());
        }

And this is the code that reads and writes to the file:

try {
                    FileReader fileReader = new FileReader(settingsFile);
                    FileWriter fileWriter = new FileWriter(settingsFile);
                    BufferedReader bf = new BufferedReader(fileReader);
                    BufferedWriter bw = new BufferedWriter(fileWriter);
                }catch(IOException b){
                    b.printStackTrace();
                }
                ArrayList<String> settingList = new ArrayList<String>();
                for (int i = 0; i < 6; i++) {
                    try {
                        settingList.add(bf.readLine());
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
                if(!settingList.get(0).equals("alwaysOnTop=true")&&!settingList.get(0).equals("alwaysOnTop=false")){
                    try {
                        bw.write("alwaysOnTop=false");
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }

Upvotes: 0

Views: 130

Answers (2)

Renis1235
Renis1235

Reputation: 4700

The answer from @JustusG is correct.

Still, I would not recommend using .txt files to keep the settings of your app. Since they are hard to maintain, you may have duplicate settings (because of appending...) and so on.

I would recommend using .properties files. At the end of the day they do the same thing, it's just that .properties files have classes to read and to write your settings/properties.

Here is an example:

Properties prop = new Properties();
File propFile = new File("path/to/app.properties");
InputStream in = new FileInputStream(propFile); // Open the prop file
prop.load(in); // Load it in the Properties object

prop.setProperty("setting1", "value1"); // Setting a new setting to what you need OR setting an old setting to a new value.

String value2 = prop.getProperty("setting2"); // Reading a property

//And at the end, writing the properties that you changed (without duplicates)
prop.store(new FileOutputStream("xyz.properties"), null);

Upvotes: 0

Justus G
Justus G

Reputation: 41

The FileWriter constructor can take a boolean argument ( FileWriter(File file, boolean append) ), which if true makes the FileWriter append to the file instead of overwriting it every time. Like this:

FileWriter fileWriter = new FileWriter(settingsFile, true);

Upvotes: 4

Related Questions