• Post Reply Bookmark Topic Watch Topic
  • New Topic

saving text area to a file  RSS feed

 
Mukesh Jain
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sir
Can you please tell me how to save the data or text of a TextArea to a file
It's urgent please
Mukesh
 
netharam ram
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is can be done using streams.Use FileOutputStream to open a file and write the content of the TextArea to that file.
*************** code: ******************
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class TArea_File extends Frame implements ActionListener
{
Button b;
TextArea t;
public TArea_File()
{
setSize(200,200);
setVisible(true);
t=new TextArea("",10,30);
b=new Button("save_To_File");
setLayout(new FlowLayout());
add(t);
add(b);
b.addActionListener(this);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}//end of constructor
public void actionPerformed(ActionEvent ae)
{
try
{
String s=t.getText();
if(s.length()>0)
{
FileDialog fd= new FileDialog(this,"Save File As",FileDialog.SAVE);
fd.setFile("temp.txt");
fd.setDirectory("c:\\windows\\temp");
fd.setVisible(true);
String path=fd.getDirectory()+fd.getFile();

FileOutputStream fos=new FileOutputStream(path);
System.out.println(s);
byte[] b=s.getBytes();
fos.write(b);
fos.close();
}
}catch(Exception e){System.out.println(e);}
}
public static void main(String args[])
{
new TArea_File();
}
}
*************** end of code ******************
Good Luck.
Netharam
 
Renee Zhang
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I want too. Thank you, netharam.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!