• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 619
  • Last Modified:

java applet using FileReader I/O

I'm currently a TA teaching a class at Lockheed Martin and loooking for a quick source code java applet which reads a information from a txt file using FileReader and displays it to the screen when the button on the applet is pushed.

anyway you have something lying around that I can use as a working example
been busting butt seaching but have found no luck and I have to have this ready tomorrow.
Your help be appreciated Thank You
0
angel875
Asked:
angel875
  • 5
  • 3
  • 2
3 Solutions
 
objectsCommented:
BufferedReader in = new BufferedReader(new FileReader(file));
String line = null;
while (null!=(line=in.readLine()))
{
   System.out.println(line);
}

to read from a local file from an applet will require the applet be signed.
0
 
CEHJCommented:
Do something like:

BufferedReader in = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/x.txt")));
String buffer = null;
while ((buffer = in.readLine()) != null) {
      System.out.println(buffer);
}
in.close();
0
 
objectsCommented:
you could use a JTextArea to display the file contents to screen.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
CEHJCommented:
If you put x.txt into the same directory as the applet class file, you won't have to sign the applet
0
 
objectsCommented:
Add the above code to the ActionListener for your button

0
 
objectsCommented:
> If you put x.txt into the same directory as the applet class file, you won't have to sign the applet

you can put it in any directory you want on the server that is visible to the applet.
you need to sign it if want to read files on the *client*.
0
 
objectsCommented:
this may help:
http://java.sun.com/docs/books/tutorial/applet/index.html

let me know if you have any questions.
0
 
CEHJCommented:
If you do need it to read files on the client, here's some info about signing the applet:

http://mindprod.com/jgloss/signedApplets.html
0
 
Tommy BraasCommented:
Hi angel875,

It would be academic dishonesty to provide you with code outright.

A FileReader can be instantiated with either a String path to a file, a File instance or a FileDescriptor. Once instantiated, it can be read from provided the file exists. If the file doesn't exist a FileNotFoundException will be thrown. See http://java.sun.com/j2se/1.4.2/docs/api/java/io/FileReader.html

To display it in an applet, you could add a TextArea to the applet and add the text you read from the file into the text area.
See http://java.sun.com/j2se/1.4.2/docs/api/java/awt/TextArea.html
and http://java.sun.com/j2se/1.4.2/docs/api/java/applet/Applet.html

In order to allow an applet to show local file content, the applet needs to be signed. An applet operates in 'sandbox' and it is not allowed to read/write files by default. Please have a look at the following page for a plethora of links http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20060040.html
or check this link out directly: http://www.suitable.com/docs/signing.html

\t
0
 
Tommy BraasCommented:
damn, it took too long to write my essay....
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now