how to store data in a text file

how to store data in a text file

View Answers

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 Beginners
how 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 - JDBC
read 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-Servlet
how 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 store data entered by User in JSP page in XML file
How to store data entered by User in JSP page in XML file  How to store data entered by user in JSP page to be saved in XML file.On clicking submit...   JSP store data entered by user into XML file 1)form.jsp: <html>
How to read the data in text file seperated by by ',' in java using IO Operations
How to read the data in text file seperated by by ',' in java using IO Operations  in Text file data like raju 45,56,67 ramu 46,65,78 raji 34,23,56 this is the student marks in text file.this data read and calculate
how to change a column data in a every row in a text file in java
how to change a column data in a every row in a text file in java   i have text file like this 11,6,13/9/14,1287605778,89... column in every row ie. account number to some text in a text file in java could
How to store data entered in JSP page by a user in XML file & later retrieval of data using id at other page
How to store data entered in JSP page by a user in XML file & later retrieval..., projectname and emp name.I want to store data related to employee in xml file.How can i store data entered by user in XML file and later retrieve data
Importing data into sql plus from a text file...
Importing data into sql plus from a text file...  How to import a text file into oracle 10g enterprise edition directly to create tables and data
Importing data into sql plus from a text file...
Importing data into sql plus from a text file...  How to import a text file into oracle 10g enterprise edition directly to create tables and data
how to store,retrieve,modify the data
how to store,retrieve,modify the data  hello sir ,how to store,retrieve,modify the data using the swing please help me
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 Database
Insert 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
convert data from pdf to text file - Java Beginners
convert data from pdf to text file   how to read the data from pdf file and put it into text file(.txt
how to convert image file to text file?
how to convert image file to text file?  hi, can anybody tell how to convert image file to text file? plz help me
create login page using data from text file
create 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 Beginners
how 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
how can i store text box values as it is in database table
how can i store text box values as it is in database table  CUSTOMER DESCRIPTION
How I Upload File and Store that file name in Database using JSP
How I Upload File and Store that file name in Database using JSP  Hi All, I m the beginner in JSP and I want to upload the file and store that file and some other form data in MySQL database. Ex. There is one employee detail
Read specific column data from text file in java
Read specific column data from text file in java  My question is if my text file contain 15 columns and i want read specific column data from that text file then what code i should do
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 database
Write 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 append text to an existing file in Java
How to append text to an existing file in Java  How to append text to an existing file in Java
how to read a text file with scanner in java
how 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 JTable
Java 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 Line
How 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 File
How 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
upload the text file through html and insert those data into the table
upload the text file through html and insert those data into the table ... into table.now i want to upload the text file through html and insert those data... the code to insert data from text file   1)page.jsp: <HTML> <
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 file
how 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.0
how 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 sort the text file and display it in jtable
how to sort the text file and display it in jtable   my text file is: contig00001 length=586 numreads=4 CGGGAAATTATCcGCGCCTTCACCGCCGCCGGTTCCACCGACGAACGGATACTGCGtGaa
HOW TO STORE MULTIPLE EMPLOYEE DETAILS TO XML FILE USING JSP?
HOW TO STORE MULTIPLE EMPLOYEE DETAILS TO XML FILE USING JSP?  HELLO SIR, CAN ANYONE HELP ME OUT HOW TO STORE MULTIPLE EMPLOYEE DETAILS TO XML... HELP ME OUT WITH HOW TO VIEW THE STORED DATA IN XML FILE USING EMPNAME. PLEASE
how to store data in other table using servlet and jsp
how to store data in other table using servlet and jsp  pls can anyone tell how to store data in other table using servlet and jsp and want to display that data too.and the data in first table must be same.pls help
How to format text file? - Java Beginners
How 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. - XML
how 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 file
text 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 get data from DB in to Text box by using Jsp & Ajax
How to get data from DB in to Text box by using Jsp & Ajax   I want to get the data from database in to text box in a jsp page by using Ajax. If I... with a and from that i need to select the required value and i should store
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
How to read excel data and store it in database - Java Beginners
How to read excel data and store it in database  Hi, I want a java code to read data from excel and store it in Ms Access database.I tried the code but but its printing the output in console.I dont know how to store the excel
text file
text 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
How to delete excel file records using Store Procedure?
How to delete excel file records using Store Procedure?  Hi.. I have created one Excel file through stored procedure.Now I want to delete records or delete excel file. Thanks
Read text file in PySpark
Read 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
iPad App to store data
iPad App to store data  Hi, How to add data support iPad application? Thanks
how to store JTree data hierarchically in mysql database from netbeans
how to store JTree data hierarchically in mysql database from netbeans  how to store JTree data hierarchically in mysql database from netbeans. I am new to this topics so I need a program and tables you are using in database
servlet program for data store in oracle?
servlet program for data store in oracle?  how to store data in oracle through servlet program
How to store two integer value in a separate file and retrieve those values
How to store two integer value in a separate file and retrieve those... with idea of saving those values in a separate file and updating values in file. But Don't have any idea how can i do that. Like what format of file i need, or how
How can store image in server folder when deployed with a .war file?
How can store image in server folder when deployed with a .war file?  Hi all; When I am using application.getRealPath() , it return null when deployed with a .war file and when I use hard code it shows syntax error or path

Ads