Solved

input / output problem

Posted on 2004-04-07
3
315 Views
Last Modified: 2010-05-18
Need help displaying (when programme starts) the two txt files into myTxtBox.  

User then selects file name from myTxtBox - can't get this to work either.

Need help creating the button which - when pressed - loads the selected txt file into myBox.  

Thanks

Code so far:-

//<applet code=Lou.class width=400 height=200></applet>
import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;

 
public class Lou extends Applet implements ActionListener{

Label myLabel;
TextArea myBox;
TextField myTxtbox; //Added new textbox onto applet into which file name can be typed for 23a.
Button mybtn;


public void init() {

      try {
        
         myLabel= new Label("The text area shows the information read from the file on the server");
         myBox = new TextArea("", 4,20);
         myTxtbox = new TextField(20);
          mybtn = new Button ("Load File into Text Area");
         add(myLabel);
          add(myBox);
         add(myTxtbox);
         add(mybtn);
         mybtn.addActionListener(this);

       URL myURL = new URL(getDocumentBase(),"readthis.txt");

                   // open file on server
                   URLConnection myConn = myURL.openConnection();  
                   InputStreamReader isr = new InputStreamReader(myConn.getInputStream());
                   BufferedReader bufr = new BufferedReader(isr);

                   String line;
                   line = bufr.readLine(); // read ahead  
                  
                   while (line != null) {
                   myBox.append(line + "\n");
                   line = bufr.readLine();  

                         } // end of while

      } // end of try


      catch (MalformedURLException me) {
         System.out.println("Bad URL encountered" + me );
       }


      catch (IOException ioe){
         System.out.println("IO Exception" + ioe);
      }


      }  // end of init()


public void actionPerformed(ActionEvent e) {



}//end action performed


} // end of Applet

0
Comment
Question by:buglouie
  • 2
3 Comments
 
LVL 14

Accepted Solution

by:
Tommy Braas earned 500 total points
Comment Utility
Try this;

import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
public class Lou extends Applet implements ActionListener {
      Label myLabel;
      TextArea myBox;
      TextField myTxtbox; //Added new textbox onto applet into which file name can be typed for 23a.
      Button mybtn;
      public void init() {
            myLabel = new Label(
            "The text area shows the information read from the file on the server");
            myBox = new TextArea("", 4, 20);
            myTxtbox = new TextField(20);
            myTxtbox.setText("readthis.txt");
            mybtn = new Button("Load File into Text Area");
            add(myLabel);
            add(myBox);
            add(myTxtbox);
            add(mybtn);
            mybtn.addActionListener(this);
      } // end of init()
      public void actionPerformed(ActionEvent e) {
            loadFile();
      }//end action performed
      public void loadFile() {
            try {
                  URL myURL = new URL(getDocumentBase(), myTxtbox.getText());
                  // open file on server
                  URLConnection myConn = myURL.openConnection();
                  InputStreamReader isr = new InputStreamReader(myConn
                              .getInputStream());
                  BufferedReader bufr = new BufferedReader(isr);
                  String line;
                  line = bufr.readLine(); // read ahead  
                  while (line != null) {
                        myBox.append(line + "\n");
                        line = bufr.readLine();
                  } // end of while            
            } // end of try
            catch (MalformedURLException me) {
                  System.out.println("Bad URL encountered" + me);
            } catch (IOException ioe) {
                  System.out.println("IO Exception" + ioe);
            }
      }
} // end of Applet
0
 

Author Comment

by:buglouie
Comment Utility
Absolute Star!
0
 
LVL 14

Expert Comment

by:Tommy Braas
Comment Utility
:-)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countHi2 challenge 7 43
mapShare challenge 13 67
Starting to learn JAVA, 7 47
Unexpected HttpURLConnection connection behavior 2 46
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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…

771 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

8 Experts available now in Live!

Get 1:1 Help Now