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

» JWorld@TW » Java 新手區  

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

數學才是王道



發文: 196
積分: 0
於 2011-11-25 14:23 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
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

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


reply to postreply to post
數學啊數學~總有一天我要狂嗑你!!
作者 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
數學啊數學~總有一天我要狂嗑你!!
作者 Re:sequentialFile 小問題~感恩! [Re:lonely741215]
pclevin





發文: 322
積分: 0
於 2011-11-25 23:18 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
我沒有看到你的Reader,也沒有辦法跑你的程式?
你的main方法呢?
還有
import com.deitel.ch17.AccountRecordSerializable;那是支什麼程式
因為你的52~55跟它有關系


reply to postreply to post
我的網誌
http://pclevin.blogspot.com
作者 Re:sequentialFile 小問題~感恩! [Re:pclevin]
lonely741215

數學才是王道



發文: 196
積分: 0
於 2011-11-26 14:10 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
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
package com.deitel.ch17; // packaged for reuse
 
import java.io.Serializable;
 
public class AccountRecordSerializable implements Serializable
{
   private int account;
   private String firstName;
   private String lastName;
   private double balance;
   
   // no-argument constructor calls other constructor with default values
   public AccountRecordSerializable() 
   {
      this( 0, "", "", 0.0 );
   } // end no-argument AccountRecordSerializable constructor
  
   // four-argument constructor initializes a record
   public AccountRecordSerializable(
      int acct, String first, String last, double bal )
   {
      setAccount( acct );
      setFirstName( first );
      setLastName( last );
      setBalance( bal );
   } // end four-argument AccountRecordSerializable 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 AccountRecordSerializable


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
95
96
97
98
99
100
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
      int accountNumber = 0; // account number for record object
      String firstName; // first name for record object
      String lastName; // last name for record object
      double balance; // balance for record object
 
      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
         {
            accountNumber = input.nextInt(); // read account number
            firstName = input.next(); // read first name
            lastName = input.next(); // read last name
            balance = input.nextDouble(); // read balance
 
            if ( accountNumber > 0 )
            {
               // create new record
               record = new AccountRecordSerializable( accountNumber,
                  firstName, lastName, balance );
               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


1
2
3
4
5
6
7
8
9
10
11
public class CreateSequentialFileTest
{
   public static void main( String[] args )
   {
      CreateSequentialFile application = new CreateSequentialFile();
 
      application.openFile();
      application.addRecords();
      application.closeFile();
   } // end main
} // end class CreateSequentialFileTest


以上是原本的code,感恩!


reply to postreply to post
數學啊數學~總有一天我要狂嗑你!!
作者 Re:sequentialFile 小問題~感恩! [Re:lonely741215]
pclevin





發文: 322
積分: 0
於 2011-11-27 20:15 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:
50~53我如果把書的code修成這樣,我如果輸入四比合格的資料,但show出資料時,卻全部都是第一筆資料的結果,也就是列出了四個一模一樣的第一筆資料,請問為什麼這樣寫反而會錯呢?謝謝

我測試是正常的,可以輸入資料到文件檔呀,
我用ObjectInputStream讀檔,出來看也是正常的。
你的code沒有用到ObjectInputStream讀檔。
你是什麼讀檔show出資料來看。


reply to postreply to post
我的網誌
http://pclevin.blogspot.com
» JWorld@TW »  Java 新手區

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

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8