| Author |
Urgent Problem Regarding I/O
|
sameershintrein
Greenhorn
Joined: Feb 18, 2002
Posts: 1
|
|
hi all, I got stuck with some I/O related program in Java.Actually this program is same as in Deitel's book. But after running the program i am getting not getting the data in the output file project.dat properly. Also i tried to retrieve the data from the file using the second program but it is also displaying the blank data in the textfield. I am not getting WHY???. Can anyone help me urgently regarding the same.Please guide me wich class i have to use. waiting for reply. thanx in advance. regards, sameer here are the two programs //this program is to store the data in a file project.dat import java.io.*; import java.awt.*; import java.awt.event.*; public class ttt extends Frame implements ActionListener{ private TextField accountField,firstNameField,lastNameField,balanceField; private Button enter,done,next; static String sum; int a,b; private DataOutputStream output; private DataInputStream input; public ttt() { super("create client file"); try{ output=new DataOutputStream(new FileOutputStream("project.dat")); //input=new DataInputStream(new FileInputStream("project1.dat")); } catch(IOException e) { //System.err.println("file not opened properly",+ e.toString()); System.exit(1); } setSize(300,150); setLayout(new GridLayout(5,2)); add(new Label("�ccoount no")); accountField=new TextField(); add(accountField); add(new Label("First name")); firstNameField=new TextField(20); add(firstNameField); add(new Label("Last Name")); lastNameField=new TextField(20); add(lastNameField); add(new Label("Balance")); balanceField=new TextField(20); add(balanceField); enter=new Button("enter"); enter.addActionListener(this); add(enter); done=new Button("done"); done.addActionListener(this); add(done); setVisible(true); } public void addRecord() { int accountNumber=0; Double d; if(!accountField.getText().equals("")) { try { accountNumber=Integer.parseInt(accountField.getText()); if(accountNumber>0) { output.writeInt(accountNumber); output.writeUTF(firstNameField.getText()); output.writeUTF(lastNameField.getText()); d=new Double(balanceField.getText()); output.writeDouble(d.doubleValue()); } accountField.setText(""); firstNameField.setText(""); lastNameField.setText(""); balanceField.setText(""); } catch(NumberFormatException nfe) { System.err.println("u must enter an in value"); } catch(IOException io) { System.err.println("err during writing a file"); System.exit(1); } } } /*public void readRecord() { int account; String first,last; double balance; try{ account=input.readInt(); first=input.readUTF(); last=input.readUTF(); balance=input.readDouble(); accountField.setText(String.valueOf(account)); firstNameField.setText(first); lastNameField.setText(last); balanceField.setText(String.valueOf(balance)); } catch(EOFException ef) { closeFile(); } catch(IOException e) { System.out.println("ghfhfgf"); System.exit(1); } }*/ /*private void closeFile() { try{ input.close(); System.exit(0); } catch(IOException e) { System.out.println("ghfhfgf"); System.exit(1); } }*/ public void actionPerformed(ActionEvent e) { addRecord(); if(e.getSource()==done) { try { output.close(); } catch(IOException io) { System.err.println("file not closed properly"+io.toString()); } System.exit(0); } } public static void main(String args[]) { new ttt(); } } and second: //this file is to retrieve the data from the file project.dat import java.io.*; import java.awt.*; import java.awt.event.*; public class ttt1 extends Frame implements ActionListener{ private TextField accountField,firstNameField,lastNameField,balanceField; private Button enter,done,next; static String sum; int a,b; private DataInputStream input; public ttt1() { super("create client file"); try{ input=new DataInputStream(new FileInputStream("project.dat")); } catch(IOException e) { //System.err.println("file not opened properly",+ e.toString()); System.exit(1); } setSize(300,150); setLayout(new GridLayout(5,2)); add(new Label("Last Name")); lastNameField=new TextField(20); add(lastNameField); add(new Label("Balance")); balanceField=new TextField(20); add(balanceField); enter=new Button("enter"); enter.addActionListener(this); add(enter); done=new Button("done"); done.addActionListener(this); add(done); next=new Button("next"); done.addActionListener(this); add(next); setVisible(true); } public void readRecord() { int account; String first,last; double balance; try{ account=input.readInt(); first=input.readUTF(); last=input.readUTF(); balance=input.readDouble(); accountField.setText(String.valueOf(account)); firstNameField.setText(first); lastNameField.setText(last); balanceField.setText(String.valueOf(balance)); } catch(EOFException ef) { closeFile(); } catch(IOException e) { System.out.println("Exception"); System.exit(1); } } private void closeFile() { try{ input.close(); System.exit(0); } catch(IOException e) { System.out.println("ghfhfgf"); System.exit(1); } } public void actionPerformed(ActionEvent e) { if(e.getSource()==next) readRecord(); else closeFile(); } public static void main(String args[]) { new ttt1(); } }
|
 |
 |
|
|
subject: Urgent Problem Regarding I/O
|
|
|