hi:
i have a program which should read and werite on a file but after the first time it writeon the file it can read te records.but other times it write on the file but just read the records which has written first time. here is my programs:
[CODE=java]
import java.io.FileOut putStream;
import java.io.IOExcep tion;
import java.io.ObjectO utputStream;
import java.util.NoSuc hElementExcepti on;
import java.util.Scann er;
import com.deitel.jhtp 7.ch14.AccountR ecordSerializab le;
public class CreateSequentia lFile
{
private ObjectOutputStr eam output; // outputs data to file
// allow user to specify file name
public void openFile()
{
try // open file
{
output = new ObjectOutputStr eam(
new FileOutputStrea m( "clients.se r" ,true) );
} // end try
catch ( IOException ioException )
{
System.err.prin tln( "Error opening file." );
} // end catch
} // end method openFile
// add records to file
public void addRecords()
{
AccountRecordSe rializable record; // object to be written to file
int accountNumber = 0; // account number for record object
String firstName; // first name for record object
String lastName; // last name for record object
double balance; // balance for record object
Scanner input = new Scanner( System.in );
System.out.prin tf( "%s\n%s\n%s\n%s \n\n",
"To terminate input, type the end-of-file indicator ",
"when you are prompted to enter input.",
"On UNIX/Linux/Mac OS X type <ctrl> d then press Enter",
"On Windows type <ctrl> z then press Enter" );
System.out.prin tf( "%s\n%s",
"Enter account number (> 0), first name, last name and balance.",
"? " );
while ( input.hasNext() ) // loop until end-of-file indicator
{
try // output values to file
{
accountNumber = input.nextInt() ; // read account number
firstName = input.next(); // read first name
lastName = input.next(); // read last name
balance = input.nextDoubl e(); // read balance
if ( accountNumber > 0 )
{
// create new record
record = new AccountRecordSe rializable( accountNumber,
firstName, lastName, balance );
output.writeObj ect( record ); // output record
} // end if
else
{
System.out.prin tln(
"Account number must be greater than 0." );
} // end else
} // end try
catch ( IOException ioException )
{
System.err.prin tln( "Error writing to file." );
return;
} // end catch
catch ( NoSuchElementEx ception elementExceptio n )
{
System.err.prin tln( "Invalid input. Please try again." );
input.nextLine( ); // discard input so user can try again
} // end catch
System.out.prin tf( "%s %s\n%s", "Enter account number (>0),",
"first name, last name and balance.", "? " );
} // end while
} // end method addRecords
// close file and terminate application
public void closeFile()
{
try // close file
{
if ( output != null )
output.close();
} // end try
catch ( IOException ioException )
{
System.err.prin tln( "Error closing file." );
System.exit( 1 );
} // end catch
} // end method closeFile
} // end class CreateSequentia lFile
[/CODE]
[CODE=java]
import java.io.EOFExce ption;
import java.io.FileInp utStream;
import java.io.IOExcep tion;
import java.io.ObjectI nputStream;
import com.deitel.jhtp 7.ch14.AccountR ecordSerializab le;
public class ReadSequentialF ile
{
private ObjectInputStre am input;
// enable user to select file to open
public void openFile()
{
try // open file
{
input = new ObjectInputStre am(
new FileInputStream ( "clients.se r" ) );
} // end try
catch ( IOException ioException )
{
System.err.prin tln( "Error opening file." );
} // end catch
} // end method openFile
// read record from file
public void readRecords()
{
AccountRecordSe rializable record;
System.out.prin tf( "%-10s%-12s%-12s%10s\n", "Account",
"First Name", "Last Name", "Balance" );
try // input the values from the file
{
while ( true )
{
record = ( AccountRecordSe rializable ) input.readObjec t();
// display record contents
System.out.prin tf( "%-10d%-12s%-12s%10.2f\n",
record.getAccou nt(), record.getFirst Name(),
record.getLastN ame(), record.getBalan ce() );
} // end while
} // end try
catch ( EOFException endOfFileExcept ion )
{
return; // end of file was reached
} // end catch
catch ( ClassNotFoundEx ception classNotFoundEx ception )
{
System.err.prin tln( "Unable to create object." );
} // end catch
catch ( IOException ioException )
{
System.err.prin tln( "Error during reading from file." );
} // end catch
} // end method readRecords
// close file and terminate application
public void closeFile()
{
try // close file and exit
{
if ( input != null )
input.close();
System.exit( 0 );
} // end try
catch ( IOException ioException )
{
System.err.prin tln( "Error closing file." );
System.exit( 1 );
} // end catch
} // end method closeFile
} // end class ReadSequentialF ile
[/CODE]
what should i do to always read all records ?
i have a program which should read and werite on a file but after the first time it writeon the file it can read te records.but other times it write on the file but just read the records which has written first time. here is my programs:
[CODE=java]
import java.io.FileOut putStream;
import java.io.IOExcep tion;
import java.io.ObjectO utputStream;
import java.util.NoSuc hElementExcepti on;
import java.util.Scann er;
import com.deitel.jhtp 7.ch14.AccountR ecordSerializab le;
public class CreateSequentia lFile
{
private ObjectOutputStr eam output; // outputs data to file
// allow user to specify file name
public void openFile()
{
try // open file
{
output = new ObjectOutputStr eam(
new FileOutputStrea m( "clients.se r" ,true) );
} // end try
catch ( IOException ioException )
{
System.err.prin tln( "Error opening file." );
} // end catch
} // end method openFile
// add records to file
public void addRecords()
{
AccountRecordSe rializable record; // object to be written to file
int accountNumber = 0; // account number for record object
String firstName; // first name for record object
String lastName; // last name for record object
double balance; // balance for record object
Scanner input = new Scanner( System.in );
System.out.prin tf( "%s\n%s\n%s\n%s \n\n",
"To terminate input, type the end-of-file indicator ",
"when you are prompted to enter input.",
"On UNIX/Linux/Mac OS X type <ctrl> d then press Enter",
"On Windows type <ctrl> z then press Enter" );
System.out.prin tf( "%s\n%s",
"Enter account number (> 0), first name, last name and balance.",
"? " );
while ( input.hasNext() ) // loop until end-of-file indicator
{
try // output values to file
{
accountNumber = input.nextInt() ; // read account number
firstName = input.next(); // read first name
lastName = input.next(); // read last name
balance = input.nextDoubl e(); // read balance
if ( accountNumber > 0 )
{
// create new record
record = new AccountRecordSe rializable( accountNumber,
firstName, lastName, balance );
output.writeObj ect( record ); // output record
} // end if
else
{
System.out.prin tln(
"Account number must be greater than 0." );
} // end else
} // end try
catch ( IOException ioException )
{
System.err.prin tln( "Error writing to file." );
return;
} // end catch
catch ( NoSuchElementEx ception elementExceptio n )
{
System.err.prin tln( "Invalid input. Please try again." );
input.nextLine( ); // discard input so user can try again
} // end catch
System.out.prin tf( "%s %s\n%s", "Enter account number (>0),",
"first name, last name and balance.", "? " );
} // end while
} // end method addRecords
// close file and terminate application
public void closeFile()
{
try // close file
{
if ( output != null )
output.close();
} // end try
catch ( IOException ioException )
{
System.err.prin tln( "Error closing file." );
System.exit( 1 );
} // end catch
} // end method closeFile
} // end class CreateSequentia lFile
[/CODE]
[CODE=java]
import java.io.EOFExce ption;
import java.io.FileInp utStream;
import java.io.IOExcep tion;
import java.io.ObjectI nputStream;
import com.deitel.jhtp 7.ch14.AccountR ecordSerializab le;
public class ReadSequentialF ile
{
private ObjectInputStre am input;
// enable user to select file to open
public void openFile()
{
try // open file
{
input = new ObjectInputStre am(
new FileInputStream ( "clients.se r" ) );
} // end try
catch ( IOException ioException )
{
System.err.prin tln( "Error opening file." );
} // end catch
} // end method openFile
// read record from file
public void readRecords()
{
AccountRecordSe rializable record;
System.out.prin tf( "%-10s%-12s%-12s%10s\n", "Account",
"First Name", "Last Name", "Balance" );
try // input the values from the file
{
while ( true )
{
record = ( AccountRecordSe rializable ) input.readObjec t();
// display record contents
System.out.prin tf( "%-10d%-12s%-12s%10.2f\n",
record.getAccou nt(), record.getFirst Name(),
record.getLastN ame(), record.getBalan ce() );
} // end while
} // end try
catch ( EOFException endOfFileExcept ion )
{
return; // end of file was reached
} // end catch
catch ( ClassNotFoundEx ception classNotFoundEx ception )
{
System.err.prin tln( "Unable to create object." );
} // end catch
catch ( IOException ioException )
{
System.err.prin tln( "Error during reading from file." );
} // end catch
} // end method readRecords
// close file and terminate application
public void closeFile()
{
try // close file and exit
{
if ( input != null )
input.close();
System.exit( 0 );
} // end try
catch ( IOException ioException )
{
System.err.prin tln( "Error closing file." );
System.exit( 1 );
} // end catch
} // end method closeFile
} // end class ReadSequentialF ile
[/CODE]
what should i do to always read all records ?
Comment