Solved

Reading a file in an Applet

Posted on 1998-07-06
3
169 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
Comment Utility
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
Comment Utility
err...sorry about the alignment, it didn't look that way when I posted the answer.

Charmaine
0
 

Author Comment

by:Tempest062898
Comment Utility
What about writing to the file though?
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

772 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

9 Experts available now in Live!

Get 1:1 Help Now