8 Replies Latest reply: Mar 23, 2008 1:55 AM by 807591 RSS

    Relative filenames for FileReader?

    807591
      Hi everybody,
      This is another concept question from me...

      I have an application I'd like to be able to port to any system as a JAR, and when I click on a certain button, I'd like it to read a certain file that has a certain name, say "myFile.txt" for this example. I know that Windows, Mac, and Linux all use different directory structures (forward vs. backslash), and Java is supposed to be portable to all those, so I imagine there must be some way to tell a FileReader to use a file with a certain name on the computer, even if you don't know exactly where it is, and print an error message. Can anyone please tell me how to do that, and how I'd throw the FileNotFoundException if no file with the name was found? I may be making this too complicated; it's probably easy.

      Thanks,
      Jezzica85
        • 1. Re: Relative filenames for FileReader?
          DarrylBurke
          One common point that comes to mind is that you can return a file path from System.getProperty ("user.home") on any system. So you could place your key file in a location relative to the user's home directory.

          There's really no need to program by exception, File has a method exists () that retuns boolean.

          luck, db
          • 2. Re: Relative filenames for FileReader?
            807591
            I'm not sure what you mean by returning a file path from System.getProperty. What I'd like to do is allow a user to install the file wherever is easy for them, and then have my application be able to find it wherever it is on the system. Would System.getProperty help me do that?

            Jezzica85
            • 3. Re: Relative filenames for FileReader?
              DarrylBurke
              If the user can place the file in a folder of choice, how do you plan on your aplication locating it? JFileChooser? If so you can implement a custom FileFilter that will accept only the file of the particular name.

              db
              • 4. Re: Relative filenames for FileReader?
                DarrylBurke
                I know that Windows, Mac, and Linux all use different directory structures (forward vs. backslash),
                All accept the forward slash. If you don't believe me, open up Windows Explorer and in the address bar type c:/windows/system32 and press <Enter>

                db
                • 5. Re: Relative filenames for FileReader?
                  807591
                  Hello Jessica,

                  please, please don't go searching your users harddrive. It's a violation of their trust, it takes forever and in the end you will have to let the user chose anyways (at least if there are multiple files "matching the description").

                  With kind regards
                  Ben
                  • 6. Re: Relative filenames for FileReader?
                    807591
                    Good points everyone, and thanks; maybe it's just a better idea to just have an input dialog and ask the user to put in the file name or directory where the files are.

                    Thanks,
                    Jezzica85
                    • 7. Re: Relative filenames for FileReader?
                      DarrylBurke
                      That's what JFileChooser is meant for.

                      db
                      • 8. Re: Relative filenames for FileReader?
                        807591
                        maybe it's just a better idea to just have an input dialog and ask the user to put in the file name or directory where the files are.
                        As Darryl says a JFileChooser is the right sort of dialog. But it is useful to point them in the right direction.

                        That's why the system properties are useful. The is a "user.home" property db referred to before can be used to make the JFileChooser point at the users home directory (or some subdirectory thereof): this converts to the "~" directory (or the "My Documents" or whatever).

                        Another option is to specify the data directory as an argument to your program when it starts. (ie one of the args[] that main() gets to see). This can be very handy if there are multiple users using the application and whoever installs it wants to set up this directory is a uniform way. For instance a network manager may want to ensure that all users use some common location for the data files.