-1

Edit: I have figured out a solution to this problem and it is very similar to was MS90 commented. Since my working directory is "ATM project", then java will only be able to notice relative path's I give it within that directory. Changing my path name to "group_0331\phase1\deposits.txt" did the trick as "group_0331" is in my current directory. The file is now created where I want it, under phase1.

I need to be able to write this file (and read from) to the same directory as the java file that has the createAccount method. So not the working directory. Also, I can't hard code the path name because this file will not always be run from the same computer. How can I do this?

void createAccount(String name, String accountType){
    try {
        accounts = new File("accounts.txt");
        Writer writer = new BufferedWriter(new FileWriter(accounts, true));
        writer.write(String.format("%s \naccounts: %s", name, accounts));
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Jonathan
  • 247
  • 3
  • 11
  • How do you propose to pass the file path to the program? Via what input? Is the program a GUI that interacts with the user? Or are you planning on getting the path via command line parameters on calling the program? – Hovercraft Full Of Eels Mar 02 '19 at 16:01
  • The program is an Atm machine. Whenever, the bank manager logs on to the atm machine to create a new account for a user; this method will be called and all the accounts should be written to "accounts.txt". I don't know if this answers your question. – Jonathan Mar 02 '19 at 16:04
  • No it does not. To re-ask: Where is accounts.txt going to be located? And how do you propose to pass this information to the program? – Hovercraft Full Of Eels Mar 02 '19 at 16:05
  • I have a folder called phase1 that contains all the java files for the project. I would like "accounts.txt" to be created there. – Jonathan Mar 02 '19 at 16:07
  • As Great Magician pointed out are you planning on getting the path via command line or GUI? – MS90 Mar 02 '19 at 16:08
  • But where is this folder in relation to the program? Do you know where Java is looking, where Java has your user's working directory? Have you researched similar questions on this site? You're not giving enough information still. – Hovercraft Full Of Eels Mar 02 '19 at 16:08
  • This is the absolute path of the directory I want my file to be placed in: "C:\Users\jonny\IdeaProjects\ATM project\group_0331\phase1". ATM project is the folder containing the project. Phase 1 is the directory I would like my file to be placed in. – Jonathan Mar 02 '19 at 16:12
  • And where is this relative to the user's working directory? – Hovercraft Full Of Eels Mar 02 '19 at 16:14
  • ATMproject is the working directory – Jonathan Mar 02 '19 at 16:15
  • Try something like new File("/phase1/accounts"); where phase1 is a folder within your project. – MS90 Mar 02 '19 at 16:17
  • I get FileNotFound exception when I do this. – Jonathan Mar 02 '19 at 16:24
  • Then you still don't know where Java is looking. Again the solution is to ask Java where the working directory is (see link to duplicate) and use that to figure out the relative path – Hovercraft Full Of Eels Mar 02 '19 at 16:40
  • See my edit above. – Jonathan Mar 02 '19 at 16:41

1 Answers1

-2

What you ask is not possible: The 'java' file is not present when your program is run; java compiles java files to class files and then runs those.

If you want to load accounts.txt from the same location as the class file, use this:

public class ExampleClass {
    // catching an exception and calling printStackTrace is bad code. Do not do this.
    void example() throws IOException {
        try (InputStream in = ClassYouAreIn.class.getResourceAsStream("accounts.txt")) {
            // you can read it here.
        }
    }
}

You can't write to it; class files are usually in places that you cannot write to (for example, jar files).

The notion 'I wish to write files in the same location as my class files' is a very bad idea. If you must write files, pick a sensible location for the file to exist, and use that. Some examples:

  • A subdirectory in the user's home dir.
  • A directory specified via parameter.
  • A hardcoded path.
rzwitserloot
  • 85,357
  • 5
  • 51
  • 72