Trang 1 của 2 12 CuốiCuối
Kết quả 1 đến 10 của 16
  1. #1
    Ngày tham gia
    Apr 2015
    Bài viết
    0

    Bài 33: Đọc ghi file theo Object trong Java

    /* bài viết thuộc loạt bài hướng dẫn trong "khóa học lập trình java miễn phí"
    trên diễn đàn android.vn, bạn có thể vào đây đọc

    để hiểu hơn về khóa học và tham gia ngay cùng mọi người */



    android.vn/attachments/serialization-jpg.22858/" border="0" alt="" />


    - đặt 1 tình huống thực tế: khi ta muốn quản lý 1 danh sách sinh viên, tất nhiên sẽ là 1 dãy rất nhiều đối tượng, mỗi đối tượng sinh viên lại có nhiều thuộc tính: tên, tuổi, điểm, .v..v.. và chúng ta cần lưu toàn bộ thông tin này vào file, ngoài ra còn phải xử lý, chỉnh sửa thông tin nếu cần. như vậy, nếu sử dụng kiến thức ở những bài trước làm điều này thì là rất khó khăn. nhưng với phần kiến thức ở bài này thì điều đó lại trở lên cực kỳ dễ dàng. cụ thể ở bài tập giống như tình huống giả thiết trên, chúng ta sẽ sử dụng kiểu đọc ghi object để thao thác, lưu trữ từng đối tượng lên file!

    - mình sẽ viết 2 ví dụ cụ thể, với các mức độ khác nhau,từ đơn giản nhất. nếu bạn thấy khó hiểu chỗ nào có thể bình luận phía dưới. mọi người cùng trao đổi.

    **chú ý: để đọc ghi theo object trong java thì tại class object đó ta cần đặt giao tiếp java.io.serializable (implements serializable)

    ví dụ 1: đọc ghi 1 đối tượng lên file theo từng object:
    - khai báo 1 class sinhvien gồm 2 thuộc tính họ tên, tuổi. tạo 1 đối tượng cụ thể sinh viên a sau đó
    gán thông tin cho sinh viên a rồi ghi đối tượng a này xuống file "e:\\sinhvien.dat"
    - mở file trên ra. đọc file đó theo kiểu object để lấy đối tượng đó ra, (cần ép kiểu về kiểu dữ liệu của đối tượng), rồi gán nó vào đối tượng a1. in ra màn hình thông tin đối tượng a1. code như sau:



    Mã nguồn PHP:
    [color=#000000]
    package javaandroidvn[/color][color=#007700]; [/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]fileinputstream[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]fileoutputstream[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]ioexception[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]objectinputstream[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]objectoutputstream[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]serializable[/color][color=#007700]; class [/color][color=#0000bb]sinhvien [/color][color=#007700]implements [/color][color=#0000bb]serializable [/color][color=#007700]{ public [/color][color=#0000bb]string hoten[/color][color=#007700]; public [/color][color=#0000bb]int tuoi[/color][color=#007700];} public class [/color][color=#0000bb]javaandroidvn [/color][color=#007700]{ public static [/color][color=#0000bb]void main[/color][color=#007700]([/color][color=#0000bb]string[/color][color=#007700][] [/color][color=#0000bb]args[/color][color=#007700]) [/color][color=#0000bb]throws ioexception [/color][color=#007700]{ [/color][color=#0000bb]sinhvien a [/color][color=#007700]= new [/color][color=#0000bb]sinhvien[/color][color=#007700](); [/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]hoten [/color][color=#007700]= [/color][color=#dd0000]"vũ văn tường"[/color][color=#007700]; [/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]tuoi [/color][color=#007700]= [/color][color=#0000bb]21[/color][color=#007700]; try { [/color][color=#0000bb]fileoutputstream f [/color][color=#007700]= new [/color][color=#0000bb]fileoutputstream[/color][color=#007700]([/color][color=#dd0000]"e:\\sinhvien.dat"[/color][color=#007700]); [/color][color=#0000bb]objectoutputstream oot [/color][color=#007700]= new [/color][color=#0000bb]objectoutputstream[/color][color=#007700]([/color][color=#0000bb]f[/color][color=#007700]); [/color][color=#ff8000]// sử dụng để ghi file theo từng object [/color][color=#0000bb]oot[/color][color=#007700].[/color][color=#0000bb]writeobject[/color][color=#007700]([/color][color=#0000bb]a[/color][color=#007700]); [/color][color=#ff8000]// ghi object là đối tượng a xuống file [/color][color=#0000bb]oot[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); [/color][color=#0000bb]f[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); } catch ([/color][color=#0000bb]ioexception e[/color][color=#007700]) { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"có lỗi xảy ra!"[/color][color=#007700]); } [/color][color=#0000bb]sinhvien a1 [/color][color=#007700]= new [/color][color=#0000bb]sinhvien[/color][color=#007700](); [/color][color=#ff8000]//tạo đối tượng a1 mới, để phía dưới gán bằng đối tượng ta lấy được ra từ file [/color][color=#007700]try { [/color][color=#0000bb]fileinputstream f [/color][color=#007700]= new [/color][color=#0000bb]fileinputstream[/color][color=#007700]([/color][color=#dd0000]"e:\\sinhvien.dat"[/color][color=#007700]); [/color][color=#0000bb]objectinputstream oit [/color][color=#007700]= new [/color][color=#0000bb]objectinputstream[/color][color=#007700]([/color][color=#0000bb]f[/color][color=#007700]); [/color][color=#ff8000]// sử dụng để đọc file theo từng object [/color][color=#0000bb]a1 [/color][color=#007700]= ([/color][color=#0000bb]sinhvien[/color][color=#007700]) [/color][color=#0000bb]oit[/color][color=#007700].[/color][color=#0000bb]readobject[/color][color=#007700](); [/color][color=#ff8000]//đọc object đầu tiên ép kiểu về kiểu sinhvien sau đó gán bằng đối tượng a1 [/color][color=#0000bb]oit[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); [/color][color=#0000bb]f[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); } catch ([/color][color=#0000bb]ioexception io[/color][color=#007700]) { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"có lỗi xảy ra!"[/color][color=#007700]); } catch ([/color][color=#0000bb]classnotfoundexception ex[/color][color=#007700]) { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"không tìm thấy class"[/color][color=#007700]); } [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"thông tin sinh vien a1 đọc ra là:
    tên: "[/color][color=#007700]+[/color][color=#0000bb]a1[/color][color=#007700].[/color][color=#0000bb]hoten[/color][color=#007700]+[/color][color=#dd0000]"
    tuổi: "[/color][color=#007700]+[/color][color=#0000bb]a1[/color][color=#007700].[/color][color=#0000bb]tuoi[/color][color=#007700]); }}[/color] 
    ví dụ 2: đọc ghi 2 đối tượng lên file theo từng object. các thuộc tính bài này để private:
    (qua ví dụ này, và kết hợp bài sau, mình sẽ giới thiệu cách đọc ghi 1 danh sách các đối tượng lên file, giúp việc quản lý danh sách các đối tượng dễ dàng hơn!)


    Mã nguồn PHP:
    [color=#000000]
    package javaandroidvn[/color][color=#007700]; [/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]fileinputstream[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]fileoutputstream[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]ioexception[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]objectinputstream[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]objectoutputstream[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]serializable[/color][color=#007700]; class [/color][color=#0000bb]sinhvien [/color][color=#007700]implements [/color][color=#0000bb]serializable [/color][color=#007700]{ private [/color][color=#0000bb]string hoten[/color][color=#007700]; private [/color][color=#0000bb]int tuoi[/color][color=#007700]; public [/color][color=#0000bb]string gethoten[/color][color=#007700]() { return [/color][color=#0000bb]hoten[/color][color=#007700]; } public [/color][color=#0000bb]void sethoten[/color][color=#007700]([/color][color=#0000bb]string hoten[/color][color=#007700]) { [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]hoten [/color][color=#007700]= [/color][color=#0000bb]hoten[/color][color=#007700]; } public [/color][color=#0000bb]int gettuoi[/color][color=#007700]() { return [/color][color=#0000bb]tuoi[/color][color=#007700]; } public [/color][color=#0000bb]void settuoi[/color][color=#007700]([/color][color=#0000bb]int tuoi[/color][color=#007700]) { [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]tuoi [/color][color=#007700]= [/color][color=#0000bb]tuoi[/color][color=#007700]; }} public class [/color][color=#0000bb]javaandroidvn [/color][color=#007700]{ public static [/color][color=#0000bb]void main[/color][color=#007700]([/color][color=#0000bb]string[/color][color=#007700][] [/color][color=#0000bb]args[/color][color=#007700]) [/color][color=#0000bb]throws ioexception [/color][color=#007700]{ [/color][color=#0000bb]sinhvien a [/color][color=#007700]= new [/color][color=#0000bb]sinhvien[/color][color=#007700](); [/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]sethoten[/color][color=#007700]([/color][color=#dd0000]"vũ văn t"[/color][color=#007700]); [/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]settuoi[/color][color=#007700]([/color][color=#0000bb]21[/color][color=#007700]); [/color][color=#0000bb]sinhvien b [/color][color=#007700]= new [/color][color=#0000bb]sinhvien[/color][color=#007700](); [/color][color=#0000bb]b[/color][color=#007700].[/color][color=#0000bb]sethoten[/color][color=#007700]([/color][color=#dd0000]"nguyễn văn a"[/color][color=#007700]); [/color][color=#0000bb]b[/color][color=#007700].[/color][color=#0000bb]settuoi[/color][color=#007700]([/color][color=#0000bb]22[/color][color=#007700]); try { [/color][color=#0000bb]fileoutputstream f [/color][color=#007700]= new [/color][color=#0000bb]fileoutputstream[/color][color=#007700]([/color][color=#dd0000]"e:\\sinhvien.dat"[/color][color=#007700]); [/color][color=#0000bb]objectoutputstream oot [/color][color=#007700]= new [/color][color=#0000bb]objectoutputstream[/color][color=#007700]([/color][color=#0000bb]f[/color][color=#007700]); [/color][color=#ff8000]// sử dụng để ghi file theo từng object [/color][color=#0000bb]oot[/color][color=#007700].[/color][color=#0000bb]writeobject[/color][color=#007700]([/color][color=#0000bb]a[/color][color=#007700]); [/color][color=#ff8000]// ghi object là đối tượng a xuống file [/color][color=#0000bb]oot[/color][color=#007700].[/color][color=#0000bb]writeobject[/color][color=#007700]([/color][color=#0000bb]b[/color][color=#007700]); [/color][color=#0000bb]oot[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); [/color][color=#0000bb]f[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); } catch ([/color][color=#0000bb]ioexception e[/color][color=#007700]) { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"có lỗi xảy ra!"[/color][color=#007700]); } [/color][color=#0000bb]sinhvien a1 [/color][color=#007700]= new [/color][color=#0000bb]sinhvien[/color][color=#007700](); [/color][color=#ff8000]//tạo đối tượng a1 mới, để phía dưới gán bằng đối tượng ta lấy được ra từ file [/color][color=#0000bb]sinhvien b1 [/color][color=#007700]= new [/color][color=#0000bb]sinhvien[/color][color=#007700](); [/color][color=#ff8000]//tạo đối tượng b1 mới, để phía dưới gán bằng đối tượng ta lấy được ra từ file [/color][color=#007700]try { [/color][color=#0000bb]fileinputstream f [/color][color=#007700]= new [/color][color=#0000bb]fileinputstream[/color][color=#007700]([/color][color=#dd0000]"e:\\sinhvien.dat"[/color][color=#007700]); [/color][color=#0000bb]objectinputstream oit [/color][color=#007700]= new [/color][color=#0000bb]objectinputstream[/color][color=#007700]([/color][color=#0000bb]f[/color][color=#007700]); [/color][color=#ff8000]// sử dụng để đọc file theo từng object [/color][color=#0000bb]a1 [/color][color=#007700]= ([/color][color=#0000bb]sinhvien[/color][color=#007700]) [/color][color=#0000bb]oit[/color][color=#007700].[/color][color=#0000bb]readobject[/color][color=#007700](); [/color][color=#ff8000]//đọc object đầu tiên ép kiểu về kiểu sinhvien sau đó gán bằng đối tượng a1 [/color][color=#0000bb]b1 [/color][color=#007700]= ([/color][color=#0000bb]sinhvien[/color][color=#007700]) [/color][color=#0000bb]oit[/color][color=#007700].[/color][color=#0000bb]readobject[/color][color=#007700](); [/color][color=#ff8000]//đọc object tiếp theo, ép kiểu về kiểu sinhvien sau đó gán bằng đối tượng b1 [/color][color=#0000bb]oit[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); [/color][color=#0000bb]f[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); } catch ([/color][color=#0000bb]ioexception io[/color][color=#007700]) { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"có lỗi xảy ra!"[/color][color=#007700]); } catch ([/color][color=#0000bb]classnotfoundexception ex[/color][color=#007700]) { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"không tìm thấy class"[/color][color=#007700]); } [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"thông tin sinh vien a1 đọc ra là:
    tên: " [/color][color=#007700]+ [/color][color=#0000bb]a1[/color][color=#007700].[/color][color=#0000bb]gethoten[/color][color=#007700]() + [/color][color=#dd0000]"
    tuổi: " [/color][color=#007700]+ [/color][color=#0000bb]a1[/color][color=#007700].[/color][color=#0000bb]gettuoi[/color][color=#007700]()); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"thông tin sinh vien b1 đọc ra là:
    tên
    : " [/color][color=#007700]+ [/color][color=#0000bb]b1[/color][color=#007700].[/color][color=#0000bb]gethoten[/color][color=#007700]() + [/color][color=#dd0000]"
    tuổi: " [/color][color=#007700]+ [/color][color=#0000bb]b1[/color][color=#007700].[/color][color=#0000bb]gettuoi[/color][color=#007700]()); }}[/color] 
    bài tập về nhà::d
    class thẻ thư viện có các thuộc tính: mã thẻ, tên sinh viên, số sách mượn. tạo ra 3 đối tượng thẻ thư viện, gán giá trị nhập vào từ bàn phím. ghi lại thông tin 3 đối tượng này xuống file theo kiểu object rồi đọc file đó, đọc dữ liệu các đối tượng từ trong file vừa ghi, in ra màn hình!

    (chú ý: các bạn nên tự giác làm bài tập, hoàn thiện rồi đăng lên, các bài bạn đã làm, nên để code trong thẻ code - chọn thẻ php cho rõ ràng và để ẩn code trong thẻ ẩn spoiler, để khi click vào mới hiện code lên , trước khi thử sức tự làm, bạn không nên nhìn code của các bạn khác đã đăng nhé)

    my facebook: https://www.facebook.com/tiasangmoi

    bài viết đăng lên diễn đàn khác vui lòng ghi rõ nguồn: android.vn

  2. #2
    Ngày tham gia
    May 2015
    Bài viết
    0
    thank rất nhiều! thất sự về mấy cái nhập file trong c và c++ rất khó hiểu và mình đã ko học được! nhưng sang java cũng ko nghĩ là nó sẽ đơn giản hơn!

  3. #3
    Ngày tham gia
    Feb 2015
    Bài viết
    0
    bài tập này không khác ví dụ mẫu là mấy
    <div class="bbcodeblock bbcodequote bbcmspoilerblock">
    <div class="attribution type"><noscript>spoiler (move your mouse to the spoiler area to reveal the content)</noscript>

    show spoiler
    hide spoiler

    </div>
    <div class="quotecontent">
    <div class="bbcm_spoiler_noscript"><blockquote>


    Mã nguồn PHP:
    [color=#000000]
    [/color][color=#007700]class [/color][color=#0000bb]thethuvien [/color][color=#007700]implements [/color][color=#0000bb]serializable [/color][color=#007700]{ private [/color][color=#0000bb]string ma_the[/color][color=#007700]; private [/color][color=#0000bb]string ten_sv[/color][color=#007700]; private [/color][color=#0000bb]int so_sach_muon[/color][color=#007700]; public [/color][color=#0000bb]thethuvien[/color][color=#007700]([/color][color=#0000bb]string ma_the[/color][color=#007700], [/color][color=#0000bb]string ten_sv[/color][color=#007700], [/color][color=#0000bb]int so_sach_muon[/color][color=#007700]) { [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]ma_the [/color][color=#007700]= [/color][color=#0000bb]ma_the[/color][color=#007700]; [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]ten_sv [/color][color=#007700]= [/color][color=#0000bb]ten_sv[/color][color=#007700]; [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]so_sach_muon [/color][color=#007700]= [/color][color=#0000bb]so_sach_muon[/color][color=#007700]; } public [/color][color=#0000bb]string getma_the[/color][color=#007700]() { return [/color][color=#0000bb]ma_the[/color][color=#007700]; } public [/color][color=#0000bb]string getten_sv[/color][color=#007700]() { return [/color][color=#0000bb]ten_sv[/color][color=#007700]; } public [/color][color=#0000bb]int getso_sach_muon[/color][color=#007700]() { return [/color][color=#0000bb]so_sach_muon[/color][color=#007700]; } } public class [/color][color=#0000bb]baitap2 [/color][color=#007700]{ public static [/color][color=#0000bb]void main[/color][color=#007700]([/color][color=#0000bb]string[/color][color=#007700][] [/color][color=#0000bb]args[/color][color=#007700]) { [/color][color=#0000bb]thethuvien a [/color][color=#007700]= new [/color][color=#0000bb]thethuvien[/color][color=#007700]([/color][color=#dd0000]"a1"[/color][color=#007700], [/color][color=#dd0000]"luffy"[/color][color=#007700],[/color][color=#0000bb]1[/color][color=#007700]); [/color][color=#0000bb]thethuvien b [/color][color=#007700]= new [/color][color=#0000bb]thethuvien[/color][color=#007700]([/color][color=#dd0000]"a2"[/color][color=#007700], [/color][color=#dd0000]"zoro"[/color][color=#007700], [/color][color=#0000bb]2[/color][color=#007700]); [/color][color=#0000bb]thethuvien c [/color][color=#007700]= new [/color][color=#0000bb]thethuvien[/color][color=#007700]([/color][color=#dd0000]"a3"[/color][color=#007700], [/color][color=#dd0000]"nami"[/color][color=#007700], [/color][color=#0000bb]3[/color][color=#007700]); try { [/color][color=#0000bb]fileoutputstream f [/color][color=#007700]= new [/color][color=#0000bb]fileoutputstream[/color][color=#007700]([/color][color=#dd0000]"e:\\thethuvien.dat"[/color][color=#007700]); [/color][color=#0000bb]objectoutputstream oos [/color][color=#007700]= new [/color][color=#0000bb]objectoutputstream[/color][color=#007700]([/color][color=#0000bb]f[/color][color=#007700]); [/color][color=#0000bb]oos[/color][color=#007700].[/color][color=#0000bb]writeobject[/color][color=#007700]([/color][color=#0000bb]a[/color][color=#007700]); [/color][color=#0000bb]oos[/color][color=#007700].[/color][color=#0000bb]writeobject[/color][color=#007700]([/color][color=#0000bb]b[/color][color=#007700]); [/color][color=#0000bb]oos[/color][color=#007700].[/color][color=#0000bb]writeobject[/color][color=#007700]([/color][color=#0000bb]c[/color][color=#007700]); [/color][color=#0000bb]f[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); [/color][color=#0000bb]oos[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); } catch ([/color][color=#0000bb]filenotfoundexception ex[/color][color=#007700]) { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#0000bb]ex[/color][color=#007700]); } catch ([/color][color=#0000bb]ioexception e[/color][color=#007700]) { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#0000bb]e[/color][color=#007700]); } try { [/color][color=#0000bb]fileinputstream f [/color][color=#007700]= new [/color][color=#0000bb]fileinputstream[/color][color=#007700]([/color][color=#dd0000]"e:\\thethuvien.dat"[/color][color=#007700]); [/color][color=#0000bb]objectinputstream ois [/color][color=#007700]= new [/color][color=#0000bb]objectinputstream[/color][color=#007700]([/color][color=#0000bb]f[/color][color=#007700]); [/color][color=#0000bb]a [/color][color=#007700]= ([/color][color=#0000bb]thethuvien[/color][color=#007700]) [/color][color=#0000bb]ois[/color][color=#007700].[/color][color=#0000bb]readobject[/color][color=#007700](); [/color][color=#0000bb]b [/color][color=#007700]= ([/color][color=#0000bb]thethuvien[/color][color=#007700]) [/color][color=#0000bb]ois[/color][color=#007700].[/color][color=#0000bb]readobject[/color][color=#007700](); [/color][color=#0000bb]c [/color][color=#007700]= ([/color][color=#0000bb]thethuvien[/color][color=#007700]) [/color][color=#0000bb]ois[/color][color=#007700].[/color][color=#0000bb]readobject[/color][color=#007700](); [/color][color=#0000bb]f[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); [/color][color=#0000bb]ois[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); } catch ([/color][color=#0000bb]classnotfoundexception [/color][color=#007700]| [/color][color=#0000bb]ioexception ex[/color][color=#007700]) { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#0000bb]ex[/color][color=#007700]); } [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"thong tin the thu vien thu nhat:
    ma the: " [/color][color=#007700]+ [/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]getma_the[/color][color=#007700]() + [/color][color=#dd0000]"
    ten: " [/color][color=#007700]+ [/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]getten_sv[/color][color=#007700]() + [/color][color=#dd0000]"
    so sach muon: " [/color][color=#007700]+ [/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]getso_sach_muon[/color][color=#007700]() + [/color][color=#dd0000]"
    ----------"[/color][color=#007700]); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"thong tin the thu vien thu hai:
    ma the: " [/color][color=#007700]+ [/color][color=#0000bb]b[/color][color=#007700].[/color][color=#0000bb]getma_the[/color][color=#007700]() + [/color][color=#dd0000]"
    ten: " [/color][color=#007700]+ [/color][color=#0000bb]b[/color][color=#007700].[/color][color=#0000bb]getten_sv[/color][color=#007700]() + [/color][color=#dd0000]"
    so sach muon: " [/color][color=#007700]+ [/color][color=#0000bb]b[/color][color=#007700].[/color][color=#0000bb]getso_sach_muon[/color][color=#007700]() + [/color][color=#dd0000]"
    ----------"[/color][color=#007700]); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"thong tin the thu vien thu ba:
    ma the: " [/color][color=#007700]+ [/color][color=#0000bb]c[/color][color=#007700].[/color][color=#0000bb]getma_the[/color][color=#007700]() + [/color][color=#dd0000]"
    ten: " [/color][color=#007700]+ [/color][color=#0000bb]c[/color][color=#007700].[/color][color=#0000bb]getten_sv[/color][color=#007700]() + [/color][color=#dd0000]"
    so sach muon: " [/color][color=#007700]+ [/color][color=#0000bb]c[/color][color=#007700].[/color][color=#0000bb]getso_sach_muon[/color][color=#007700]() + [/color][color=#dd0000]"
    ----------"[/color][color=#007700]); }}[/color] 
    </blockquote></div>
    </div>
    </div>

  4. #4
    Ngày tham gia
    Jul 2015
    Bài viết
    186
    cảm ơn rất nhiều

  5. #5
    Ngày tham gia
    Jul 2015
    Bài viết
    2
    <div class="bbcodeblock bbcodequote bbcmspoilerblock">
    <div class="attribution type"><noscript>spoiler (move your mouse to the spoiler area to reveal the content)</noscript>

    show spoiler
    hide spoiler

    </div>
    <div class="quotecontent">
    <div class="bbcm_spoiler_noscript"><blockquote>


    Mã:
    import java.io.fileinputstream;
    import java.io.fileoutputstream;
    import java.io.ioexception;
    import java.io.objectinputstream;
    import java.io.objectoutputstream;
    import java.io.serializable;
    import java.util.scanner;
     
    class sinhvien implements serializable
    {
        private string tensv;
        private int mathe;
        private int sosachmuon;
        public string gettensv() {
            return tensv;
        }
        public void settensv(string tensv) {
            this.tensv = tensv;
        }
        public int getmathe() {
            return mathe;
        }
        public void setmathe(int mathe) {
            this.mathe = mathe;
        }
        public int getsosachmuon() {
            return sosachmuon;
        }
        public void setsosachmuon(int sosachmuon) {
            this.sosachmuon = sosachmuon;
        }
     
    }
    public class btbai33 {
        public static void main(string[]args){
            sinhvien a=new sinhvien();
            sinhvien b=new sinhvien();
            sinhvien c=new sinhvien();
            system.out.println("nhap tt sv a ");
            nhaptt(a);
            system.out.println("nhap tt sv b ");
            nhaptt(b);
            system.out.println("nhap tt sv c ");
            nhaptt(c);
            //ghi file
            ghifile(a, b, c);
            docfile(a, b, c);
        }
        public static void nhaptt(sinhvien a)
        {
        system.out.println("nhap thông tin sinh vien :  " );
            scanner in=new scanner(system.in);
                system.out.println("nhap ma the :");
                a.setmathe(in.nextint());
                in.nextline();
                system.out.println("nhap ho ten:");
                a.settensv(in.nextline());
                system.out.println("nhap so sach muon:");
                a.setsosachmuon(in.nextint());
                in.nextline(); 
            }
        public static void ghifile(sinhvien a,sinhvien b,sinhvien c)
        {
              try {
                  fileoutputstream f = new fileoutputstream("d:\\sinhvien.txt");
                  objectoutputstream oot = new objectoutputstream(f); // sử dụng để ghi file theo từng object
                  oot.writeobject(a); // ghi  object là đối tượng a xuống file
                  oot.writeobject(b);
                  oot.writeobject(c);
                  oot.close();
                  f.close();
              } catch (ioexception e) {
                  system.out.println("có lỗi xảy ra!");
              }
     
        }
        public static void docfile(sinhvien a,sinhvien b,sinhvien c)
        {
            sinhvien a1 = new sinhvien();
            sinhvien b1 = new sinhvien();
            sinhvien c1 = new sinhvien();
          try {
              fileinputstream f = new fileinputstream("d:\\sinhvien.txt");
              objectinputstream oit = new objectinputstream(f);
              a1 = (sinhvien) oit.readobject();
              b1 = (sinhvien) oit.readobject();
              c1 = (sinhvien) oit.readobject();
              oit.close();
              f.close();
          } catch (ioexception io) {
              system.out.println("có lỗi xảy ra!");
          } catch (classnotfoundexception ex) {
              system.out.println("không tìm thấy class");
          }
          system.out.println("thông tin sinh vien a1 đọc ra là:  " );
          system.out.println(" ma the:  "+a.getmathe() );
          system.out.println(" ho ten:  "+a.gettensv() );
          system.out.println(" so sach muon:  "+a.getsosachmuon() );
          system.out.println("thông tin sinh vien b1 đọc ra là:  " );
          system.out.println(" ma the:  "+b.getmathe() );
          system.out.println(" ho ten:  "+b.gettensv() );
          system.out.println(" so sach muon:  "+b.getsosachmuon() );
          system.out.println("thông tin sinh vien c1 đọc ra là:  " );
          system.out.println(" ma the:  "+c.getmathe() );
          system.out.println(" ho ten:  "+c.gettensv() );
          system.out.println(" so sach muon:  "+c.getsosachmuon() );
     
        }
    }
    </blockquote></div>
    </div>
    </div>

  6. #6
    Ngày tham gia
    Apr 2016
    Bài viết
    0
    Mã:
    /**
    *
    */
    package example;
     
    import java.io.fileinputstream;
    import java.io.fileoutputstream;
    import java.io.ioexception;
    import java.io.objectinputstream;
    import java.io.objectoutputstream;
    import java.io.serializable;
    import java.util.scanner;
     
    /**
    * @author chienbk
    *
    */
    public class manalibrary {
     
        /**
        * chuong trinh quan ly thu vien the thu vien co cac thuoc tinh ma the, ten
        * sinh vien, so sach muon tao ra 3 doi tuong the thu vien nhap thong tin 3
        * doi tuong tu ban phim ghi thong tin 3 doi tuong xuong file doc file do va
        * in du lieu ra man hinh
        */
        public static void main(string[] args) throws ioexception {
            // todo auto-generated method stub
            scanner sc = new scanner(system.in);
            librarycard lb1 = new librarycard();
            librarycard lb2 = new librarycard();
            librarycard lb3 = new librarycard();
            // lb1.age =15;
     
            system.out.println("input infomation");
            system.out.println("input infomation id of 1: ");
            lb1.setid(sc.nextline());
            system.out.println("input infomation name of 1: ");
            lb1.setname(sc.nextline());
            system.out.println("input infomation nobook of 1: ");
            lb1.setnobook(sc.nextint());
     
            sc.nextline();
     
            system.out.println("input infomation id of 2: ");
            lb2.setid(sc.nextline());
            system.out.println("input infomation name of 2: ");
            lb2.setname(sc.nextline());
            system.out.println("input infomation nobook of 2: ");
            lb2.setnobook(sc.nextint());
            sc.nextline();
     
            system.out.println("input infomation id of 3: ");
            lb3.setid(sc.nextline());
            system.out.println("input infomation name of 3: ");
            lb3.setname(sc.nextline());
            system.out.println("input infomation nobook of 3: ");
            lb3.setnobook(sc.nextint());
            sc.nextline();
     
            try {
                fileoutputstream f = new fileoutputstream("e:\\data.doc");
                objectoutputstream oos = new objectoutputstream(f);
                oos.writeobject(lb1);
                oos.writeobject(lb2);
                oos.writeobject(lb3);
                oos.close();
                f.close();
     
            } catch (ioexception e) {
                // todo: handle exception
                system.out.println("error!");
            }
            // display infomation
     
            try {
                fileinputstream fs = new fileinputstream("e:\\data.doc");
                objectinputstream ois = new objectinputstream(fs);
                lb1 = (librarycard) ois.readobject();
                lb2 = (librarycard) ois.readobject();
                lb3 = (librarycard) ois.readobject();
                ois.close();
                fs.close();
     
            } catch (ioexception e) {
                // todo: handle exception
                system.out.println("error!");
            } catch (classnotfoundexception e) {
                // todo: handle exception
                system.out.println("not found class");
            }
     
            system.out.println("infomation of student lb1 is: " + lb1.getid() + " "
                    + lb1.getname() + " " + lb1.getnobook());
            system.out.println("infomation of student lb1 is: " + lb2.getid() + " "
                    + lb2.getname() + " " + lb2.getnobook());
            system.out.println("infomation of student lb1 is: " + lb3.getid() + " "
                    + lb3.getname() + " " + lb3.getnobook());
        }
     
    }
     
    class librarycard implements serializable {
        private int tuoi;
        public int age;
        private string id;
        private string name;
        private int nobook;
     
        public string getid() {
            return id;
        }
     
        public void setid(string id) {
            this.id = id;
        }
     
        public string getname() {
            return name;
        }
     
        public void setname(string name) {
            this.name = name;
        }
     
        public int getnobook() {
            return nobook;
        }
     
        public void setnobook(int nobook) {
            this.nobook = nobook;
        }
     
    }

  7. #7
    Ngày tham gia
    Apr 2015
    Bài viết
    0
    cho mình hỏi chút
    có cách nào để đếm số object đã ghi vào file ko?
    muốn ghi thêm object chứ ko ghi đè thì làm thế nào?

  8. #8
    Ngày tham gia
    Sep 2014
    Bài viết
    0
    Trích dẫn Gửi bởi haihack
    bài tập này không khác ví dụ mẫu là mấy
    <div class="bbcodeblock bbcodequote bbcmspoilerblock">
    <div class="attribution type"><noscript>spoiler (move your mouse to the spoiler area to reveal the content)</noscript>

    show spoiler
    hide spoiler

    </div>
    <div class="quotecontent">
    <div class="bbcm_spoiler_noscript"><blockquote>


    Mã nguồn PHP:
    [color=#000000]
    [/color][color=#007700]class [/color][color=#0000bb]thethuvien [/color][color=#007700]implements [/color][color=#0000bb]serializable [/color][color=#007700]{ private [/color][color=#0000bb]string ma_the[/color][color=#007700]; private [/color][color=#0000bb]string ten_sv[/color][color=#007700]; private [/color][color=#0000bb]int so_sach_muon[/color][color=#007700]; public [/color][color=#0000bb]thethuvien[/color][color=#007700]([/color][color=#0000bb]string ma_the[/color][color=#007700], [/color][color=#0000bb]string ten_sv[/color][color=#007700], [/color][color=#0000bb]int so_sach_muon[/color][color=#007700]) { [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]ma_the [/color][color=#007700]= [/color][color=#0000bb]ma_the[/color][color=#007700]; [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]ten_sv [/color][color=#007700]= [/color][color=#0000bb]ten_sv[/color][color=#007700]; [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]so_sach_muon [/color][color=#007700]= [/color][color=#0000bb]so_sach_muon[/color][color=#007700]; } public [/color][color=#0000bb]string getma_the[/color][color=#007700]() { return [/color][color=#0000bb]ma_the[/color][color=#007700]; } public [/color][color=#0000bb]string getten_sv[/color][color=#007700]() { return [/color][color=#0000bb]ten_sv[/color][color=#007700]; } public [/color][color=#0000bb]int getso_sach_muon[/color][color=#007700]() { return [/color][color=#0000bb]so_sach_muon[/color][color=#007700]; } } public class [/color][color=#0000bb]baitap2 [/color][color=#007700]{ public static [/color][color=#0000bb]void main[/color][color=#007700]([/color][color=#0000bb]string[/color][color=#007700][] [/color][color=#0000bb]args[/color][color=#007700]) { [/color][color=#0000bb]thethuvien a [/color][color=#007700]= new [/color][color=#0000bb]thethuvien[/color][color=#007700]([/color][color=#dd0000]"a1"[/color][color=#007700], [/color][color=#dd0000]"luffy"[/color][color=#007700],[/color][color=#0000bb]1[/color][color=#007700]); [/color][color=#0000bb]thethuvien b [/color][color=#007700]= new [/color][color=#0000bb]thethuvien[/color][color=#007700]([/color][color=#dd0000]"a2"[/color][color=#007700], [/color][color=#dd0000]"zoro"[/color][color=#007700], [/color][color=#0000bb]2[/color][color=#007700]); [/color][color=#0000bb]thethuvien c [/color][color=#007700]= new [/color][color=#0000bb]thethuvien[/color][color=#007700]([/color][color=#dd0000]"a3"[/color][color=#007700], [/color][color=#dd0000]"nami"[/color][color=#007700], [/color][color=#0000bb]3[/color][color=#007700]); try { [/color][color=#0000bb]fileoutputstream f [/color][color=#007700]= new [/color][color=#0000bb]fileoutputstream[/color][color=#007700]([/color][color=#dd0000]"e:\\thethuvien.dat"[/color][color=#007700]); [/color][color=#0000bb]objectoutputstream oos [/color][color=#007700]= new [/color][color=#0000bb]objectoutputstream[/color][color=#007700]([/color][color=#0000bb]f[/color][color=#007700]); [/color][color=#0000bb]oos[/color][color=#007700].[/color][color=#0000bb]writeobject[/color][color=#007700]([/color][color=#0000bb]a[/color][color=#007700]); [/color][color=#0000bb]oos[/color][color=#007700].[/color][color=#0000bb]writeobject[/color][color=#007700]([/color][color=#0000bb]b[/color][color=#007700]); [/color][color=#0000bb]oos[/color][color=#007700].[/color][color=#0000bb]writeobject[/color][color=#007700]([/color][color=#0000bb]c[/color][color=#007700]); [/color][color=#0000bb]f[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); [/color][color=#0000bb]oos[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); } catch ([/color][color=#0000bb]filenotfoundexception ex[/color][color=#007700]) { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#0000bb]ex[/color][color=#007700]); } catch ([/color][color=#0000bb]ioexception e[/color][color=#007700]) { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#0000bb]e[/color][color=#007700]); } try { [/color][color=#0000bb]fileinputstream f [/color][color=#007700]= new [/color][color=#0000bb]fileinputstream[/color][color=#007700]([/color][color=#dd0000]"e:\\thethuvien.dat"[/color][color=#007700]); [/color][color=#0000bb]objectinputstream ois [/color][color=#007700]= new [/color][color=#0000bb]objectinputstream[/color][color=#007700]([/color][color=#0000bb]f[/color][color=#007700]); [/color][color=#0000bb]a [/color][color=#007700]= ([/color][color=#0000bb]thethuvien[/color][color=#007700]) [/color][color=#0000bb]ois[/color][color=#007700].[/color][color=#0000bb]readobject[/color][color=#007700](); [/color][color=#0000bb]b [/color][color=#007700]= ([/color][color=#0000bb]thethuvien[/color][color=#007700]) [/color][color=#0000bb]ois[/color][color=#007700].[/color][color=#0000bb]readobject[/color][color=#007700](); [/color][color=#0000bb]c [/color][color=#007700]= ([/color][color=#0000bb]thethuvien[/color][color=#007700]) [/color][color=#0000bb]ois[/color][color=#007700].[/color][color=#0000bb]readobject[/color][color=#007700](); [/color][color=#0000bb]f[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); [/color][color=#0000bb]ois[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); } catch ([/color][color=#0000bb]classnotfoundexception [/color][color=#007700]| [/color][color=#0000bb]ioexception ex[/color][color=#007700]) { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#0000bb]ex[/color][color=#007700]); } [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"thong tin the thu vien thu nhat:
    ma the: " [/color][color=#007700]+ [/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]getma_the[/color][color=#007700]() + [/color][color=#dd0000]"
    ten: " [/color][color=#007700]+ [/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]getten_sv[/color][color=#007700]() + [/color][color=#dd0000]"
    so sach muon: " [/color][color=#007700]+ [/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]getso_sach_muon[/color][color=#007700]() + [/color][color=#dd0000]"
    ----------"[/color][color=#007700]); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"thong tin the thu vien thu hai:
    ma the: " [/color][color=#007700]+ [/color][color=#0000bb]b[/color][color=#007700].[/color][color=#0000bb]getma_the[/color][color=#007700]() + [/color][color=#dd0000]"
    ten: " [/color][color=#007700]+ [/color][color=#0000bb]b[/color][color=#007700].[/color][color=#0000bb]getten_sv[/color][color=#007700]() + [/color][color=#dd0000]"
    so sach muon: " [/color][color=#007700]+ [/color][color=#0000bb]b[/color][color=#007700].[/color][color=#0000bb]getso_sach_muon[/color][color=#007700]() + [/color][color=#dd0000]"
    ----------"[/color][color=#007700]); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"thong tin the thu vien thu ba:
    ma the: " [/color][color=#007700]+ [/color][color=#0000bb]c[/color][color=#007700].[/color][color=#0000bb]getma_the[/color][color=#007700]() + [/color][color=#dd0000]"
    ten: " [/color][color=#007700]+ [/color][color=#0000bb]c[/color][color=#007700].[/color][color=#0000bb]getten_sv[/color][color=#007700]() + [/color][color=#dd0000]"
    so sach muon: " [/color][color=#007700]+ [/color][color=#0000bb]c[/color][color=#007700].[/color][color=#0000bb]getso_sach_muon[/color][color=#007700]() + [/color][color=#dd0000]"
    ----------"[/color][color=#007700]); }}[/color] 
    </blockquote></div>
    </div>
    </div>
    sao mình ghi ra file.txt , sau đó mở lên coi thì nó bị lỗi vậy nhỉ . giải thích cho mình với được hk?

  9. #9
    Ngày tham gia
    Mar 2015
    Bài viết
    0
    <div class="bbcodeblock bbcodequote bbcmspoilerblock">
    <div class="attribution type"><noscript>spoiler (move your mouse to the spoiler area to reveal the content)</noscript>

    show spoiler
    hide spoiler

    </div>
    <div class="quotecontent">
    <div class="bbcm_spoiler_noscript"><blockquote>


    Mã nguồn PHP:
    [color=#000000]
    import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]fileinputstream[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]filenotfoundexception[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]fileoutputstream[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]ioexception[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]objectinputstream[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]objectoutputstream[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]serializable[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]util[/color][color=#007700].[/color][color=#0000bb]logging[/color][color=#007700].[/color][color=#0000bb]level[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]util[/color][color=#007700].[/color][color=#0000bb]logging[/color][color=#007700].[/color][color=#0000bb]logger[/color][color=#007700]; [/color][color=#ff8000]/** * * @author duythanh */[/color][color=#007700]class [/color][color=#0000bb]thuvien [/color][color=#007700]implements [/color][color=#0000bb]serializable [/color][color=#007700]{ private [/color][color=#0000bb]string mathe[/color][color=#007700]; private [/color][color=#0000bb]string tensinhvien[/color][color=#007700]; private [/color][color=#0000bb]int sosachmuon[/color][color=#007700]; public [/color][color=#0000bb]string getmathe[/color][color=#007700]() { return [/color][color=#0000bb]mathe[/color][color=#007700]; } public [/color][color=#0000bb]void setmathe[/color][color=#007700]([/color][color=#0000bb]string mathe[/color][color=#007700]) { [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]mathe [/color][color=#007700]= [/color][color=#0000bb]mathe[/color][color=#007700]; } public [/color][color=#0000bb]string gettensinhvien[/color][color=#007700]() { return [/color][color=#0000bb]tensinhvien[/color][color=#007700]; } public [/color][color=#0000bb]void settensinhvien[/color][color=#007700]([/color][color=#0000bb]string tensinhvien[/color][color=#007700]) { [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]tensinhvien [/color][color=#007700]= [/color][color=#0000bb]tensinhvien[/color][color=#007700]; } public [/color][color=#0000bb]int getsosachmuon[/color][color=#007700]() { return [/color][color=#0000bb]sosachmuon[/color][color=#007700]; } public [/color][color=#0000bb]void setsosachmuon[/color][color=#007700]([/color][color=#0000bb]int sosachmuon[/color][color=#007700]) { [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]sosachmuon [/color][color=#007700]= [/color][color=#0000bb]sosachmuon[/color][color=#007700]; } } public class [/color][color=#0000bb]objectthuvien [/color][color=#007700]{public static [/color][color=#0000bb]void main[/color][color=#007700]([/color][color=#0000bb]string [/color][color=#007700][] [/color][color=#0000bb]args[/color][color=#007700]) [/color][color=#0000bb]throws ioexception[/color][color=#007700], [/color][color=#0000bb]classnotfoundexception[/color][color=#007700]{ [/color][color=#0000bb]thuvien tv1 [/color][color=#007700]= new [/color][color=#0000bb]thuvien[/color][color=#007700](); [/color][color=#0000bb]tv1[/color][color=#007700].[/color][color=#0000bb]settensinhvien[/color][color=#007700]([/color][color=#dd0000]"nguyễn văn a"[/color][color=#007700]); [/color][color=#0000bb]tv1[/color][color=#007700].[/color][color=#0000bb]setmathe[/color][color=#007700]([/color][color=#dd0000]"nva"[/color][color=#007700]); [/color][color=#0000bb]tv1[/color][color=#007700].[/color][color=#0000bb]setsosachmuon[/color][color=#007700]([/color][color=#0000bb]10000[/color][color=#007700]); [/color][color=#0000bb]thuvien tv2 [/color][color=#007700]= new [/color][color=#0000bb]thuvien[/color][color=#007700](); [/color][color=#0000bb]tv2[/color][color=#007700].[/color][color=#0000bb]settensinhvien[/color][color=#007700]([/color][color=#dd0000]"nguyễn văn b"[/color][color=#007700]); [/color][color=#0000bb]tv2[/color][color=#007700].[/color][color=#0000bb]setmathe[/color][color=#007700]([/color][color=#dd0000]"nvb"[/color][color=#007700]); [/color][color=#0000bb]tv2[/color][color=#007700].[/color][color=#0000bb]setsosachmuon[/color][color=#007700]([/color][color=#0000bb]70000[/color][color=#007700]); [/color][color=#0000bb]thuvien tv3 [/color][color=#007700]= new [/color][color=#0000bb]thuvien[/color][color=#007700](); [/color][color=#0000bb]tv3[/color][color=#007700].[/color][color=#0000bb]settensinhvien[/color][color=#007700]([/color][color=#dd0000]"nguyễn văn c"[/color][color=#007700]); [/color][color=#0000bb]tv3[/color][color=#007700].[/color][color=#0000bb]setmathe[/color][color=#007700]([/color][color=#dd0000]"nvc"[/color][color=#007700]); [/color][color=#0000bb]tv3[/color][color=#007700].[/color][color=#0000bb]setsosachmuon[/color][color=#007700]([/color][color=#0000bb]90000[/color][color=#007700]); try { [/color][color=#0000bb]fileoutputstream f [/color][color=#007700]= new [/color][color=#0000bb]fileoutputstream[/color][color=#007700]([/color][color=#dd0000]"d:\\thuvien.dat"[/color][color=#007700]); [/color][color=#0000bb]objectoutputstream oot [/color][color=#007700]= new [/color][color=#0000bb]objectoutputstream[/color][color=#007700]([/color][color=#0000bb]f[/color][color=#007700]); [/color][color=#0000bb]oot[/color][color=#007700].[/color][color=#0000bb]writeobject[/color][color=#007700]([/color][color=#0000bb]tv1[/color][color=#007700]); [/color][color=#0000bb]oot[/color][color=#007700].[/color][color=#0000bb]writeobject[/color][color=#007700]([/color][color=#0000bb]tv2[/color][color=#007700]); [/color][color=#0000bb]oot[/color][color=#007700].[/color][color=#0000bb]writeobject[/color][color=#007700]([/color][color=#0000bb]tv3[/color][color=#007700]); [/color][color=#0000bb]f[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); [/color][color=#0000bb]oot[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); } catch ([/color][color=#0000bb]filenotfoundexception ex[/color][color=#007700]) { [/color][color=#0000bb]logger[/color][color=#007700].[/color][color=#0000bb]getlogger[/color][color=#007700]([/color][color=#0000bb]objectthuvien[/color][color=#007700].class.[/color][color=#0000bb]getname[/color][color=#007700]()).[/color][color=#0000bb]log[/color][color=#007700]([/color][color=#0000bb]level[/color][color=#007700].[/color][color=#0000bb]severe[/color][color=#007700], [/color][color=#0000bb]null[/color][color=#007700], [/color][color=#0000bb]ex[/color][color=#007700]); } [/color][color=#0000bb]thuvien a1 [/color][color=#007700]= new [/color][color=#0000bb]thuvien[/color][color=#007700](); [/color][color=#0000bb]thuvien a2 [/color][color=#007700]= new [/color][color=#0000bb]thuvien[/color][color=#007700](); [/color][color=#0000bb]thuvien a3 [/color][color=#007700]= new [/color][color=#0000bb]thuvien[/color][color=#007700](); [/color][color=#0000bb]fileinputstream f [/color][color=#007700]= new [/color][color=#0000bb]fileinputstream[/color][color=#007700]([/color][color=#dd0000]"d:\\thuvien.dat"[/color][color=#007700]); [/color][color=#0000bb]objectinputstream oit [/color][color=#007700]= new [/color][color=#0000bb]objectinputstream[/color][color=#007700]([/color][color=#0000bb]f[/color][color=#007700]); [/color][color=#0000bb]a1 [/color][color=#007700]= ([/color][color=#0000bb]thuvien[/color][color=#007700]) [/color][color=#0000bb]oit[/color][color=#007700].[/color][color=#0000bb]readobject[/color][color=#007700](); [/color][color=#0000bb]a2 [/color][color=#007700]= ([/color][color=#0000bb]thuvien[/color][color=#007700]) [/color][color=#0000bb]oit[/color][color=#007700].[/color][color=#0000bb]readobject[/color][color=#007700](); [/color][color=#0000bb]a3 [/color][color=#007700]= ([/color][color=#0000bb]thuvien[/color][color=#007700]) [/color][color=#0000bb]oit[/color][color=#007700].[/color][color=#0000bb]readobject[/color][color=#007700](); [/color][color=#0000bb]f[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); [/color][color=#0000bb]oit[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"the thu vien 1: " [/color][color=#007700]+ [/color][color=#0000bb]a1[/color][color=#007700].[/color][color=#0000bb]gettensinhvien[/color][color=#007700]() + [/color][color=#dd0000]". ma: " [/color][color=#007700]+ [/color][color=#0000bb]a1[/color][color=#007700].[/color][color=#0000bb]getmathe[/color][color=#007700]() + [/color][color=#dd0000]". so sach muon: " [/color][color=#007700]+ [/color][color=#0000bb]a1[/color][color=#007700].[/color][color=#0000bb]getsosachmuon[/color][color=#007700]()); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"the thu vien 1: " [/color][color=#007700]+ [/color][color=#0000bb]a2[/color][color=#007700].[/color][color=#0000bb]gettensinhvien[/color][color=#007700]() + [/color][color=#dd0000]". ma: " [/color][color=#007700]+ [/color][color=#0000bb]a2[/color][color=#007700].[/color][color=#0000bb]getmathe[/color][color=#007700]() + [/color][color=#dd0000]". so sach muon: " [/color][color=#007700]+ [/color][color=#0000bb]a2[/color][color=#007700].[/color][color=#0000bb]getsosachmuon[/color][color=#007700]()); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"the thu vien 1: " [/color][color=#007700]+ [/color][color=#0000bb]a3[/color][color=#007700].[/color][color=#0000bb]gettensinhvien[/color][color=#007700]() + [/color][color=#dd0000]". ma: " [/color][color=#007700]+ [/color][color=#0000bb]a3[/color][color=#007700].[/color][color=#0000bb]getmathe[/color][color=#007700]() + [/color][color=#dd0000]". so sach muon: " [/color][color=#007700]+ [/color][color=#0000bb]a3[/color][color=#007700].[/color][color=#0000bb]getsosachmuon[/color][color=#007700]());} } [/color] 
    </blockquote></div>
    </div>
    </div>

  10. #10
    Ngày tham gia
    Jan 2015
    Bài viết
    0
    trong phần đọc đối tượng nếu trong file có bao nhiêu đối tượng thì lại phải tạo bấy nhiêu đối tượng mới à. có cách nào chỉ phải tạo 1 đối tượng mới sau đó đọc tuần tự file gán giá trị cho đối tượng rồi in ra màn hình không

Trang 1 của 2 12 CuốiCuối

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •