Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Load text files?

Started by sushi-one Apr 30, 2004 at 2:11 PM 2 replies 2.5k views
Original Post
sushi-one
sushi-one
Why do I get the message "Applet not initialized" when I try to open a simple text file??
try {
	file = new FileReader("player.txt");
} catch( FileNotFoundException e ){
	System.out.println("Unexpected Exception: "+e.toString());
}
------------- Hans-Petter [edited by - sushi-one on April 30, 2004 3:18:13 PM]
Hans-Petter Harveg
Optus
Optus
I''m pretty sure it''s because of the Applet sandbox. You cannot load an arbitrary file from the user''s computer from an Applet. The way to have external files would be to package them in the JAR file, then use ClassLoader''s getResource* methods.
Chaoslab
Chaoslab
Optuse is correct on this one....

No problems though as you can just read the file from the server the applet is being server from....



/* following method has dependancies....
import java.net.*;
import java.io.*;
*/

/** read url
*
* @param url_loc location of url to read from
*
* @return String of url connections or null
*
*/
public static final String read_url(String url_loc) {
String result = "";

try {
URL url = new URL(url_loc);

InputStreamReader ins = new InputStreamReader(url.openStream());
BufferedReader in = new BufferedReader(ins);
String line = in.readLine();

for(;line != null; line = in.readLine())
result += line;
} catch(Exception e) {
CS.ex("Read URL exception:", e);
}

return result;
}



:-) hope that helps....

ujhkfkfk
sushi-one
sushi-one
thanx alot!! you are my savier!hehe



Hans-Petter
Hans-Petter Harveg

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.