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;
   }

}

 


 



import java.io.FileNotFoundException;
import java.lang.SecurityException;
import java.util.Formatter;
import java.util.FormatterClosedException;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class CreateTextFile

{

   private Formatter output;

   public void openFile() //開文件

   {

      try

      {
         output = new Formatter("clients.txt");
      }

      catch ( SecurityException securityException )

      {
         System.err.println("您沒有對這個文件的寫入");
         System.exit(1);
      }

      catch ( FileNotFoundException filesNotFoundException )

      {
         System.err.println("創造錯誤文件.");
         System.exit(1);
      }

   }

  
   public void addRecords()

   {

      AccountRecord record = new AccountRecord();

      Scanner input = new Scanner( System.in );

      System.out.printf("%s\n%s\n%s\n%s\n\n",
       "終止輸入, 鍵入end-of-file顯示",
       "當你被提示進去輸入.",
       "在UNIX/Linux/Mac OS X之下 按 + d 然後按進入",
       "在Windows之下 按 + z 然後按進入");

      System.out.printf("%s\n%s\n",
       "帳號必須是大於0, 姓,名,結餘",
       "請依序輸入");

      while( input.hasNext() )

      {

         try

         {

            record.setAccount( input.nextInt() );
            record.setFirstName( input.next() );
            record.setLastName( input.next() );
            record.setBalance( input.nextDouble() );

            if ( record.getAccount() > 0 )   //檢查帳號是否大於零

            {
               output.format( "%d %s %s %.2f\n",
                       record.getAccount(),
                       record.getFirstName(),
                       record.getLastName(),
                       record.getBalance() );
            }

            else

            {
               System.out.println("帳號必須是大於0");
            }

         }

         catch ( FormatterClosedException formatterClosedException )
         {
            System.err.println("寫給錯誤文件");
            return;
         }

         catch ( NoSuchElementException elementException )
         {
            System.err.println( "無效輸入。請再嘗試." );
            input.nextLine();
         }

         System.out.printf( "%s %s\n%s\n",
            "帳號必須是大於0",
            "姓,名,結餘", "請依序輸入" );

      }

   }

   public void closeFile()

   {
      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 class ReadTextFileTest

{

   public static void main ( String args[] )

   {

      ReadTextFile application = new ReadTextFile();

      application.openFile();
      application.readRecords();
      application.closeFile();

   }

}




上面三個檔是存了一個文字檔
下面兩個檔是為了把那個已儲存的文字檔 叫出來看

 

上面三個檔是存了一個文字檔下面兩個檔是為了把那個已儲存的文字檔 叫出來看 上面三個檔是存了一個文字檔下面兩個檔是為了把那個已儲存的文字檔 叫出來看

 

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 a25561970 的頭像
    a25561970

    萬花筒

    a25561970 發表在 痞客邦 留言(0) 人氣()