|
-
Dec 3rd, 2000, 03:50 PM
#1
Thread Starter
Addicted Member
Hi!, Can someone please explain to me how i can write the contains of a TextField into a file...
I tried this, but is not working...
try
{
output_stream =new DataOutputStream(new DataOutputStream
(new BufferedOutputStream
(new FileOutputStream("c:\\data.txt"))));
Data=textfield.getText();
output_stream.writeChars(Data);
output_stream.close();
}//try
catch(IOException e){}
I checked the file and its empty 
Thanks in advance!
-
Dec 3rd, 2000, 09:58 PM
#2
When do you attempt the try block?
When do you attempt the try block? Also, a second new DataOuputStream was excessive although it works with the redundant one which I omitted here. I'm attempting the try block after I hit the Ok button. Run this code, put text in the textfield, hit the Ok button, then check the file. You can also close the app/window with the X.
I'm sure there are better ways to write strings.
//Test.java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Test extends Frame{
public Test(){
final TextField textfield = new TextField();
add(textfield);
Button b = new Button("Ok");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
try
{
DataOutputStream output_stream =new DataOutputStream(new BufferedOutputStream
(new FileOutputStream("c:\\data.txt")));
String Data=textfield.getText();
output_stream.writeChars(Data);
output_stream.close();
}//try
catch(IOException e){}
System.out.println("Pressed Ok Button");
}
});
add(b, BorderLayout.SOUTH);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.out.println("Pressed X");
System.exit(0);
}
});
pack();
show();
}
public static void main(String[] args){
new Test();
}
}
-
Dec 3rd, 2000, 10:07 PM
#3
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|