Solved

java applet using FileReader I/O

Posted on 2004-10-24
546 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
Question by:angel875
    10 Comments
     
    LVL 92

    Accepted Solution

    by:
    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
    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
    you could use a JTextArea to display the file contents to screen.
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    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
    Add the above code to the ActionListener for your button

    0
     
    LVL 92

    Expert Comment

    by:objects
    > 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
    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
    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
    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
    damn, it took too long to write my essay....
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    The viewer will learn how to implement Singleton Design Pattern in Java.

    877 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now