Java How to Program上面的一段示例,参照一下: import java.io.*;// Java extension packages import javax.swing.*;// Deitel packages import com.deitel.jhtp4.ch16.RandomAccessAccountRecord;public class CreateRandomFile { // enable user to select file to open private void createFile() { // display dialog so user can choose file JFileChooser fileChooser = new JFileChooser(); fileChooser.setFileSelectionMode( JFileChooser.FILES_ONLY ); int result = fileChooser.showSaveDialog( null );
// if user clicked Cancel button on dialog, return if ( result == JFileChooser.CANCEL_OPTION ) return; // obtain selected file File fileName = fileChooser.getSelectedFile(); // display error if file name invalid if ( fileName == null || fileName.getName().equals( "" ) ) JOptionPane.showMessageDialog( null, "Invalid File Name", "Invalid File Name", JOptionPane.ERROR_MESSAGE ); else { // open file try { RandomAccessFile file = new RandomAccessFile( fileName, "rw" ); RandomAccessAccountRecord blankRecord = new RandomAccessAccountRecord(); // write 100 blank records for ( int count = 0; count < 100; count++ ) blankRecord.write( file ); // close file file.close(); // display message that file was created JOptionPane.showMessageDialog( null, "Created file " + fileName, "Status", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); // terminate program } // process exceptions during open, write or // close file operations catch ( IOException ioException ) { JOptionPane.showMessageDialog( null, "Error processing file", "Error processing file", JOptionPane.ERROR_MESSAGE ); System.exit( 1 ); } } } // end method openFile // execute application to create file user specifies public static void main( String args[] ) { CreateRandomFile application = new CreateRandomFile(); application.createFile(); } } // end class CreateRandomFile
// no-argument constructor calls other constructor // with default values public RandomAccessAccountRecord() { this( 0, "", "", 0.0 ); } // initialize a RandomAccessAccountRecord public RandomAccessAccountRecord( int account, String firstName, String lastName, double balance ) { super( account, firstName, lastName, balance ); } // read a record from specified RandomAccessFile public void read( RandomAccessFile file ) throws IOException { setAccount( file.readInt() ); setFirstName( padName( file ) ); setLastName( padName( file ) ); setBalance( file.readDouble() ); } // ensure that name is proper length private String padName( RandomAccessFile file ) throws IOException { char name[] = new char[ 15 ], temp; for ( int count = 0; count < name.length; count++ ) { temp = file.readChar(); name[ count ] = temp; }
return new String( name ).replace( '\0', ' ' ); } // write a record to specified RandomAccessFile public void write( RandomAccessFile file ) throws IOException { file.writeInt( getAccount() ); writeName( file, getFirstName() ); writeName( file, getLastName() ); file.writeDouble( getBalance() ); } // write a name to file; maximum of 15 characters private void writeName( RandomAccessFile file, String name ) throws IOException { StringBuffer buffer = null; if ( name != null ) buffer = new StringBuffer( name ); else buffer = new StringBuffer( 15 ); buffer.setLength( 15 ); file.writeChars( buffer.toString() ); } // NOTE: This method contains a hard coded value for the // size of a record of information. public static int size() { return 72; }} // end class RandomAccessAccountRecord
import java.io.*;// Java extension packages
import javax.swing.*;// Deitel packages
import com.deitel.jhtp4.ch16.RandomAccessAccountRecord;public class CreateRandomFile { // enable user to select file to open
private void createFile()
{
// display dialog so user can choose file
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(
JFileChooser.FILES_ONLY ); int result = fileChooser.showSaveDialog( null );
// if user clicked Cancel button on dialog, return
if ( result == JFileChooser.CANCEL_OPTION )
return; // obtain selected file
File fileName = fileChooser.getSelectedFile(); // display error if file name invalid
if ( fileName == null ||
fileName.getName().equals( "" ) )
JOptionPane.showMessageDialog( null,
"Invalid File Name", "Invalid File Name",
JOptionPane.ERROR_MESSAGE ); else { // open file
try {
RandomAccessFile file =
new RandomAccessFile( fileName, "rw" ); RandomAccessAccountRecord blankRecord =
new RandomAccessAccountRecord(); // write 100 blank records
for ( int count = 0; count < 100; count++ )
blankRecord.write( file ); // close file
file.close(); // display message that file was created
JOptionPane.showMessageDialog( null,
"Created file " + fileName, "Status",
JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); // terminate program
} // process exceptions during open, write or
// close file operations
catch ( IOException ioException ) {
JOptionPane.showMessageDialog( null,
"Error processing file", "Error processing file",
JOptionPane.ERROR_MESSAGE ); System.exit( 1 );
}
} } // end method openFile // execute application to create file user specifies
public static void main( String args[] )
{
CreateRandomFile application = new CreateRandomFile(); application.createFile();
} } // end class CreateRandomFile
import java.io.*;public class RandomAccessAccountRecord extends AccountRecord {
// no-argument constructor calls other constructor
// with default values
public RandomAccessAccountRecord()
{
this( 0, "", "", 0.0 );
} // initialize a RandomAccessAccountRecord
public RandomAccessAccountRecord( int account,
String firstName, String lastName, double balance )
{
super( account, firstName, lastName, balance );
} // read a record from specified RandomAccessFile
public void read( RandomAccessFile file ) throws IOException
{
setAccount( file.readInt() );
setFirstName( padName( file ) );
setLastName( padName( file ) );
setBalance( file.readDouble() );
} // ensure that name is proper length
private String padName( RandomAccessFile file )
throws IOException
{
char name[] = new char[ 15 ], temp; for ( int count = 0; count < name.length; count++ ) {
temp = file.readChar();
name[ count ] = temp;
}
return new String( name ).replace( '\0', ' ' );
} // write a record to specified RandomAccessFile
public void write( RandomAccessFile file ) throws IOException
{
file.writeInt( getAccount() );
writeName( file, getFirstName() );
writeName( file, getLastName() );
file.writeDouble( getBalance() );
} // write a name to file; maximum of 15 characters
private void writeName( RandomAccessFile file, String name )
throws IOException
{
StringBuffer buffer = null; if ( name != null )
buffer = new StringBuffer( name );
else
buffer = new StringBuffer( 15 ); buffer.setLength( 15 );
file.writeChars( buffer.toString() );
} // NOTE: This method contains a hard coded value for the
// size of a record of information.
public static int size()
{
return 72;
}} // end class RandomAccessAccountRecord
既帮助了别人,也提高了自己!
我也学的java how to program
朝5颗星奋斗