how to add two object in a particular file

how to add two object in a particular file

View Answers

July 22, 2009 at 10:55 AM

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>();
list.add("ID"+" Name"+" Designation"+" Salary"+" Address");

java.util.List<String> list1 = new ArrayList<String>();
for(int i = 0; i < id.length; i++) {
String line = id[i]+" "+name[i]+" "+des[i]+" "+sal[i]+" "+add[i];
list1.add(line);

java.util.List<String> list2 = new ArrayList<String>();
list2.addAll(list);
list2.addAll(list1);
BufferedWriter out = null;
try {
File file = new File("Employee.txt");
out = new BufferedWriter(new FileWriter(file,true));
for (String s : list2) {
out.write(s);
out.newLine();
}
out.close();
} catch(IOException e) {}
}
}
});
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 Inventory{
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 add two object in a particular file - Java Beginners
how to add two object in a particular file  Hi frend.. I have two arraylist object in which there is some data..............now i want to add these two objects in a particular file using file handling in java....and also
how to add the two tables in same row when generating pdf file from jsp - JSP-Servlet
how to add the two tables in same row when generating pdf file from jsp  How to add the two tables in same row one is left side and other is right side please help me.   Hi Friend, Try the following code
Advertisements
How to add two numbers in Java?
How to add two numbers in a Java program? In Java you can easily add two number by using the plus (+) operator. The plus operator will add two... this will print: Sum is: 30 So, in this program you learned how to sum(add) two numbers
How to add radio button value in a table for particular field?
How to add radio button value in a table for particular field?  Hi,I... dynamically from other table and two radio button one for present and the second...;form name="myForm" method="post" action="add_attendance_sheet.jsp" >
how to add a file in GZIP
how to add a file in GZIP  Hi, how to add a file in GZIP using Java.   Hi, I have found a good referral site for How to Add a file in GZIP file format using Java. http://www.roseindia.net/java/examples/io
how to dynamically add text boxes with add and remove link based on drop down choices, if a particular choice is selected.
how to dynamically add text boxes with add and remove link based on drop down choices, if a particular choice is selected.  how to dynamically add text boxes with add and remove link based on drop down choices, if a particular
file share to particular user
file share to particular user  hi i am doing my project in jsp-servlet.i am able to upload successfully any file to oracle database. i want to share a file to specific user , how to write code plzzz help. thank you in advance
How to add two calendars on the same html page
How to add two calendars on the same html page  I have used the same... use single calendar for a single html page.but while implementing two calendars.... Please someone help me with this..   Do changes in calendar.hmtl file
Count lines of a particular file
In this program you will learn how to read a file and count number of lines in it. This is the simple program that opens a file and read one line at a time and count no of lines in a file. Program first asks the user to enter
How to read text file to two different name array
How to read text file to two different name array   I have those numbers:12,4,9,5 numbers:19,12,1,1 how to put it in two different name array in text file to java
How to read text file to two different name array
How to read text file to two different name array   I have those numbers:12,4,9,5 numbers:19,12,1,1 how to put it in two different name array in text file to java
Compare two word file
Compare two word file  How to compare two word file using Java
Add two number in java
Add two number in java In this section you will learn about how to add two... be added, So two add,  convert these String into numeric type..."); System.out.println("Please Enter two number to add :"); Scanner sin=new
reading a csv file from a particular row
reading a csv file from a particular row  how to read a csv file from a particular row and storing data to sql server by using jsp servlet
how to add new column before two column in sql.
how to add new column before two column in sql.  how to add new column in table before two column in sql.   Syntax of Alter table: ALTER... named 'employee' having three fields id, name and salary. Now we want to add two
how to add new column before two column in sql.
how to add new column before two column in sql.  how to add new column in table before two column in sql.   Syntax of Alter table: ALTER... named 'employee' having three fields id, name and salary. Now we want to add two
how to add new column before two column in sql.
how to add new column before two column in sql.  how to add new column in table before two column in sql.   Syntax of Alter table: ALTER... named 'employee' having three fields id, name and salary. Now we want to add two
how to add new column before two column in sql.
how to add new column before two column in sql.  how to add new column in table before two column in sql.   Syntax of Alter table: ALTER... named 'employee' having three fields id, name and salary. Now we want to add two
how to seperation one file into two - Java Interview Questions
how to seperation one file into two  hi can anyone tell me how to seperation one file into two so that the data inside e.g a=apple will get seperated and comes as a in one file and apple in another
can u plz help out how to attach file directly & send to a particular mail id
can u plz help out how to attach file directly & send to a particular mail id  i am building a project online LEAVE MANAGEMENT SYSTEM to our coll... for approval so i need ur guidance how to tackle this problem
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 values  I need to save two values so i can use those values later i came up.... But Don't have any idea how can i do that. Like what format of file i need, or how
How can you add elements of two arrayLists in java?
How can you add elements of two arrayLists in java?  if you have an arrayList of tempArr = [hi, my, name, is, john] and another arrayList..., is d, john e] how do you go about
How to get of lastmodified file list between two times in java?
How to get of lastmodified file list between two times in java?  Hi, here is a code that list the last modified files in a directory between two dates. This is working well but is it possible to do the same thing between two
Add two big numbers
Add two big numbers       In this section, you will learn how to add two big numbers...(BigDecimal big): This method is used to add the two BigDecimal numbers. Here
writing a text into text file at particular line number
writing a text into text file at particular line number   Hi, thanks for quick response, I want to insert text at some particular line number.. after line number four my text will display in text file using java program
Applet for add two numbers
Applet for add two numbers  what is the java applet code for add two...); add(text2); label3 = new Label("Sum of Two Numbers...: "); label1.setBounds(20,20,100,20); add(label1); text1 = new TextField(5
writing a text into text file at particular line number
writing a text into text file at particular line number   Hi, thanks for quick response, I want to insert text at some particular line number.. after line number four my text will display in text file using java program  
writing a text into text file at particular line number
writing a text into text file at particular line number   Hi, thanks for quick response, I want to insert text at some particular line number.. after line number four my text will display in text file using java program  
How to add Apache Commons IO in pom.xml file?
How to add Apache Commons IO in pom.xml file?  Hi, How to add the dependency of the Apache Commons IO library in the pom.xml file in a Java project? My Project is using the maven for dependency management and compilation
How to add a file in GZIP file format in Java programme.
How to add a file in GZIP file format in Java programme.  Hi please help me. How to add a file GZIP File format in Java program. If example..., This is the example and suggest link that will help you related to how to add the file
How to read and compare content of two different text file
Description: In the given example you will see how a two text file's content are compared. The BufferedReader class allow us to read a file. The readLine() method used to read the contents of the specified file.  The way
Insert an object with the add(Object) method
Insert an object with the add(Object) method   ... at the end of a list using the add(Object) method. Here is an example that provides the usage of the add(Object) method in more detail. Create a class "
How to include Two multi rows, one with a file Upload and another with normal fields, have problem while including both
How to include Two multi rows, one with a file Upload and another with normal fields, have problem while including both  How to include Two multi rows, one with a file Upload and another with normal fields, have problem while
How to add download option for openwith and saveAs on exporting file to excel in servlet
How to add download option for openwith and saveAs on exporting file to excel... for generated .excel file to save at particular position on the system.Now as per...(); System.out.println("Your excel file has been generated!"); } catch
How to add download option for openwith and saveAs on exporting file to excel in servlet
How to add download option for openwith and saveAs on exporting file to excel... for generated .excel file to save at particular position on the system.Now as per...(); System.out.println("Your excel file has been generated!"); } catch
Java file object
Java file object This section demonstrates you the concept of File object... the programming much easier. Here we will discus the operations performed by File object. Using the File object, you can perform several operations. It parses
To Retrieve a Particular Object From an ArrayList
To Retrieve a Particular Object From an ArrayList   ... an ArrayList object (that contains multiple elements) using the get() method... objects. Now retrieve an object( that is contained in the arraylist) using the get
ModuleNotFoundError: No module named 'nxpy-file-object'
named 'nxpy-file-object' How to remove the ModuleNotFoundError: No module named 'nxpy-file-object' error? Thanks   Hi, In your...ModuleNotFoundError: No module named 'nxpy-file-object'  Hi, My
ModuleNotFoundError: No module named 'py-object-file'
named 'py-object-file' How to remove the ModuleNotFoundError: No module named 'py-object-file' error? Thanks   Hi, In your python...ModuleNotFoundError: No module named 'py-object-file'  Hi, My
Java file line reader
Java file line reader In this section, you will learn how to read a particular line from file. Sometimes you want to read a particular line from the file but you have to read the whole file for this process. So in order to remove
How to know the Sunrise time of particular date and Location.
How to know the Sunrise time of particular date and Location.  Hi, How to create a small application to know the sunrise time of specific date as well as the locations. So, Please provide any online help. so, that i
JDOM Attribute Example, How to add a list of attribute at a xml file element.
JDOM Attribute Example, How to add a list of attribute to a xml file In this tutorial, we will see how to add a list of attribute to a xml file element with the helpof JDOM library. With the help of this java code, you can add
JDOM Element Example, How to add and remove child from xml file.
JDOM Element Example, How to add and remove child from xml file. In this tutorial, we will see how to add and remove child node in an xml Document tree. We can add child any where in xml document tree with the help of JDOM API
how to check particular value is avilable in hashmap - Java Beginners
how to check particular value is avilable in hashmap  Hi friends, i...,these tags is in the form arraylist.then how i will check particular tags...(); if(keyWordExists(headLine)) //here keyWord may be one,two or three like
How to disable an HTML object?
How to disable an HTML object?  How to disable an HTML object
Add two big numbers - Java Beginners
Add two big numbers - Java Beginners  Hi, I am beginner in Java and leaned basic concepts of Java. Now I am trying to find example code for adding big numbers in Java. I need basic Java Beginners example. It should easy
Java file exist
Java file exist  In this section, you will learn how to check the existence of the particular file. Description of code: This is a very simple task... or not. For this, we have created an object of class File and used exists() method
python merge two dictionaries add values
python merge two dictionaries add values  Hi, In Python I have two dictionaries with same keys. I want to merge the two dictionaries and add the values (int values) into the merged dictionary. For example student marks in three
How to get the focus from a particular textarea for some action?
How to get the focus from a particular textarea for some action?  Hi All, I have created one small swing applications with two textarea. I want... in textarea-1... Can somebody please tell me ..How to find a component which had
How to Send particular error to another page in Servlet from web.xml
How to Send particular error to another page in Servlet from web.xml  How to Send particular error to another page in Servlet from web.xml   Hi you can forward particular error from web.xml file as <error-page>

Ads