Solved

Reading a file in an Applet

Posted on 1998-07-06
3
196 Views
Last Modified: 2010-03-30
I have a file (settings.ini) that is located in the same directory as the applet on a server.  I have to read the first 10 lines separately (they are all specific properties that are separated by carriage returns).  What is the easiest way to read these values from the file into an array?  Example code would be muchly appreciated!

Thanks ahead of time for any help!
0
Comment
Question by:Tempest062898
  • 2
3 Comments
 

Accepted Solution

by:
Charmaine041198 earned 20 total points
ID: 1225139
Hi Tempest,

The following is a Java 1.1 working example of an applet reading the first 10 lines of a file located at the applet server. I have assumed that you'll be storing the data as Strings, so I created a String array. You'll be mainly interested in the code under the method "read()".

Hope it's helpful!!

Charmaine

*****************************
// start of program
import java.applet.Applet;
import java.io.*;
import java.net.*;
import java.awt.*;

public class ReadFile extends Applet {

    private URL yourfile;
    private BufferedReader in;

    private TextArea textarea;

    public void init() {
      setLayout(new BorderLayout());
      textarea = new TextArea("Test\n",10,40);
      add("Center",textarea);
    }

    public void start() {
      read();
    }

    /**
      * Reads first 10 lines from the specified file and stores in a String array.
      * Also displays the array elements in a text area on the applet for checking purposes.
      */
    public void read() {
      try {
          yourfile = new URL("http://yourserver/test.txt");
      } catch(MalformedURLException me) {
          System.out.println("malformed URL exception\n");
      }
      try {
          in = new BufferedReader(new InputStreamReader(myfile.openStream()));

          String inputLine;
          String array[] = new String[10];
          int i = 0;

                  // read the contents of the file into an array. Display for checking.
                  while (((inputLine = in.readLine()) != null) && (i<10)) {
            array[i] = inputLine;
            textarea.append(array[i]+"\n");
            i++;
          }
                  in.close();
      } catch(IOException ie) {
                  System.out.println("Exception thrown");
      }
    } // end read()
}
// end of program


0
 

Expert Comment

by:Charmaine041198
ID: 1225140
err...sorry about the alignment, it didn't look that way when I posted the answer.

Charmaine
0
 

Author Comment

by:Tempest062898
ID: 1225141
What about writing to the file though?
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jboss 7.1 start up error 1 61
ejb mdb examples 1 19
American Express @Work site and Java 4 64
Find out all repos that a user is most active on Github 1 29
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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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…

756 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