Hi,

I need help, to get the get working directory of the class file on a Windows system. I'm opening up a txt file to read from in the same folder as the class file, when I just enter the location of the txt file, it says:

java.security.AccessControlException: access denied (java.io.FilePermission txt/txtfile.txt read)

It works if I enter the full C:\ path, but I need it to work without entering the C:\. This will be used on a few different machines therefore I need it to be compatible, and locations of the folder of the class file will change often so I can't be changing the source code all that time... Please help! Thanks!

This is what I have so far:

private void LoadTXT() {
    FileReader in;
    BufferedReader Buffer;
    String out = "";
    MyTXTFile = new File("txt/txtfile.txt");
    
    if (!MyTXTFile.exists()) System.out.println("File does not exist."); 

    try {
      in = new FileReader(MyTXTFile);
      Buffer = new BufferedReader(in);
      while ((out = ReadQuestion.readLine()) != null) {
        System.out.println("" + out); 
      }
    } catch (FileNotFoundException e) {
        System.out.println("File does not exist."); 
    } catch (IOException e) {
        System.out.println("IOException Error"); 
    }  
}

Edit: Just so you know, this is run from an APPLET.

Exception shows that you are loading a file within your applet. By default applet cannot access file system. If you want to access the file system then you have to set permission using policy file & policytool.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.