public class AccountRecord
{
private int account;
private String firstName;
private String lastName;
private double balance;
public AccountRecord()
{
this(0,"","", 0.0);
}
public AccountRecord( int acct, String first, String last, double bal )
{
setAccount(acct);
setFirstName(first);
setLastName(last);
setBalance(bal);
}
public void setAccount( int acct )
{
account = acct;
}
public int getAccount()
{
return account;
}
public void setFirstName( String first )
{
firstName = first;
}
public String getFirstName()
{
return firstName;
}
public void setLastName( String last )
{
lastName = last;
}
public String getLastName()
{
return lastName;
}
public void setBalance( double bal )
{
balance = bal;
}
public double getBalance()
{
return balance;
}
} public class CreateTextFile { private Formatter output; public void openFile() //開文件 { try { catch ( SecurityException securityException ) { catch ( FileNotFoundException filesNotFoundException ) { } { AccountRecord record = new AccountRecord(); Scanner input = new Scanner( System.in ); System.out.printf("%s\n%s\n%s\n%s\n\n", System.out.printf("%s\n%s\n", while( input.hasNext() ) { try { record.setAccount( input.nextInt() ); if ( record.getAccount() > 0 ) //檢查帳號是否大於零 { else { } catch ( FormatterClosedException formatterClosedException ) catch ( NoSuchElementException elementException ) System.out.printf( "%s %s\n%s\n", } } public void closeFile() { }
import java.io.FileNotFoundException;
import java.lang.SecurityException;
import java.util.Formatter;
import java.util.FormatterClosedException;
import java.util.NoSuchElementException;
import java.util.Scanner;
output = new Formatter("clients.txt");
}
System.err.println("您沒有對這個文件的寫入");
System.exit(1);
}
System.err.println("創造錯誤文件.");
System.exit(1);
}
public void addRecords()
"終止輸入, 鍵入end-of-file顯示",
"當你被提示進去輸入.",
"在UNIX/Linux/Mac OS X之下 按 + d 然後按進入",
"在Windows之下 按 + z 然後按進入");
"帳號必須是大於0, 姓,名,結餘",
"請依序輸入");
record.setFirstName( input.next() );
record.setLastName( input.next() );
record.setBalance( input.nextDouble() );
output.format( "%d %s %s %.2f\n",
record.getAccount(),
record.getFirstName(),
record.getLastName(),
record.getBalance() );
}
System.out.println("帳號必須是大於0");
}
{
System.err.println("寫給錯誤文件");
return;
}
{
System.err.println( "無效輸入。請再嘗試." );
input.nextLine();
}
"帳號必須是大於0",
"姓,名,結餘", "請依序輸入" );
if ( output != null )
output.close(); //關閉文件
}
public class CreateTextFileTest
public class CreateTextFileTest
public class CreateTextFileTest
{
public static void main ( String args[] )
{
CreateTextFile application = new CreateTextFile();
application.openFile(); //開檔
application.addRecords(); //寫紀錄
application.closeFile(); //關檔
}
}
輸入最後要離開的時後按Ctrl + Z 再 Enter 然後記事本檔用Word開啟
輸入最後要離開的時後按Ctrl + Z 再 Enter 然後記事本檔用Word開啟
輸入最後要離開的時後按Ctrl + Z 再 Enter 然後記事本檔用Word開啟
import java.io.File;
import java.io.FileNotFoundException;
import java.lang.IllegalStateException;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class ReadTextFile
{
private Scanner input;
public void openFile()
{
try
{
input = new Scanner ( new File ( "clients.txt" ) );
}
catch ( FileNotFoundException fileNotFoundException )
{
System.err.println( "Error opening file." );
System.exit( 1 );
}
}
public void readRecords()
{
AccountRecord record = new AccountRecord();
System.out.printf( "%-10s%-12s%-12s%10s\n", "帳號","姓","名", "餘額" );
try
{
while ( input.hasNext() )
{
record.setAccount( input.nextInt() );
record.setFirstName( input.next() );
record.setLastName( input.next() );
record.setBalance( input.nextDouble() );
System.out.printf( %-12d%-13s%-18s%10.2f\n",
record.getAccount(),
record.getFirstName(),
record.getLastName(),
record.getBalance() );
}
}
catch ( NoSuchElementException elementException )
{
System.err.println( "File improperly formed." );
input.close();
System.exit( 1 );
}
catch ( IllegalStateException stateException )
{
System.err.println( "Error reading from file." );
System.exit( 1 );
}
}
public void closeFile()
{
if( input != null )
input.close();
}
} { public static void main ( String args[] ) { ReadTextFile application = new ReadTextFile(); application.openFile(); } }
public class ReadTextFileTest
application.readRecords();
application.closeFile();
上面三個檔是存了一個文字檔
下面兩個檔是為了把那個已儲存的文字檔 叫出來看
留言列表