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

    Bài 35: ArrayList trong Java (2)

    /* 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/arraylistwith5points-png.22937/" border="0" alt="" />


    ở bài viết này, mình sẽ chia sẽ về cách lưu lại dữ liệu khi trong chương trình sử dụng arraylist.
    thực ra, kết hợp với bài trước, bài đọc ghi object, sẽ có nhiều bạn làm được điều này ngay. nhưng mình sẽ viết để cho một số bạn cảm thấy khó khăn tham khảo luôn. mình sẽ chú thích trong code!
    về cách lưu lên file ở chương trình có dùng arraylist, mình dùng 2 cách như sau

    cách 1: lưu trữ các đối tượng trong một đối tượng arraylist xuống file!
    chương trình dưới đây sẽ lưu toàn bộ các đối tượng học sinh xuống file, để thao tác, ta nạp các đối tượng này và 1 đối tượng arraylist


    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]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]arraylist[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]util[/color][color=#007700].[/color][color=#0000bb]scanner[/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]; class [/color][color=#0000bb]hocsinh [/color][color=#007700]implements [/color][color=#0000bb]serializable [/color][color=#007700]{ private [/color][color=#0000bb]string ten[/color][color=#007700]; private [/color][color=#0000bb]int tuoi[/color][color=#007700]; public [/color][color=#0000bb]string getten[/color][color=#007700]() { return [/color][color=#0000bb]ten[/color][color=#007700]; } public [/color][color=#0000bb]void setten[/color][color=#007700]([/color][color=#0000bb]string ten[/color][color=#007700]) { [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]ten [/color][color=#007700]= [/color][color=#0000bb]ten[/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]arraylist[/color][color=#007700]<[/color][color=#0000bb]hocsinh[/color][color=#007700]> [/color][color=#0000bb]lisths [/color][color=#007700]= new [/color][color=#0000bb]arraylist[/color][color=#007700](); [/color][color=#ff8000]// khai báo 1 đối tượng arraylist, các phần tử tạo ra từ lớp hocsinh [/color][color=#0000bb]scanner input [/color][color=#007700]= new [/color][color=#0000bb]scanner[/color][color=#007700]([/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]in[/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]"nhập số học sinh: "[/color][color=#007700]); [/color][color=#0000bb]int n [/color][color=#007700]= [/color][color=#0000bb]input[/color][color=#007700].[/color][color=#0000bb]nextint[/color][color=#007700](); [/color][color=#ff8000]//lấy dữ liệu và ghi vào file [/color][color=#007700]try { [/color][color=#0000bb]fileoutputstream f [/color][color=#007700]= new [/color][color=#0000bb]fileoutputstream[/color][color=#007700]([/color][color=#dd0000]"e:\\hocsinh.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=#007700]for ([/color][color=#0000bb]int i [/color][color=#007700]= [/color][color=#0000bb]0[/color][color=#007700]; [/color][color=#0000bb]i [/color][color=#007700]< [/color][color=#0000bb]n[/color][color=#007700]; [/color][color=#0000bb]i[/color][color=#007700]++) { [/color][color=#0000bb]hocsinh x [/color][color=#007700]= new [/color][color=#0000bb]hocsinh[/color][color=#007700](); [/color][color=#ff8000]//tạo đối tượng x để lưu tạm thời dữ liệu [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"học sinh thứ " [/color][color=#007700]+ [/color][color=#0000bb]i [/color][color=#007700]+ [/color][color=#dd0000]": "[/color][color=#007700]); [/color][color=#0000bb]input[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700](); [/color][color=#ff8000]//dòng này để tránh bị trượt dòng! [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#dd0000]"tên: "[/color][color=#007700]); [/color][color=#0000bb]string tenx [/color][color=#007700]= [/color][color=#0000bb]input[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700](); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#dd0000]"tuổi: "[/color][color=#007700]); [/color][color=#0000bb]int tuoix [/color][color=#007700]= [/color][color=#0000bb]input[/color][color=#007700].[/color][color=#0000bb]nextint[/color][color=#007700](); [/color][color=#0000bb]x[/color][color=#007700].[/color][color=#0000bb]setten[/color][color=#007700]([/color][color=#0000bb]tenx[/color][color=#007700]); [/color][color=#0000bb]x[/color][color=#007700].[/color][color=#0000bb]settuoi[/color][color=#007700]([/color][color=#0000bb]tuoix[/color][color=#007700]); [/color][color=#0000bb]oot[/color][color=#007700].[/color][color=#0000bb]writeobject[/color][color=#007700]([/color][color=#0000bb]x[/color][color=#007700]); [/color][color=#ff8000]// ghi object là đối tượng x xuống file [/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]javaandroidvn[/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=#ff8000]//đọc dữ liệu từ file, lấy các object ra rồi gán vào lisths [/color][color=#007700]try { [/color][color=#0000bb]fileinputstream f2 [/color][color=#007700]= new [/color][color=#0000bb]fileinputstream[/color][color=#007700]([/color][color=#dd0000]"e:\\hocsinh.dat"[/color][color=#007700]); [/color][color=#0000bb]objectinputstream oit [/color][color=#007700]= new [/color][color=#0000bb]objectinputstream[/color][color=#007700]([/color][color=#0000bb]f2[/color][color=#007700]); [/color][color=#ff8000]// sử dụng để đọc file theo từng object [/color][color=#0000bb]hocsinh x [/color][color=#007700]= new [/color][color=#0000bb]hocsinh[/color][color=#007700](); [/color][color=#ff8000]//tạo đối tượng x để lưu tạm thời dữ liệu [/color][color=#007700]for ([/color][color=#0000bb]int i [/color][color=#007700]= [/color][color=#0000bb]0[/color][color=#007700]; [/color][color=#0000bb]i [/color][color=#007700]< [/color][color=#0000bb]n[/color][color=#007700]; [/color][color=#0000bb]i[/color][color=#007700]++) { [/color][color=#0000bb]x [/color][color=#007700]= ([/color][color=#0000bb]hocsinh[/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]lisths[/color][color=#007700].[/color][color=#0000bb]add[/color][color=#007700]([/color][color=#0000bb]x[/color][color=#007700]); } [/color][color=#0000bb]oit[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); [/color][color=#0000bb]f2[/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=#ff8000]//in thông tin ra ngoài! [/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 danh sách vừa nhập vào là: "[/color][color=#007700]); for ([/color][color=#0000bb]int i [/color][color=#007700]= [/color][color=#0000bb]0[/color][color=#007700]; [/color][color=#0000bb]i [/color][color=#007700]< [/color][color=#0000bb]lisths[/color][color=#007700].[/color][color=#0000bb]size[/color][color=#007700](); [/color][color=#0000bb]i[/color][color=#007700]++) { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#dd0000]"học sinh thứ " [/color][color=#007700]+ [/color][color=#0000bb]i[/color][color=#007700]); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#dd0000]" - tên " [/color][color=#007700]+ [/color][color=#0000bb]lisths[/color][color=#007700].[/color][color=#0000bb]get[/color][color=#007700]([/color][color=#0000bb]i[/color][color=#007700]).[/color][color=#0000bb]getten[/color][color=#007700]() + [/color][color=#dd0000]" tuổi: " [/color][color=#007700]+ [/color][color=#0000bb]lisths[/color][color=#007700].[/color][color=#0000bb]get[/color][color=#007700]([/color][color=#0000bb]i[/color][color=#007700]).[/color][color=#0000bb]gettuoi[/color][color=#007700]() + [/color][color=#dd0000]"
    "[/color][color=#007700]); } }}[/color] 
    cách 2: lưu đối tượng arraylist xuống file
    vì bản thân lisths là 1 đối tượng, nên ngắn gọn ta chỉ cần lưu trữ đối tượng lisths là được!


    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]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]arraylist[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]util[/color][color=#007700].[/color][color=#0000bb]scanner[/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]; class [/color][color=#0000bb]hocsinh [/color][color=#007700]implements [/color][color=#0000bb]serializable [/color][color=#007700]{ private [/color][color=#0000bb]string ten[/color][color=#007700]; private [/color][color=#0000bb]int tuoi[/color][color=#007700]; public [/color][color=#0000bb]string getten[/color][color=#007700]() { return [/color][color=#0000bb]ten[/color][color=#007700]; } public [/color][color=#0000bb]void setten[/color][color=#007700]([/color][color=#0000bb]string ten[/color][color=#007700]) { [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]ten [/color][color=#007700]= [/color][color=#0000bb]ten[/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]arraylist[/color][color=#007700]<[/color][color=#0000bb]hocsinh[/color][color=#007700]> [/color][color=#0000bb]lisths [/color][color=#007700]= new [/color][color=#0000bb]arraylist[/color][color=#007700](); [/color][color=#ff8000]// khai báo 1 đối tượng arraylist, các phần tử tạo ra từ lớp hocsinh [/color][color=#0000bb]scanner input [/color][color=#007700]= new [/color][color=#0000bb]scanner[/color][color=#007700]([/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]in[/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]"nhập số học sinh: "[/color][color=#007700]); [/color][color=#0000bb]int n [/color][color=#007700]= [/color][color=#0000bb]input[/color][color=#007700].[/color][color=#0000bb]nextint[/color][color=#007700](); [/color][color=#ff8000]//lấy dữ liệu và ghi vào file [/color][color=#007700]for ([/color][color=#0000bb]int i [/color][color=#007700]= [/color][color=#0000bb]0[/color][color=#007700]; [/color][color=#0000bb]i [/color][color=#007700]< [/color][color=#0000bb]n[/color][color=#007700]; [/color][color=#0000bb]i[/color][color=#007700]++) { [/color][color=#0000bb]hocsinh x [/color][color=#007700]= new [/color][color=#0000bb]hocsinh[/color][color=#007700](); [/color][color=#ff8000]//tạo đối tượng x để lưu tạm thời dữ liệu [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"học sinh thứ " [/color][color=#007700]+ [/color][color=#0000bb]i [/color][color=#007700]+ [/color][color=#dd0000]": "[/color][color=#007700]); [/color][color=#0000bb]input[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700](); [/color][color=#ff8000]//dòng này để tránh bị trượt dòng! [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#dd0000]"tên: "[/color][color=#007700]); [/color][color=#0000bb]string tenx [/color][color=#007700]= [/color][color=#0000bb]input[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700](); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#dd0000]"tuổi: "[/color][color=#007700]); [/color][color=#0000bb]int tuoix [/color][color=#007700]= [/color][color=#0000bb]input[/color][color=#007700].[/color][color=#0000bb]nextint[/color][color=#007700](); [/color][color=#0000bb]x[/color][color=#007700].[/color][color=#0000bb]setten[/color][color=#007700]([/color][color=#0000bb]tenx[/color][color=#007700]); [/color][color=#0000bb]x[/color][color=#007700].[/color][color=#0000bb]settuoi[/color][color=#007700]([/color][color=#0000bb]tuoix[/color][color=#007700]); [/color][color=#0000bb]lisths[/color][color=#007700].[/color][color=#0000bb]add[/color][color=#007700]([/color][color=#0000bb]x[/color][color=#007700]); } try { [/color][color=#0000bb]fileoutputstream f [/color][color=#007700]= new [/color][color=#0000bb]fileoutputstream[/color][color=#007700]([/color][color=#dd0000]"e:\\hocsinh.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]lisths[/color][color=#007700]); [/color][color=#ff8000]// ghi object là đối tượng x xuống file [/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]javaandroidvn[/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=#ff8000]//đọc dữ liệu từ file, lấy các object ra rồi gán vào lisths [/color][color=#0000bb]arraylist[/color][color=#007700]<[/color][color=#0000bb]hocsinh[/color][color=#007700]> [/color][color=#0000bb]lisths2 [/color][color=#007700]= new [/color][color=#0000bb]arraylist[/color][color=#007700](); [/color][color=#ff8000]// khai báo 1 đối tượng arraylist thứ 2, các phần tử tạo ra từ lớp hocsinh //dùng để lưu dữ liệu riêng khi ta đọc file! [/color][color=#007700]try { [/color][color=#0000bb]fileinputstream f2 [/color][color=#007700]= new [/color][color=#0000bb]fileinputstream[/color][color=#007700]([/color][color=#dd0000]"e:\\hocsinh.dat"[/color][color=#007700]); [/color][color=#0000bb]objectinputstream oit [/color][color=#007700]= new [/color][color=#0000bb]objectinputstream[/color][color=#007700]([/color][color=#0000bb]f2[/color][color=#007700]); [/color][color=#ff8000]// sử dụng để đọc file theo từng object [/color][color=#0000bb]lisths2 [/color][color=#007700]= ([/color][color=#0000bb]arraylist[/color][color=#007700]<[/color][color=#0000bb]hocsinh[/color][color=#007700]>) [/color][color=#0000bb]oit[/color][color=#007700].[/color][color=#0000bb]readobject[/color][color=#007700](); [/color][color=#ff8000]//ép kiểu đối tượng lấy từ file ra về dạng arraylist<hocsinh> [/color][color=#0000bb]oit[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); [/color][color=#0000bb]f2[/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=#ff8000]//in thông tin ra ngoài! thông tin lấy từ đối tượng lisths2 vừa đọc từ file [/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 danh sách vừa nhập vào là: "[/color][color=#007700]); for ([/color][color=#0000bb]int i [/color][color=#007700]= [/color][color=#0000bb]0[/color][color=#007700]; [/color][color=#0000bb]i [/color][color=#007700]< [/color][color=#0000bb]lisths2[/color][color=#007700].[/color][color=#0000bb]size[/color][color=#007700](); [/color][color=#0000bb]i[/color][color=#007700]++) { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#dd0000]"học sinh thứ " [/color][color=#007700]+ [/color][color=#0000bb]i[/color][color=#007700]); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#dd0000]" - tên " [/color][color=#007700]+ [/color][color=#0000bb]lisths2[/color][color=#007700].[/color][color=#0000bb]get[/color][color=#007700]([/color][color=#0000bb]i[/color][color=#007700]).[/color][color=#0000bb]getten[/color][color=#007700]() + [/color][color=#dd0000]" tuổi: " [/color][color=#007700]+ [/color][color=#0000bb]lisths2[/color][color=#007700].[/color][color=#0000bb]get[/color][color=#007700]([/color][color=#0000bb]i[/color][color=#007700]).[/color][color=#0000bb]gettuoi[/color][color=#007700]() + [/color][color=#dd0000]"
    "[/color][color=#007700]); } }}[/color] 
    bài tập về nhà::d
    làm chương trình từ điển. danh sách từ là danh sách các đối tượng gồm 2 thuộc tính từ và nghĩa
    số từ nhập vào từ màn hình. nhập dữ liệu các từ qua màn hình console. khi nhập dữ liệu xong, toàn bộ dữ liệu được lưu vào file theo kiểu object.
    sau đó chương trình hỏi từ cần tìm, nhập từ cần tìm. chương trình đọc file vừa ghi, rồi tìm kiếm nghĩa của từ vừa nhập, in ra nghĩa của từ cần tra cứu!

    (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
    Oct 2015
    Bài viết
    257
    rất hay. cám ơn bạn nhìu
    đây là bài của mình. bạn check hộ cái nha


    Mã nguồn PHP:
    [color=#000000]
    package com[/color][color=#007700].[/color][color=#0000bb]example[/color][color=#007700].[/color][color=#0000bb]test[/color][color=#007700]; public class [/color][color=#0000bb]tudienanh_viet [/color][color=#007700]{ private [/color][color=#0000bb]string tu[/color][color=#007700]; private [/color][color=#0000bb]string nghia[/color][color=#007700]; public [/color][color=#0000bb]tudienanh_viet[/color][color=#007700]([/color][color=#0000bb]string tu[/color][color=#007700], [/color][color=#0000bb]string nghia[/color][color=#007700]){ [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]tu [/color][color=#007700]= [/color][color=#dd0000]""[/color][color=#007700]; [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]nghia [/color][color=#007700]= [/color][color=#dd0000]""[/color][color=#007700]; } public [/color][color=#0000bb]string gettu[/color][color=#007700](){ return [/color][color=#0000bb]tu[/color][color=#007700]; } public [/color][color=#0000bb]string getnghia[/color][color=#007700](){ return [/color][color=#0000bb]nghia[/color][color=#007700]; } public [/color][color=#0000bb]void settu[/color][color=#007700]([/color][color=#0000bb]string tu[/color][color=#007700]){ [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]tu [/color][color=#007700]= [/color][color=#0000bb]tu[/color][color=#007700]; } public [/color][color=#0000bb]void setnghia[/color][color=#007700]([/color][color=#0000bb]string nghia[/color][color=#007700]){ [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]nghia [/color][color=#007700]= [/color][color=#0000bb]nghia[/color][color=#007700]; } }[/color] 
    Mã nguồn PHP:
    [color=#000000]
    package com[/color][color=#007700].[/color][color=#0000bb]example[/color][color=#007700].[/color][color=#0000bb]test[/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]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]util[/color][color=#007700].*; public class [/color][color=#0000bb]main_tudien [/color][color=#007700]{ public static [/color][color=#0000bb]scanner sc[/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]arraylist[/color][color=#007700]<[/color][color=#0000bb]tudienanh_viet[/color][color=#007700]> [/color][color=#0000bb]listtd [/color][color=#007700]= new [/color][color=#0000bb]arraylist[/color][color=#007700]<[/color][color=#0000bb]tudienanh_viet[/color][color=#007700]>(); [/color][color=#0000bb]sc [/color][color=#007700]= new [/color][color=#0000bb]scanner[/color][color=#007700]([/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]in[/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]"nhap so tu: "[/color][color=#007700]); [/color][color=#0000bb]int input [/color][color=#007700]= [/color][color=#0000bb]sc[/color][color=#007700].[/color][color=#0000bb]nextint[/color][color=#007700](); for ([/color][color=#0000bb]int i [/color][color=#007700]= [/color][color=#0000bb]0[/color][color=#007700]; [/color][color=#0000bb]i [/color][color=#007700]< [/color][color=#0000bb]input[/color][color=#007700]; [/color][color=#0000bb]i[/color][color=#007700]++) { [/color][color=#0000bb]tudienanh_viet td [/color][color=#007700]= new [/color][color=#0000bb]tudienanh_viet[/color][color=#007700]([/color][color=#dd0000]""[/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]"nhap tu thu " [/color][color=#007700]+ ([/color][color=#0000bb]i [/color][color=#007700]+ [/color][color=#0000bb]1[/color][color=#007700]) + [/color][color=#dd0000]": "[/color][color=#007700]); [/color][color=#0000bb]sc[/color][color=#007700].[/color][color=#0000bb]nextline[/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]"tu: "[/color][color=#007700]); [/color][color=#0000bb]string nametu [/color][color=#007700]= [/color][color=#0000bb]sc[/color][color=#007700].[/color][color=#0000bb]nextline[/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]"nghia: "[/color][color=#007700]); [/color][color=#0000bb]string namenghia [/color][color=#007700]= [/color][color=#0000bb]sc[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700](); [/color][color=#0000bb]td[/color][color=#007700].[/color][color=#0000bb]settu[/color][color=#007700]([/color][color=#0000bb]nametu[/color][color=#007700]); [/color][color=#0000bb]td[/color][color=#007700].[/color][color=#0000bb]setnghia[/color][color=#007700]([/color][color=#0000bb]namenghia[/color][color=#007700]); [/color][color=#0000bb]listtd[/color][color=#007700].[/color][color=#0000bb]add[/color][color=#007700]([/color][color=#0000bb]td[/color][color=#007700]); } try { [/color][color=#0000bb]fileoutputstream f [/color][color=#007700]= new [/color][color=#0000bb]fileoutputstream[/color][color=#007700]([/color][color=#dd0000]"tu dien.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]listtd[/color][color=#007700]); } catch ([/color][color=#0000bb]exception 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]"loi 1"[/color][color=#007700]); } [/color][color=#ff8000]// system.out.println("thong tin vua nhap la:"); // for (int i = 0; i < input; i++) { // system.out.println("tu thu " + (i + 1) + ": "); // system.out.println("tu: " + listtd.get(i).gettu() // + "\tco nghia la: " + listtd.get(i).getnghia()); // } [/color][color=#0000bb]arraylist[/color][color=#007700]<[/color][color=#0000bb]tudienanh_viet[/color][color=#007700]> [/color][color=#0000bb]listtd2 [/color][color=#007700]= new [/color][color=#0000bb]arraylist[/color][color=#007700]<[/color][color=#0000bb]tudienanh_viet[/color][color=#007700]>(); try { [/color][color=#0000bb]fileinputstream f2 [/color][color=#007700]= new [/color][color=#0000bb]fileinputstream[/color][color=#007700]([/color][color=#dd0000]"tu dien.dat"[/color][color=#007700]); [/color][color=#0000bb]objectinputstream ois [/color][color=#007700]= new [/color][color=#0000bb]objectinputstream[/color][color=#007700]([/color][color=#0000bb]f2[/color][color=#007700]); [/color][color=#0000bb]listtd2 [/color][color=#007700]= ([/color][color=#0000bb]arraylist[/color][color=#007700]<[/color][color=#0000bb]tudienanh_viet[/color][color=#007700]>) [/color][color=#0000bb]ois[/color][color=#007700].[/color][color=#0000bb]readobject[/color][color=#007700](); [/color][color=#0000bb]ois[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); [/color][color=#0000bb]f2[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); } catch ([/color][color=#0000bb]exception 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]"loi 2"[/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]"trong file co: "[/color][color=#007700]); for ([/color][color=#0000bb]int i [/color][color=#007700]= [/color][color=#0000bb]0[/color][color=#007700]; [/color][color=#0000bb]i [/color][color=#007700]< [/color][color=#0000bb]listtd2[/color][color=#007700].[/color][color=#0000bb]size[/color][color=#007700](); [/color][color=#0000bb]i[/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]"tu thu: " [/color][color=#007700]+ ([/color][color=#0000bb]i [/color][color=#007700]+ [/color][color=#0000bb]1[/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]"tu: " [/color][color=#007700]+ [/color][color=#0000bb]listtd2[/color][color=#007700].[/color][color=#0000bb]get[/color][color=#007700]([/color][color=#0000bb]i[/color][color=#007700]).[/color][color=#0000bb]gettu[/color][color=#007700]() + [/color][color=#dd0000]"\tco nghia la: " [/color][color=#007700]+ [/color][color=#0000bb]listtd2[/color][color=#007700].[/color][color=#0000bb]get[/color][color=#007700]([/color][color=#0000bb]i[/color][color=#007700]).[/color][color=#0000bb]getnghia[/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]"nhap tu muon tim "[/color][color=#007700]); [/color][color=#0000bb]string namesearchtu [/color][color=#007700]= [/color][color=#0000bb]sc[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700](); for ([/color][color=#0000bb]int i [/color][color=#007700]= [/color][color=#0000bb]0[/color][color=#007700]; [/color][color=#0000bb]i [/color][color=#007700]< [/color][color=#0000bb]input[/color][color=#007700]; [/color][color=#0000bb]i[/color][color=#007700]++) { if ([/color][color=#0000bb]listtd2[/color][color=#007700].[/color][color=#0000bb]get[/color][color=#007700]([/color][color=#0000bb]i[/color][color=#007700]).[/color][color=#0000bb]gettu[/color][color=#007700]().[/color][color=#0000bb]equals[/color][color=#007700]([/color][color=#0000bb]namesearchtu[/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]"nghia cua tu nay la: " [/color][color=#007700]+ [/color][color=#0000bb]listtd2[/color][color=#007700].[/color][color=#0000bb]get[/color][color=#007700]([/color][color=#0000bb]i[/color][color=#007700]).[/color][color=#0000bb]getnghia[/color][color=#007700]()); } } }}[/color] 

  3. #3
    Ngày tham gia
    Mar 2014
    Bài viết
    0
    cảm ơn rất nhiều ! hết sức cơ bản và dễ hiểu

  4. #4
    Ngày tham gia
    Jan 2015
    Bài viết
    0
    cảm ơn rất nhiều ! hết sức cơ bản và dễ hiểu

  5. #5
    Ngày tham gia
    Aug 2015
    Bài viết
    1
    <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.filenotfoundexception;
    import java.io.fileoutputstream;
    import java.io.ioexception;
    import java.io.objectinputstream;
    import java.io.objectoutputstream;
    import java.io.serializable;
    import java.util.arraylist;
    import java.util.scanner;
    import java.util.logging.level;
    import java.util.logging.logger;
     
    class tu implements serializable{
        private string tu;
        private string nghia;
        public string gettu() {
            return tu;
        }
        public void settu(string tu) {
            this.tu = tu;
        }
        public string getnghia() {
            return nghia;
        }
        public void setnghia(string nghia) {
            this.nghia = nghia;
        }
       
    }
    public class btbai34 {
        public static void main(string[]args) throws ioexception{
            arraylist<tu> arr=new arraylist();
            scanner in=new scanner(system.in);
            int n;
            system.out.println("nhap so tu:");
            n=in.nextint();
            in.nextline();
            int i;
            string tu,nghia;
            for(i=0;i<n;i++)
            {
                tu x=new tu();
                system.out.println("nhap tt tu thu "+i);
                system.out.println("nhap tu: ");
                tu=in.nextline();
                system.out.println("nhap nghia");
                nghia=in.nextline();
                x.settu(tu);
                x.setnghia(nghia);
                arr.add(x);
            }
            //ghi file
            try {
                fileoutputstream f = new fileoutputstream("d:\\hocsinh.txt");
                objectoutputstream oot = new objectoutputstream(f);
                oot.writeobject(arr);
            } catch (filenotfoundexception ex) {
                logger.getlogger(vdbai35b.class.getname()).log(level.severe, null, ex);
            }
            // nhap tu can tk
            string s;
            system.out.println("nhap tu can tim kiem: ");
            s=in.nextline();
            //đọc dữ liệu từ file, lấy các object ra rồi gán vào arr
     
            arraylist<tu> arrb = new arraylist(); // khai báo 1 đối tượng arraylist thứ 2, các phần tử tạo ra từ lớp hocsinh
            //dùng để lưu dữ liệu riêng khi ta đọc file!                 
            try {
                fileinputstream f2 = new fileinputstream("d:\\hocsinh.txt");
                objectinputstream oit = new objectinputstream(f2); // sử dụng để đọc file theo từng object
                arrb = (arraylist<tu>) oit.readobject();
                oit.close();
                f2.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");
            }
     
            /*//in thông tin ra ngoài! thông tin lấy từ đối tượng arrb vừa đọc từ file
            system.out.println("thông tin tu dien  là: ");
            for (i = 0; i < arrb.size(); i++) {
                system.out.println(arrb.get(i).gettu() + " ie: " + arrb.get(i).getnghia() );
            }*/
            //tim kiem tu
            int dem=0;
            for(i=0;i<arrb.size();i++)
            {
                if(s.comparetoignorecase(arrb.get(i).gettu())==0)
                {
                    dem++;
                    system.out.println(" nghia cua tu do la: "+arrb.get(i).getnghia());
                }
            }
            if(dem==0)
            {
                system.out.println(" khong co tu nay trong tu dien.");
            }
           
        }
    }
    </blockquote></div>
    </div>
    </div>

  6. #6
    Ngày tham gia
    Aug 2014
    Bài viết
    0
    bài của mình :d
    <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ã:
    /**
    *
    */
    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.arraylist;
    import java.util.scanner;
     
    /**
    * @author chienbk
    *
    */
    class newword implements serializable {
        private string eng;
        private string vn;
        public string geteng() {
            return eng;
        }
        public void seteng(string eng) {
            this.eng = eng;
        }
        public string getvn() {
            return vn;
        }
        public void setvn(string vn) {
            this.vn = vn;
        }
       
    }
     
     
    public class arraylist2 {
     
        /**
        * làm chương trình từ điển.
        * danh sách từ là danh sách các đối tượng gồm 2 thuộc tính từ và nghĩa
        * số từ nhập vào từ màn hình.
        * nhập dữ liệu các từ qua màn hình console.
        * khi nhập dữ liệu xong, toàn bộ dữ liệu được lưu vào file theo kiểu object.
        * sau đó chương trình hỏi từ cần tìm, nhập từ cần tìm.
        * chương trình đọc file vừa ghi, rồi tìm kiếm nghĩa của từ vừa nhập, in ra nghĩa của từ cần tra cứu!
        */
       
        public static int noword;
        public void addword(){
            scanner sc = new scanner(system.in);
            try {
                fileoutputstream f = new fileoutputstream("e:\\td.txt");
                objectoutputstream oos = new objectoutputstream(f);
                system.out.print("input your number new word: ");
                noword = sc.nextint();
                sc.nextline();
                for (int i = 0; i < noword; i++) {
                    newword nw = new newword();
                    system.out.print("english: ");
                    nw.seteng(sc.nextline());
                    system.out.print("vietnamese: ");
                    nw.setvn(sc.nextline());
                    oos.writeobject(nw);
                   
                }
                oos.close();
                f.close();
            } catch (ioexception e) {
                // todo: handle exception
                system.out.println("error!");
            }
        }
        public void seachword(){
            arraylist<newword> arr = new arraylist<>();
            string seach;
            scanner sc = new scanner(system.in);
            try {
                fileinputstream f = new fileinputstream("e:\\td.txt");
                objectinputstream ois = new objectinputstream(f);
                for (int i = 0; i < noword; i++) {
                    newword nw = new newword();
                    nw = (newword) ois.readobject();
                    arr.add(nw);
                }
                ois.close();
                f.close();
            } catch (ioexception e) {
                // todo: handle exception
                system.out.println("error!");
            }catch(classnotfoundexception e){
                system.out.println("not found class!");
            }
            //search for word
            system.out.print("input your word you wuant seach: ");
            seach = sc.nextline();
            for (int i = 0; i < arr.size(); i++) {
                if (seach.equals(arr.get(i).geteng())) {
                    system.out.println("vietnamese: "+ arr.get(i).getvn());
                    break;
                } else {
                    system.out.println("not word!");
     
                }
            }
        }
       
        public static void main(string[] args) throws ioexception{
            arraylist2 arrl2 = new arraylist2();
            system.out.println("dictionary eng - viet");
            int choice =0;
            scanner scc = new scanner(system.in);
            do {
                system.out.println("1. add new word!");
                system.out.println("2. seach for word");
                system.out.println("3. exit!");
                system.out.print("input your choice: ");
                choice = scc.nextint();
                switch (choice) {
                case 1:
                    arrl2.addword();
                    break;
                case 2:
                    arrl2.seachword();
                    break;
                case 3:
                   
                    break;
     
                default:
                    system.out.println("not correct! please try again!");
                    break;
                }
               
            } while (choice!=3);
        }
     
    }
    </blockquote></div>
    </div>
    </div>

  7. #7
    Ngày tham gia
    Jun 2014
    Bài viết
    0
    tiasangmoi92 cho mình hỏi là tại sao khi mình ghi vào file.dat mà khi mở bằng wordpad lại bị lỗi phông chữ thế?thanks

  8. #8
    Ngày tham gia
    Dec 2014
    Bài viết
    0
    Trích dẫn Gửi bởi manhdinh
    tiasangmoi92 cho mình hỏi là tại sao khi mình ghi vào file.dat mà khi mở bằng wordpad lại bị lỗi phông chữ thế?thanks
    à bạn ơi, ghi object là mã hóa rồi, nên không xem được thông tin bằng các phần mềm thông thường đâu, trừ khi là ghi dạng văn bản, bạn phải dùng chính chương trình java bạn viết, mở đúng định dạng object bạn quy định mới hiển thị được ^^

  9. #9
    Ngày tham gia
    Jul 2014
    Bài viết
    0
    bạn ơi, bạn xem hộ mình bài làm của mình ấy, tại sao khi tìm đc mình dùng break để thoát vòng lặp rồi mà nó lại in ra not word trong câu lệnh else nhỉ

  10. #10
    Ngày tham gia
    Jul 2015
    Bài viết
    0
    bài của mình đơn giản chỉ có thế này thôi: :d

    <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]
    package com[/color][color=#007700].[/color][color=#0000bb]hanhnv[/color][color=#007700].[/color][color=#0000bb]bai35[/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]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]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]arraylist[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]util[/color][color=#007700].[/color][color=#0000bb]scanner[/color][color=#007700]; public class [/color][color=#0000bb]homeworkunit35 [/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]arraylist[/color][color=#007700]<[/color][color=#0000bb]vocabulary[/color][color=#007700]> [/color][color=#0000bb]listvocin [/color][color=#007700]= new [/color][color=#0000bb]arraylist[/color][color=#007700](); [/color][color=#0000bb]scanner s [/color][color=#007700]= new [/color][color=#0000bb]scanner[/color][color=#007700]([/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]in[/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]"enter number of vocabulary you want to insert: "[/color][color=#007700]); [/color][color=#0000bb]int n [/color][color=#007700]= [/color][color=#0000bb]s[/color][color=#007700].[/color][color=#0000bb]nextint[/color][color=#007700](); [/color][color=#0000bb]s[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700](); for ([/color][color=#0000bb]int i [/color][color=#007700]= [/color][color=#0000bb]0[/color][color=#007700]; [/color][color=#0000bb]i [/color][color=#007700]< [/color][color=#0000bb]n[/color][color=#007700]; [/color][color=#0000bb]i[/color][color=#007700]++) { [/color][color=#0000bb]vocabulary vocin [/color][color=#007700]= new [/color][color=#0000bb]vocabulary[/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]"please enter the word number " [/color][color=#007700]+ ([/color][color=#0000bb]i [/color][color=#007700]+ [/color][color=#0000bb]1[/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]"enter word: "[/color][color=#007700]); [/color][color=#0000bb]string w [/color][color=#007700]= [/color][color=#0000bb]s[/color][color=#007700].[/color][color=#0000bb]nextline[/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]"enter mean: "[/color][color=#007700]); [/color][color=#0000bb]string m [/color][color=#007700]= [/color][color=#0000bb]s[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700](); [/color][color=#0000bb]vocin[/color][color=#007700].[/color][color=#0000bb]setword[/color][color=#007700]([/color][color=#0000bb]w[/color][color=#007700]); [/color][color=#0000bb]vocin[/color][color=#007700].[/color][color=#0000bb]setmean[/color][color=#007700]([/color][color=#0000bb]m[/color][color=#007700]); [/color][color=#0000bb]listvocin[/color][color=#007700].[/color][color=#0000bb]add[/color][color=#007700]([/color][color=#0000bb]vocin[/color][color=#007700]); } try { [/color][color=#0000bb]fileoutputstream fo [/color][color=#007700]= new [/color][color=#0000bb]fileoutputstream[/color][color=#007700]([/color][color=#dd0000]"d:\\listvocabulary.txt"[/color][color=#007700]); [/color][color=#0000bb]objectoutputstream oos [/color][color=#007700]= new [/color][color=#0000bb]objectoutputstream[/color][color=#007700]([/color][color=#0000bb]fo[/color][color=#007700]); [/color][color=#0000bb]oos[/color][color=#007700].[/color][color=#0000bb]writeobject[/color][color=#007700]([/color][color=#0000bb]listvocin[/color][color=#007700]); [/color][color=#0000bb]oos[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); [/color][color=#0000bb]fo[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); } catch ([/color][color=#0000bb]filenotfoundexception 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]"file not found!"[/color][color=#007700]); } catch ([/color][color=#0000bb]exception 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]tostring[/color][color=#007700]()); } [/color][color=#0000bb]arraylist[/color][color=#007700]<[/color][color=#0000bb]vocabulary[/color][color=#007700]> [/color][color=#0000bb]listvocout [/color][color=#007700]= new [/color][color=#0000bb]arraylist[/color][color=#007700](); try { [/color][color=#0000bb]vocabulary vocout [/color][color=#007700]= new [/color][color=#0000bb]vocabulary[/color][color=#007700](); [/color][color=#0000bb]fileinputstream fi [/color][color=#007700]= new [/color][color=#0000bb]fileinputstream[/color][color=#007700]([/color][color=#dd0000]"d:\\listvocabulary.txt"[/color][color=#007700]); [/color][color=#0000bb]objectinputstream ois [/color][color=#007700]= new [/color][color=#0000bb]objectinputstream[/color][color=#007700]([/color][color=#0000bb]fi[/color][color=#007700]); [/color][color=#0000bb]listvocout [/color][color=#007700]= ([/color][color=#0000bb]arraylist[/color][color=#007700]<[/color][color=#0000bb]vocabulary[/color][color=#007700]>) [/color][color=#0000bb]ois[/color][color=#007700].[/color][color=#0000bb]readobject[/color][color=#007700](); [/color][color=#0000bb]ois[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); [/color][color=#0000bb]fi[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); } catch ([/color][color=#0000bb]filenotfoundexception 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]"file not found!"[/color][color=#007700]); } catch ([/color][color=#0000bb]exception 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].[/color][color=#0000bb]tostring[/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]"//============================//"[/color][color=#007700]); [/color][color=#0000bb]boolean found [/color][color=#007700]= [/color][color=#0000bb]false[/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]"enter new word you want to look up: "[/color][color=#007700]); [/color][color=#0000bb]string search [/color][color=#007700]= [/color][color=#0000bb]s[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700](); for ([/color][color=#0000bb]int i [/color][color=#007700]= [/color][color=#0000bb]0[/color][color=#007700]; [/color][color=#0000bb]i [/color][color=#007700]< [/color][color=#0000bb]listvocout[/color][color=#007700].[/color][color=#0000bb]size[/color][color=#007700](); [/color][color=#0000bb]i[/color][color=#007700]++) { if ([/color][color=#0000bb]search[/color][color=#007700].[/color][color=#0000bb]comparetoignorecase[/color][color=#007700]([/color][color=#0000bb]listvocout[/color][color=#007700].[/color][color=#0000bb]get[/color][color=#007700]([/color][color=#0000bb]i[/color][color=#007700]).[/color][color=#0000bb]getword[/color][color=#007700]())==[/color][color=#0000bb]0[/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]listvocout[/color][color=#007700].[/color][color=#0000bb]get[/color][color=#007700]([/color][color=#0000bb]i[/color][color=#007700]).[/color][color=#0000bb]getword[/color][color=#007700]()+[/color][color=#dd0000]": "[/color][color=#007700]+[/color][color=#0000bb]listvocout[/color][color=#007700].[/color][color=#0000bb]get[/color][color=#007700]([/color][color=#0000bb]i[/color][color=#007700]).[/color][color=#0000bb]getmean[/color][color=#007700]()); [/color][color=#0000bb]found [/color][color=#007700]= [/color][color=#0000bb]true[/color][color=#007700]; } } if (![/color][color=#0000bb]found[/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 word you want to look up not found!"[/color][color=#007700]); } } } class [/color][color=#0000bb]vocabulary [/color][color=#007700]implements [/color][color=#0000bb]serializable [/color][color=#007700]{ private [/color][color=#0000bb]string word[/color][color=#007700], [/color][color=#0000bb]mean[/color][color=#007700]; public [/color][color=#0000bb]string getword[/color][color=#007700]() { return [/color][color=#0000bb]word[/color][color=#007700]; } public [/color][color=#0000bb]void setword[/color][color=#007700]([/color][color=#0000bb]string word[/color][color=#007700]) { [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]word [/color][color=#007700]= [/color][color=#0000bb]word[/color][color=#007700]; } public [/color][color=#0000bb]string getmean[/color][color=#007700]() { return [/color][color=#0000bb]mean[/color][color=#007700]; } public [/color][color=#0000bb]void setmean[/color][color=#007700]([/color][color=#0000bb]string mean[/color][color=#007700]) { [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]mean [/color][color=#007700]= [/color][color=#0000bb]mean[/color][color=#007700]; }}[/color] 
    </blockquote></div>
    </div>
    </div>

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
  •