Solved

Reading a file in an Applet

Posted on 1998-07-06
3
206 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
[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
  • 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

615 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