wood burning stoves*
The moose likes Applets and the fly likes Use plain text files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "Use plain text files" Watch "Use plain text files" New topic
Author

Use plain text files

Kim Mirtens
Greenhorn

Joined: Dec 17, 2006
Posts: 21
I want to convert my command line app to an applet. My command line app uses plain text files to load some data that program needs. When I implement this class in an applet it can't read the file (eventhough it's in the same directory). I use this principle to load files:

try{
BufferedReader reader = new BufferedReader(new FileReader(new File("text.txt"));
String line = null;
while((line=reader.readLine()) != null){
System.out.println(line);
}
reader.close();
} catch(Exception ex) {
System.out.println("Cannot read file");
}

What do I need to change?
And are there some visual component which can display my System.out.println("")? I mainly want to make it into an applet to make distribution easier, but the output of the program can still come in some sort of text window (which one?).
[ January 07, 2007: Message edited by: Tim Pintens ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41114
    
  45
As a security precaution, applets are not allowed to perform file I/O. There are two principal ways around that: digitally signing the applet, and aletring the local security policy. Both are outlined here.

The output of System.out goes to the Java Console, which you can open either through a menu in the browser, or through the little Java icon in the Windows tray. On OS X , you'd use Console.app.

But if the program has only text input and output, why make it an applet? Maybe a WebStart application would be more appropriate?


Ping & DNS - my free Android networking tools app
Kim Mirtens
Greenhorn

Joined: Dec 17, 2006
Posts: 21
I think I didn't make myself clear enough: the file it has to load recides on the host. I do not want access to the local filesystem of the user. The program accually uses a textfile as a small database to get data.
For example if I would like to make an applet which gives back the capital of the country when a country is selected out of a list. Can't I just use a textfile which holds comma-separated text to find the capital.

Example of what the textfile would hold:
France,Paris
Germany,Berlin
UK,London

I do not want to put this data directly into the program, because I want it to be dynamically i.e. I want to be able to at capitals without changing the program, but only changing the textfile.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41114
    
  45
That makes more sense. You can use HTTP to retrieve the file. It could be as simple as



At which point you can use String methods to parse the file contents into its various parts.
[ January 07, 2007: Message edited by: Ulf Dittmer ]
 
wood burning stoves
 
subject: Use plain text files
 
Similar Threads
Help for getting output of Runtime.getRuntime().exec()
Working with file and directories..
BufferedReader in applet fails to read bigger file
Passing values from method1 to method2
problem in rediredtin GUI to IO Streams