0

So I am writing code that acts as a File System of sorts for a log in system two accounts are pre added, both being admins. they can create accounts. All accounts are stored in an array list for their username, and an array list for their password. I am trying to make it so that when i export this application, when an admin adds to the array list, is sends the information to a txt file, so that the next time the program is run, the program will load any accounts on the txt file. I have tested the loading of accounts, and that works fine, but i cannot seem to get to writing to a txt file. I picked up this code in this method from this site, but im not sure what I am doing wrong:

public static void writeFileContnet(){
    String path = "C:\\Users\\DJANDEK\\Desktop\\LogInPro\\info.txt";
    BufferedWriter writer = null;

    try {
        writer = new BufferedWriter(new FileWriter(path, true));
        if(!loadFileAsString(path).equals("")){
            writer.newLine();
        }
        System.out.println(Account.accounts.get(Account.accounts.size()-1));
        writer.write(Account.accounts.get(Account.accounts.size()-1));

        System.out.println(breaker);
        writer.write(breaker);

        System.out.println(Account.passwords.get(Account.passwords.size()-1));
        writer.write(Account.passwords.get(Account.passwords.size()-1));

        writer.write(breaker);

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

    BufferedReader in = null;
    try {
        in = new BufferedReader(new FileReader(path));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    String line;
    try {
        while((line = in.readLine()) != null)
        {
            System.out.println("it prints this");
            System.out.println(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

just for those wondering about the println's, the console displays: Bob; 1Q2W; BobsPass (the info is correct). I get no errors in the console. To answer the duplicate question, He was told to use a file, not how to use the file. If I have data already on the txt file, the Reader will read and s.o.p it.

Deek3117
  • 57
  • 8
  • Possible duplicate of [How can an app use files inside the JAR for read and write?](http://stackoverflow.com/questions/5052311/how-can-an-app-use-files-inside-the-jar-for-read-and-write) – whistling_marmot Feb 06 '16 at 13:49

2 Answers2

0

You can't write out to a file inside your jar. You'll need to give the path to a file on the file system.

whistling_marmot
  • 3,561
  • 3
  • 25
  • 39
  • Ok, I wasn't sure but I figured that would be a problem. I did try that before, but for some reason the pathing wasnt working correctly. I tried this path: C:\\Users\\DJANDEK\\Desktop\\LogInPro\\info.txt but that did not seem to work. Do i need to do someting about absolute pathing? – Deek3117 Feb 06 '16 at 14:14
  • I did fix the pathing issue (I must not be able to spell because it works fine now), but it still doesn't seem to be writing it. If i have text in the file, it still reads it, but no additional text appears to be added. – Deek3117 Feb 06 '16 at 14:27
  • @Deek, there was another error - you weren't closing the BufferedWriter. I've added a second answer for this. – whistling_marmot Feb 06 '16 at 14:32
0

Even when you change to using a file in the file system rather than one in the jar, it still doesn't write out to the file because you don't close the BufferedWriter.

You can explicitly close it, as you do the BufferedReader. Or better still, you can use try-with-resources.

whistling_marmot
  • 3,561
  • 3
  • 25
  • 39