Solved

Reading a file in an Applet

Posted on 1998-07-06
3
185 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

777 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