Loading files into an applet

So, I need to load some text files into my applet. This is the function I have to read a file into a string:

File file = new File(location);
        StringBuffer contents = new StringBuffer();
        BufferedReader reader = null;

        try {
            reader = new BufferedReader(new FileReader(file));
            String text = null;

            // repeat until all lines is read
            while ((text = reader.readLine()) != null) {
                contents.append(text+"\n");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

This worked fine in eclipse, but I just switched to Intellij IDEA, and now I get this:

java.io.FileNotFoundException: oryx.vcol (The system cannot find the file specified)

I think it might be because it’s an applet. How should I read a file into a string with an applet?

Is the file inside a JAR?

This code is completely irrelevant since you don’t show how you got the location :wink:

The file isn’t inside a jar. location is a String which, in this case, is “oryx.vcol” The oryx.vcol file is stored in the projectroot/assets, which is set as a source folder so that the files in assets go to the output directory.

Hmmm, creating a test case, I get an AccessDenied when using a relative path like that.

I normally never use File, I prefer just getting a direct InputStream:


BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream("oryx.vcol"),"UTF-8"));