The Artima Developer Community
Sponsored Link

Java Answers Forum
how do I read from a file and display it in a text area?

8 replies on 1 page. Most recent reply: May 5, 2002 4:39 AM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 8 replies on 1 page
Neva Mohammad

Posts: 28
Nickname: neva
Registered: Apr, 2002

how do I read from a file and display it in a text area? Posted: Apr 17, 2002 5:34 AM
Reply to this message Reply
Advertisement
Very simple question..
I am not able to do the above with BufferedReader, can someone help?
thanks


Singh M.

Posts: 154
Nickname: ms
Registered: Mar, 2002

Re: how do I read from a file and display it in a text area? Posted: Apr 17, 2002 5:45 AM
Reply to this message Reply
post your code.

Neva Mohammad

Posts: 28
Nickname: neva
Registered: Apr, 2002

Re: how do I read from a file and display it in a text area? Posted: Apr 21, 2002 11:20 PM
Reply to this message Reply
Sorry I totally forgot that I had posted that message. I have managed to do it. But I will post my code because eventhough it reads the file and displays it in the text area it does not do so line by line...

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.lang.*;
import java.util.*;
 
public class UploadFiles2 extends Frame implements ActionListener{
      Button browse, upload;
      Label enter;
      TextField name, name2;
      TextArea tt;
      FlowLayout gl = new FlowLayout();
      Frame win;
      String lastDir = "";
      String lastDirk = "F";
      String s /*= "../../gif/"*/;
      String t /*= "gif/"*/;
      BufferedReader in;
      FileDialog d, k;
      StringTokenizer tokenizer;
 
    UploadFiles2(){
      setLayout(gl);
      enter = new Label("Enter name of file to upload, or click Browse to find file");
      name = new TextField(40);
      browse = new Button("Browse");
      upload = new Button("Upload File");
      tt = new TextArea();
      name2 = new TextField(40);
      add(enter);
      add(name);
      add(browse);
      add(upload);
      add(tt);
      add(name2);
      browse.addActionListener(this);
      upload.addActionListener(this);
    }
 
    public void actionPerformed(ActionEvent e){
    win = new Err1();
    String line;
      if (e.getSource() == browse){
        	d = new FileDialog(this, "Select File", FileDialog.LOAD);
			    d.setFile("*.*");
			    d.setDirectory(lastDir);
          d.show();
          String f = d.getFile();
          lastDir = d.getDirectory();
			    if (f!=null && lastDir!=null){
            name.setText(d.getDirectory() + d.getFile());
          }else win.show();
    } else if (e.getSource() == upload){
     try{//read file from local drive.
 
        BufferedReader in = new BufferedReader(new FileReader(d.getDirectory() + d.getFile()));
        //line = in.readLine();
           while ((line=in.readLine())!= null) {
              tokenizer = new StringTokenizer(line);
              tt.append(line);
            // save this in the archive folder on the server.
 
            /*make the changes in the file, save the file as newsletter.html
            in its correct location need to know how to give the correct location*/
           }in.close(); 
        } catch (IOException ioe){
            tt.setText(ioe.toString());
          }
       }
}
 
public static void main(String[] args){
	UploadFiles2 up2 = new UploadFiles2();
    WindowListener wl = new WindowAdapter(){
    public void windowClosing(WindowEvent e){
      ((Window) e.getSource()).dispose();
      System.exit(0);
      }
    };
  up2.addWindowListener(wl);
	up2.setSize(500, 400);
	up2.show();
}
}


Thanks

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: how do I read from a file and display it in a text area? Posted: Apr 22, 2002 3:29 AM
Reply to this message Reply
Where is the definition of the class Err1 ?
... // snipped
public void actionPerformed(ActionEvent e)
{    
  win = new Err1();
  ...
... // snipped


Thomas,

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: how do I read from a file and display it in a text area? Posted: Apr 22, 2002 8:19 AM
Reply to this message Reply
change the line:

tt.append(line);

to

tt.append(line + "\n");

Neva Mohammad

Posts: 28
Nickname: neva
Registered: Apr, 2002

Re: how do I read from a file and display it in a text area? Posted: Apr 24, 2002 1:04 AM
Reply to this message Reply
thanks.. I will give it a try..
sorry I did not post Err1.java..it is just a plain applaication that outlines the error.

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: how do I read from a file and display it in a text area? Posted: Apr 24, 2002 5:54 AM
Reply to this message Reply
Without knowing if Charles's solution is fine, this should help you getting a more generic solution :
package test.lang;
 
import java.util.Enumeration;
import java.util.Properties;
 
 
public class SysProps
{
	public static void main (String[] args)
	{
		Properties props = System.getProperties();
		props.list(System.out);
	}	
}


Please looke a the "line.separator" property

Cheers,

Thomas,

Neva Mohammad

Posts: 28
Nickname: neva
Registered: Apr, 2002

Re: how do I read from a file and display it in a text area? Posted: May 5, 2002 4:33 AM
Reply to this message Reply
Thanks, I'll give that a try. FYI Charles's solution worked fine....

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: how do I read from a file and display it in a text area? Posted: May 5, 2002 4:39 AM
Reply to this message Reply
Thanks for your encouragement. I really needed that this morning.

Charles

Flat View: This topic has 8 replies on 1 page
Topic: How can I print hml-files with java? URGENT Previous Topic   Next Topic Topic: How to run an Applet?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use