Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Reading a file in an Applet

Posted on 1998-07-06
3
Medium Priority
?
213 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 40 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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…
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…
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 learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses

670 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