• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

saving text area to a file

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what I want too. Thank you, netharam.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic