Question

I'm using the acm libraries for my Java program, and I want to embed my program into my website via HTML. I have other .jar files embedded just fine in my website, by using the

<applet archive="file.jar, acm.jar"
code="main.class"
width=400 height=600 />

but have found that when embedded in HTML the program sort of freaks out and stops responding when it gets to the part where it should load the .txt file.

I remember vaguely my AP CompSci teacher telling us that java in web browsers blocked the import of .txt files, but I might be remembering incorrectly. Here is my java code below:

public NameSurferDataBase(String filename) {
    nameEntry = new HashMap<String, NameSurferEntry>();
    try {
        BufferedReader rd = new BufferedReader(new FileReader(filename));
        while (true) {
            String line = rd.readLine();
            if (line == null) break;
            NameSurferEntry entry = new NameSurferEntry(line);
            nameEntry.put(entry.getName().toUpperCase(), entry);                
        }
    } catch (IOException ex) {
        throw new ErrorException(ex);
    }
}

So not only do I not know how to actually add the .txt file as something to use before it runs, I don't even know if it is possible.

Était-ce utile?

La solution

It's because when running applets, the security manager doesn't let you work with the filesystem (unless you specifically change the plugin settings which is a bad idea). If you're just trying to read, put the file in your classpath, and use ClassLoader.getResourceAsStream(String resource) to get the input stream instead.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top