July 10, 2009 at 3:22 PM
Hi Friend,
Try the following code:
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
class Form extends JFrame{
JButton ADD,RETRIEVE;
JPanel panel,pan;
JLabel label1,label2,label3,label4,label5;
final JTextField text1,text2,text3,text4,text5;
Form() {
label1 = new JLabel();
label1.setText("Employee Id:");
text1 = new JTextField(20);
label2 = new JLabel();
label2.setText("Employee Name:");
text2 = new JTextField(20);
label3 = new JLabel();
label3.setText("Employee Designation:");
text3 = new JTextField(20);
label4 = new JLabel();
label4.setText("Employee Salary:");
text4 = new JTextField(20);
label5 = new JLabel();
label5.setText("Employee Address:");
text5 = new JTextField(20);
ADD=new JButton("ADD");
RETRIEVE=new JButton("RETRIEVE");
panel=new JPanel(new GridLayout(6,2));
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(label3);
panel.add(text3);
panel.add(label4);
panel.add(text4);
panel.add(label5);
panel.add(text5);
panel.add(ADD);
panel.add(RETRIEVE);
add(panel,BorderLayout.CENTER);
setTitle("FORM");
ADD.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
String id[]={text1.getText()};
String name[]={text2.getText()};
String des[]={text3.getText()};
String sal[]={text4.getText()};
String add[]={text5.getText()};
java.util.List<String> list = new ArrayList<String>();
for(int i = 0; i < id.length; i++) {
String line = id[i]+" "+name[i]+" "+des[i]+" "+sal[i]+" "+add[i];
list.add(line);
}
writeToFile(list, "Employee.txt");
}
});
RETRIEVE.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
File file=new File("Employee.txt");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
String data="";
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
while (dis.available() != 0) {
data+=dis.readLine()+"\n";
}
System.out.println(data);
JTextArea area =new JTextArea(10,25);
JScrollPane sPane = new JScrollPane(area);
area.setText(data);
pan=new JPanel();
pan.add(sPane);
JFrame frame=new JFrame();
frame.add(pan);
frame.setSize(300,100);
frame.setVisible(true);
fis.close();
bis.close();
dis.close();
}
catch(Exception e){}
}
});
}
private static void writeToFile(java.util.List<String> list, String path) {
BufferedWriter out = null;
try {
File file = new File(path);
out = new BufferedWriter(new FileWriter(file,true));
for (String s : list) {
out.write(s);
out.newLine();
}
out.close();
} catch(IOException e) {}
}
}
class InsertToFile{
public static void main(String arg[]) {
try
{
Form frame=new Form();
frame.setSize(300,300);
frame.setVisible(true);
}
catch(Exception e)
{}
}
}
Hope that it will be helpful for you.
Thanks
Related Tutorials/Questions & Answers:
how to store data in a text file - Java Beginnershow to
store data in a
text file Hi friends,
I want to know,
how we can save the
data in a .txt
file using swings....... for example, i want to
store the arraylist
data of swing program into a
text file.......and also i want
read text file and store the data in mysql - JDBCread
text file and
store the
data in mysql when we
store the
data in mysql table from
text file its
store the
data from new line to new column.
how to
store the
data in different column from a single line of
text file.
Advertisements
how to store data in XML file - JSP-Servlethow to
store data in XML file hi
i have to
store the
data for example user id and password in a xml
file the input userid and password will be coming from jsp middle ware servlet
how to do that? Hi friend
how to update the text file?how to update the
text file? if my
text file contains a string and integer in each line say,:
aaa 200
bbb 500
ccc 400
i need a java code to update the integer value if my input String matches with the string in
file.
please
how to update the text file?how to update the
text file? my textfile with name list.txt:
Rice... i want to update the quantity in my
text file, if the item i entered matches...=new float[n1];
for(int i=0;i
File file ;
FileReader fr=null
Question about "Insert text file data into Database"Question about "Insert
text file data into Database" Hey
I was reading the tutorial "Insert
text file data into Database", (awesome btw), and noticed that both a FileInputStream, a DataInputStream and a BufferedReader
How to store url path in file ?How to
store url path in
file ? Hi,
How to
store url path in
file... = robot.createScreenCapture(new Rectangle(1000,700));
String
file = "C:/xampp...";
This is
store phiscal directory but i want
store url path like
Insert text file data into DatabaseInsert
text file data into Database
In this section, you will learn
how to insert the
text file data into the database. For this purpose, we have created a 'student.txt'
file consisting of
students information i.e, id, name, course
create login page using data from text filecreate login page using
data from
text file I want to create login page using
data store in textfile(
data submit from regiter page to textfile) using jsp and servlet. Thanks
how to store/retrieve doc file - Java Beginnershow to
store/retrieve doc file i want to wirte a code that stores... with the code? Use this stuff inside your jsp page for
store file inside database, for follwing same concept for retrive
data
Thanks
Use Map to display file data into textfields. You have mostly
used ArrayList or Vector classes to
store the
file data. In this section, you will learn
how to
store the
file data into HashMap in the form...Use Map to display
file data into textfields
Collection framework allows
Write records into text file from databaseWrite records into
text file from database
You have already learnt
how to insert records from
text file to database. But here we are going to retrieve records from database and
store the
data into the
text file. For this purpose, we have
how to read a text file with scanner in javahow to read a
text file with scanner in java Hi,
I am looking for the example code in Java for reading
text file line by line using the Scanner class.
how to read a
text file with scanner in java?
Thanks
Hi
Java insert file data to JTableJava insert
file data to JTable
In this section, you will learn
how to insert
text file data into JTable.
Swing has provide useful and sophisticated set... and
store the
data into vector. Then pass the vector value as a parameter to
the
How to write file text In New LineHow to write
file text In New Line Hi friends,
How to write
file text in new line in Java program.
How to write the syntax for this with example... you went new
text in new line. For this we created a new
text file named
How to Convert Text Files into Gzip FileHow to Convert
Text Files into Gzip File Hi,
I am developing a small application using PHP language. The real problem is that
how do i convert the
text files into gzip
file.
how can i solve the problem....
Tnanks
How to read text file in Servlets
How to read
text file in Servlets
This section illustrates you
how to read
text file in servlets.
In this example we will use the input stream to read the
text how to match the key word from a text filehow to match the key word from a
text file p>Hi all,
I have the code to match the key word and from the
text. I have input like this reader.txt...();
System.out.println("
Data of Class2(keyWrd): " + data2);
try {
BufferedReader br
how to read text file in jtable in netbeans7.0how to read
text file in jtable in netbeans7.0
text file... want to displaythe above .txt
file in jtable as following format having 3 columns
contigID length size
and then display sequence like "ATGCGSA..." in
text How to format text file? - Java BeginnersHow to format
text file? I want the answer for following Query
***(Java code)
How to open,read and format
text file(notepad) or MS-word... String getContents(
File aFile) {
StringBuilder contents = new StringBuilder
how to convert text file to xml file in java. - XMLhow to convert
text file to xml
file in java. Hi all,
I m having some problem. Problem is I want to convert a
text file which is having the no of record(i.e no of different line of information)to a xml
file through java
text filetext file Hello can I modify the program below so that all the numerical
data is stored in an external
text file,that is the
data contained in the array list.Thank you!
mport java.util.*;
import java.text.*;
import
How to read a large text file line by line in java?How to read a large
text file line by line in java? I have been assigned a work to read big
text file and extract the
data and save into database... was using the
text file for saving the
data through an application developed in C
text filetext file Hi can you help me I have to modify the program below so that all the
data held in it is stored in an external
text file.So there should... at the start of the program from a seperate external
text file.Thank you!
mport
Read text file in PySparkRead
text file in PySpark -
How to read a
text file in PySpark?
The PySpark...
text file and then collect the
data into RDD.
The term RDD stands.../spark-2.3.0-bin-hadoop2.7/bin$
In this tutorial we have learned
how to read a
text file