Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

java applet using FileReader I/O

Posted on 2004-10-24
12
Medium Priority
?
607 Views
Last Modified: 2008-02-01
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
Comment
Question by:angel875
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
12 Comments
 
LVL 92

Accepted Solution

by:
objects earned 672 total points
ID: 12395696
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
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 664 total points
ID: 12395707
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
 
LVL 92

Expert Comment

by:objects
ID: 12395708
you could use a JTextArea to display the file contents to screen.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 86

Expert Comment

by:CEHJ
ID: 12395711
If you put x.txt into the same directory as the applet class file, you won't have to sign the applet
0
 
LVL 92

Expert Comment

by:objects
ID: 12395714
Add the above code to the ActionListener for your button

0
 
LVL 92

Expert Comment

by:objects
ID: 12395719
> 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
 
LVL 92

Expert Comment

by:objects
ID: 12395720
this may help:
http://java.sun.com/docs/books/tutorial/applet/index.html

let me know if you have any questions.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12395731
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
 
LVL 14

Assisted Solution

by:Tommy Braas
Tommy Braas earned 664 total points
ID: 12395733
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
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 12395740
damn, it took too long to write my essay....
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question