写入和读取顺序文件
来源:互联网 发布:linux创建启动器 编辑:程序博客网 时间:2024/04/27 13:34
下面是文件结构
// 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();}}
- 写入和读取顺序文件
- 文件读取和写入
- 文件读取和写入
- 文件读取和写入
- 读取文件和写入文件
- 文件的顺序写入与读取
- 文件的读取和写入
- 读取和写入plist文件
- 读取和写入plist文件
- ******************* 文件的写入和读取 *******************
- 【OpenMesh】文件读取和写入
- 文件的读取和写入
- matlab文件读取和写入
- XML文件写入和读取
- javascript文件读取和写入
- 文件的读取和写入
- Java读取和写入文件
- hdf5文件读取和写入
- 在S2SH项目中--把hibernate.cfg.xml合并到applicationContext.xml中
- test
- 学习网站
- Perl Verify Data
- pku acm 1012
- 写入和读取顺序文件
- 热转印
- 相信你也会喜欢这八句话!
- 人与人为何不同
- malloc()函数与alloc()函数
- 金属拉丝
- C语言字节对齐
- android 学习第一篇
- c/c++ 字符串函数 strtok函数