Java – read file in an applet


Hi there I want to read out a file that lies on the server.
I get the path to the file by a parameter

<PARAM name=fileToRead value="">

when I now start the applet following error occurs

Caused by: java.lang.IllegalArgumentException: URI scheme is not "file"

Can someone give me a hint?

BufferedReader file;
                        String strFile = new String(getParameter("fileToRead"));

                        URL url = new URL(strFile);
                        URI uri = url.toURI();
                        try {

                            File theFile = new File(uri);
                            file = new BufferedReader(new FileReader(new File(uri)));

                        String input = "";

                            while ((input = file.readLine()) != null) {
                        } catch (IOException ex) {
                          Logger.getLogger(Hedgeman.class.getName()).log(Level.SEVERE, null, ex);

Best Solution

 File theFile = new File(uri);

is not the correct method. You accessing an URL, not a File.

Your code should look like this:

 URL url = new URL(strFile);
 InputStream in = url.openStream();
 (... read file...)
} catch(IOException err)
 (... process error...)