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

» JWorld@TW » Java SE 討論區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 製作通訊錄的問題...
trbslv





發文: 16
積分: 0
於 2005-01-11 23: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
public ClassMate(String a,String b,int c)(){
     name = a;
     phone = b;
     year = c;
}


主程式中宣告一個陣列空間來存同學資料
1
ClassMate[] c = new ClassMate[100];

並且也有方法可以新增,修改,和瀏覽 同學的資料
問題來了...
程式下次開啟 之前輸入的資料都沒了...
我想要將資料存下來
並且下次開啟程式的時候可以讀取以前輸入的資料
然後可以繼續新增...


reply to postreply to post
作者 Re:製作通訊錄的問題... [Re:trbslv]
browser

戀香

版主

發文: 3570
積分: 1
於 2005-01-11 23:29 user profilesend a private message to usersend email to browserreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
請把資料儲存成檔案 or 資料庫中
不然資料當然會不見

PS.

這個問題和製作通訊錄無關 ....
依照你命名方式的邏輯可以為:
製作XXX的問題


reply to postreply to post
作者 Re:製作通訊錄的問題... [Re:trbslv]
trbslv





發文: 16
積分: 0
於 2005-01-12 00:17 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
不好意思= =
那我這樣問吧...
已經將陣列資料寫入到檔案
假設已經有兩筆資料ClassMate[0],ClassMate[1]
讀取的時候怎樣讓這些資料重新排入ClassMate[0],ClassMate[1]
並且在新增資料的時候會從下一個陣列重新寫入ClassMate[2]

謝謝....


reply to postreply to post
作者 Re:製作通訊錄的問題... [Re:trbslv]
hahawawa





發文: 50
積分: 0
於 2005-01-12 01:02 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
如果方便的話,把你完整的程式碼post上來

reply to postreply to post
作者 Re:製作通訊錄的問題... [Re:trbslv]
trbslv





發文: 16
積分: 0
於 2005-01-12 06:16 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
public class ClassMate{
 
  public String name,memo;
  public int phone;
 
  public ClassMate(String a,int b,String c){
     name = a;
                   phone = b;
     memo = c;
  }
 
public class Show{
          public static void main(String[] args){
   int countNum = 0;    //陣列中用來紀錄同學資料筆數
                int exit = 0;
                ClassMate[] c = new ClassMate[1000];
 
            while(exit!=88){
                 try{
                     BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
                 //選單(減少篇幅...)1是新增同學資料
                     String option = in.readLine();
    //新增同學資料--------
                  if(option.equals("1")){
                     System.out.print("姓名:");
                     String n = in.readLine();
                     System.out.print("電話:");
                     int aPhone = Integer.parseInt(in.readLine());
                     System.out.print("備註:");
                     String m = in.readLine();
                     c[countNum] = new ClassMate(n,aPhone,m);
                     countNum++ ;
                    }
                 }catch(){
                 }
              }//while 結束
 
         }
}   


程式碼大慨是這樣子

再說一次我的問題吧...
由使用者新增N筆資料到陣列要怎麼把陣列裡面的資料寫出
下次開啟時能夠繼續增加資料,並不會影響到現存的資料...

困擾許久 麻煩了
先謝啦!


reply to postreply to post
作者 Re:製作通訊錄的問題... [Re:trbslv]
weaker

小屁屁∼



發文: 379
積分: 4
於 2005-01-12 09:27 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
trbslv 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
public class ClassMate{
 
  public String name,memo;
  public int phone;
 
  public ClassMate(String a,int b,String c){
     name = a;
                   phone = b;
     memo = c;
  }
 
public class Show{
          public static void main(String[] args){
   int countNum = 0;    //陣列中用來紀錄同學資料筆數
                int exit = 0;
                ClassMate[] c = new ClassMate[1000];
 
            while(exit!=88){
                 try{
                     BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
                 //選單(減少篇幅...)1是新增同學資料
                     String option = in.readLine();
    //新增同學資料--------
                  if(option.equals("1")){
                     System.out.print("姓名:");
                     String n = in.readLine();
                     System.out.print("電話:");
                     int aPhone = Integer.parseInt(in.readLine());
                     System.out.print("備註:");
                     String m = in.readLine();
                     c[countNum] = new ClassMate(n,aPhone,m);
                     countNum++ ;
                    }
                 }catch(){
                 }
              }//while 結束
 
         }
}   


程式碼大慨是這樣子

再說一次我的問題吧...
由使用者新增N筆資料到陣列要怎麼把陣列裡面的資料寫出
下次開啟時能夠繼續增加資料,並不會影響到現存的資料...

困擾許久 麻煩了
先謝啦!


用 ArrayList or Vector 存資料吧。
然後利用 ObjectOutputStream 存入到檔案,
要讀出來時,再利用 ObjectInputStream 讀取。
下次讀取出來,再利用 add 就可以直接加在最後面了。


reply to postreply to post
作者 Re:製作通訊錄的問題... [Re:trbslv]
trbslv





發文: 16
積分: 0
於 2005-01-12 16:27 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
該怎麼作啊...我還是一頭霧水@@"

reply to postreply to post
作者 Re:製作通訊錄的問題... [Re:trbslv]
trbslv





發文: 16
積分: 0
於 2005-01-12 20:54 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
是這樣子用嗎...
查了一整天API= =
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 //宣告
   Vector v = new Vector();
......
....
....
//讀取會員陣列
   ....
  ...
  ...
   for(int i = 1 ; i<c.length ; i++)
   v.add(c[i]);
   將Vector存入ObjectOutputStream   ←←←我不會了= =
 
//寫入會員陣列
   我又不會了.....><"
 


reply to postreply to post
作者 Re:製作通訊錄的問題... [Re:trbslv]
weaker

小屁屁∼



發文: 379
積分: 4
於 2005-01-12 23:27 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
trbslv wrote:
是這樣子用嗎...
查了一整天API= =
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 //宣告
   Vector v = new Vector();
......
....
....
//讀取會員陣列
   ....
  ...
  ...
   for(int i = 1 ; i<c.length ; i++)
   v.add(c[i]);
   將Vector存入ObjectOutputStream   ←←←我不會了= =
 
//寫入會員陣列
   我又不會了.....><"
 



基本的存取檔案你會嗎?
其實使用方法是一樣的阿。
你確定你有看 API ?
ObjectOutputStream 和 ObjectInputStream 都有給你一個範例了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
ObjectOutputStream :
  FileOutputStream fos = new FileOutputStream("t.tmp");
  ObjectOutputStream oos = new ObjectOutputStream(fos);
 
