|
Re: how do I read from a file and display it in a text area?
|
Posted: Apr 21, 2002 11:20 PM
|
|
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
|
|