下面是文件结构
// Fig. 14.5: AccountRecord.java
// A class that represents one record of information.
public class AccountRecord
{
private int account;
private String firstName;
private String lastName;
private double balance;
// no-argument constructor calls other constructor with default values
public AccountRecord()
{
this( 0, "", "", 0.0 ); // call four-argument constructor
} // end no-argument AccountRecord constructor
// initialize a record
public AccountRecord( int acct, String first, String last, double bal )
{
setAccount( acct );
setFirstName( first );
setLastName( last );
setBalance( bal );
} // end four-argument AccountRecord constructor
// set account number
public void setAccount( int acct )
{
account = acct;
} // end method setAccount
// get account number
public int getAccount()
{
return account;
} // end method getAccount
// set first name
public void setFirstName( String first )
{
firstName = first;
} // end method setFirstName
// get first name
public String getFirstName()
{
return firstName;
} // end method getFirstName
// set last name
public void setLastName( String last )
{
lastName = last;
} // end method setLastName
// get last name
public String getLastName()
{
return lastName;
} // end method getLastName
// set balance
public void setBalance( double bal )
{
balance = bal;
} // end method setBalance
// get balance
public double getBalance()
{
return balance;
} // end method getBalance
} // end class AccountRecord
下面是创建
import java.io.*;
import java.util.Formatter;
import java.util.InputMismatchException;
import java.util.Scanner;
public class CreatTextFile {
private Formatter textFile;
public void creatFile()
{
try
{
textFile = new Formatter("test.txt");
}
catch (SecurityException e)
{
System.out.println("you have no access to open this file");
System.exit(1);
}
catch( FileNotFoundException e )
{
System.out.println("Creat File Error!");
System.exit(1);
}
}
public void addRecord()
{
AccountRecord record = new AccountRecord();
System.out.print("enter account number(>0), first name, last name, balance\n? ");
Scanner input = new Scanner (System.in);
while (input.hasNext())
{
try
{
record.setAccount( input.nextInt() );
record.setFirstName( input.next() );
record.setLastName( input.next() );
record.setBalance( input.nextDouble() );
if (record.getAccount() < 0)
{
System.out.println("account number must greater than 0, try again" );
}
else
{
textFile.format("%d %s %s %.2f\n", record.getAccount(), record.getFirstName(),
record.getLastName(), record.getBalance() );
System.out.print("enter account number(>0), first name, last name, balance\n? ");
}
}
catch (InputMismatchException e)
{
System.out.println("Input invailed,try again");
input.nextLine();
}
}
}
public void closeFile()
{
textFile.close();
}
public static void main (String args[] )
{
CreatTextFile myTest = new CreatTextFile();
myTest.creatFile();
myTest.addRecord();
myTest.closeFile();
}
}
下面是读取文件
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.InputMismatchException;
public class ReadFiles {
public Scanner input;
public void openFiles()
{
try
{
input = new Scanner ( new File ("D:\\Java\\FIG14.7\\test.txt"));
}
catch (SecurityException e)
{
System.out.println("You have no access to this file.");
System.exit(1);
}
catch(FileNotFoundException e)
{
System.out.println("File not found.");
System.exit(1); //注意 一定要加 exit语句,否则程序在捕捉后还会运行!!!
}
/*
catch (IllegalStateException e)
{
}
*/
}
public void readData()
{
AccountRecord record = new AccountRecord();
while (input.hasNext() )
{
try
{
record.setAccount( input.nextInt() );
record.setFirstName( input.next() );
record.setLastName( input.next() );
record.setBalance( input.nextDouble() );
}
catch (InputMismatchException e)
{
System.out.println( "inputMismatch");
System.exit(1);
}
System.out.printf("%d\t%s\t%s\t%.2f\n", record.getAccount() , record.getFirstName(),
record.getLastName(), record.getBalance() );
}
}
public void closeFile()
{
input.close();
}
public static void main(String argc[] )
{
ReadFiles a= new ReadFiles();
a.openFiles();
a.readData();
a.closeFile();
}
}