  oos.writeInt(12345);
  oos.writeObject("Today");
  oos.writeObject(new Date());
 
  oos.close();
 
ObjectInputStream :
  FileInputStream fis = new FileInputStream("t.tmp");
  ObjectInputStream ois = new ObjectInputStream(fis);
 
  int i = ois.readInt();
  String today = (String) ois.readObject();
  Date date = (Date) ois.readObject();
 
  ois.close();


reply to postreply to post
作者 Re:製作通訊錄的問題... [Re:trbslv]
attz





發文: 64
積分: 0
於 2005-01-13 03:34 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
先將妳的資料結構繼承 序列化物件
然後在關閉時 以檔案寫入某地方
在開起時 以檔案讀出

微塵


reply to postreply to post
作者 Re:製作通訊錄的問題... [Re:trbslv]
trbslv





發文: 16
積分: 0
於 2005-01-13 03:44 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
喔...謝謝大大!英文不太好@@"
現在有一個問題是 該怎麼把一組陣列資料存到Vecter
再透過ObjectOutputStream儲存

ObjectOutputStream跟ObjectInputStream我研究出一點點了
可是我的資料會被後來讀取的地方覆寫耶...
不知道哪裡出問題@@ 我沒用Vector

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
//宣告
        ClassMate[] c = new ClassMate[1000];
        ObjectOutputStream objOutput;
        ObjectInputStream objInput;
        Object o ="";
        File myFile = new File("obj.dat");
 
//Load-----
        objInput = new ObjectInputStream(new FileInputStream("obj.dat"));
        countNum = objInput.readInt();
        for(int i = 0; i < countNum  ; i++) {
              o = objInput.readObject();
             ((ClassMate)o).profile();
         }
         objInput.close();
                     System.out.println("(countP=)"+countP);
                     System.out.println("(countNum=)"+countNum);
 
//Save------
        objOutput = new ObjectOutputStream(new FileOutputStream("obj.dat"));
 
        objOutput.writeInt(countP);
        for(int i = 0; i < c.length; i++)
             objOutput.writeObject(c[i]);
        objOutput.close();


附上我的程式碼@@"

http://knugt.d2g.com/download/java.rar

java.rar (4.17k)


reply to postreply to post
作者 Re:製作通訊錄的問題... [Re:trbslv]
weaker

小屁屁∼



發文: 379
積分: 4
於 2005-01-13 09:22 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
trbslv wrote:
喔...謝謝大大!英文不太好@@"
現在有一個問題是 該怎麼把一組陣列資料存到Vecter
再透過ObjectOutputStream儲存

ObjectOutputStream跟ObjectInputStream我研究出一點點了
可是我的資料會被後來讀取的地方覆寫耶...
不知道哪裡出問題@@ 我沒用Vector

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
//宣告
        ClassMate[] c = new ClassMate[1000];
        ObjectOutputStream objOutput;
        ObjectInputStream objInput;
        Object o ="";
        File myFile = new File("obj.dat");
 
//Load-----
        objInput = new ObjectInputStream(new FileInputStream("obj.dat"));
        countNum = objInput.readInt();
        for(int i = 0; i < countNum  ; i++) {
              o = objInput.readObject();
             ((ClassMate)o).profile();
         }
         objInput.close();
                     System.out.println("(countP=)"+countP);
                     System.out.println("(countNum=)"+countNum);
 
//Save------
        objOutput = new ObjectOutputStream(new FileOutputStream("obj.dat"));
 
        objOutput.writeInt(countP);
        for(int i = 0; i < c.length; i++)
             objOutput.writeObject(c[i]);
        objOutput.close();


附上我的程式碼@@"

http://knugt.d2g.com/download/java.rar


你的save動作,相當於重新建立一個 obj.dat 來存資料。
這樣當然資料都會被覆蓋過去。
可以仔細看一下 FileOutputStream 的 API。
1
2
3
4
5
6
7
public FileOutputStream(File file,
                        boolean append)
                 throws FileNotFoundException
 
    Creates a file output stream to write to the file represented by the 
specified File object.If the second argument is true, then bytes will
 be written to the end of the file rather than the beginning. 


reply to postreply to post
作者 Re:製作通訊錄的問題... [Re:trbslv]
trbslv





發文: 16
積分: 0
於 2005-01-13 19:59 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
原來是這樣子啊@@!!
多謝!!!
多謝!!!


reply to postreply to post
» JWorld@TW »  Java SE 討論區

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