1

I'm reading in a file, the problem is that I can read anything in my own directory, and anything in any sub-directories, however when I use .getPath() It gives me the full path from route e.g. C:\Users\smandre\Documents\file.txt. How can I use this path to read in a file from my program? I've also tried `.getCanonicalPath()

int returnVal = c.showOpenDialog(c);

        if(returnVal == JFileChooser.APPROVE_OPTION) {
            setFilePath(c.getSelectedFile().getPath());
                try {
                br = new BufferedReader(new FileReader(getFilePath()));
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            System.out.println("You chose to open this file: " + c.getSelectedFile().getPath());

            // System.out.println(ClassLoader.getSystemResourceAsStream(getFilePath()));
            return;
        }

Obviously the above is trying to open the file path starting from my system route, not from my current directory...

drizzy
  • 139
  • 2
  • 15
  • Are you looking for `ClassLoader.getResourceAsStream()`? That will load your files relative to your _classpath_ instead of the root of the filesystem, or your current working directory. – Colin M Jul 18 '13 at 13:24
  • your own directory is your project's base? show us the code you are using – fGo Jul 18 '13 at 13:24
  • Related: [How to get the path of a running jar file?](http://stackoverflow.com/q/320542/335858). – Sergey Kalinichenko Jul 18 '13 at 13:25
  • 1
    What exactly are you asking? From "How can I use this path..." it sounds like you've **got** the path to a file which you wish to read, but this doesn't really conform to the title. Please provide a more complete explanation, and preferably an [SSCCE](http://sscce.org/). – Bernhard Barker Jul 18 '13 at 13:30
  • saw your edit. Still not clear what is exactly your problem? Wrong file is opened? Some error occurs? – yair Jul 18 '13 at 13:37
  • When you open a file in Java, you must specify the entire path from the root of the file system. You don't need access to those folders to use them in a file path. When you construct the FileReader on the full path, it should work correctly. If it does not, please specify the error you receive. – AgilePro Jul 18 '13 at 14:12

0 Answers0