JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java 新手區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 Re:sequentialFile 小問題~感恩! [Re:lonely741215]
lonely741215

數學才是王道



發文: 196
積分: 0
於 2011-11-25 14:24 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
lonely741215 wrote:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Fig. 17.17: CreateSequentialFile.java
// Writing objects sequentially to a file with class ObjectOutputStream.
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.NoSuchElementException;
import java.util.Scanner;
 
import com.deitel.ch17.AccountRecordSerializable;
 
public class CreateSequentialFile
{
   private ObjectOutputStream output; // outputs data to file
 
   // allow user to specify file name
   public void openFile()
   {
      try // open file
      {
         output = new ObjectOutputStream(
            new FileOutputStream( "clients.ser" ) );
      } // end try
      catch ( IOException ioException )
      {
         System.err.println( "Error opening file." );
      } // end catch
   } // end method openFile
 
   // add records to file
   public void addRecords()
   {
      AccountRecordSerializable record; // object to be written to file
      Scanner input = new Scanner( System.in );
 
      System.out.printf( "%s\n%s\n%s\n%s\n\n",
         "To terminate input, type the end-of-file indicator ",
         "when you are prompted to enter input.",
         "On UNIX/Linux/Mac OS X type <ctrl> d then press Enter",
         "On Windows type <ctrl> z then press Enter" );
 
      System.out.printf( "%s\n%s", 
         "Enter account number (> 0), first name, last name and balance.",
         "? " );
 
      while ( input.hasNext() ) // loop until end-of-file indicator
      {
         try // output values to file
         {
            record.setAccount( input.nextInt() );              //從這列開始
            record.setFirstName( input.next() );            
            record.setLastname( input.next() );
            record.setBalance( input.nextDouble() );         //到這列結束
 
            if ( accountNumber > 0 )
            {
               output.writeObject( record ); // output record
            } // end if
            else
            {
               System.out.println(
                  "Account number must be greater than 0." );
            } // end else
         } // end try
         catch ( IOException ioException )
         {
            System.err.println( "Error writing to file." );
            return;
         } // end catch
         catch ( NoSuchElementException elementException )
         {
            System.err.println( "Invalid input. Please try again." );
            input.nextLine(); // discard input so user can try again
         } // end catch
 
         System.out.printf( "%s %s\n%s", "Enter account number (>0),",
            "first name, last name and balance.", "? " );
      } // end while
   } // end method addRecords
 
   // close file and terminate application 
   public void closeFile() 
   {
      try // close file
      {
         if ( output != null )
            output.close();
      } // end try
      catch ( IOException ioException )
      {
         System.err.println( "Error closing file." );
         System.exit( 1 );
      } // end catch
   } // end method closeFile
} // end class CreateSequentialFile

52~55我如果把書的code修成這樣,我如果輸入四比合格的資料,但show出資料時,卻全部都是第一筆資料的結果,也就是列出了四個一模一樣的第一筆資料,請問為什麼這樣寫反而會錯呢?謝謝


reply to postreply to post
數學啊數學~總有一天我要狂嗑你!!
話題樹型展開
人氣 標題 作者 字數 發文時間
515 sequentialFile 小問題~感恩! lonely741215 3254 2011-11-25 14:23
470 Re:sequentialFile 小問題~感恩! lonely741215 3299 2011-11-25 14:24
364 Re:sequentialFile 小問題~感恩! pclevin 115 2011-11-25 23:18
435 Re:sequentialFile 小問題~感恩! lonely741215 5664 2011-11-26 14:10
383 Re:sequentialFile 小問題~感恩! pclevin 242 2011-11-27 20:15
» JWorld@TW »  Java 新手區

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8