Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

saving text area to a file

 
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
 
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
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I want too. Thank you, netharam.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic