Kết quả 1 đến 9 của 9
  1. #1
    Ngày tham gia
    Mar 2015
    Bài viết
    0

    Bài 29: File truy cập ngẫu nhiên (Random Access Files) 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/randomaccessfile-hierarchy-jpg.18384/" border="0" alt="" />


    1, random access files?
    ​- bên cạnh việc xử lý xuất nhập trên file theo kiểu tuần tự thông qua các luồng, java cũng hỗ trợ truy cập ngẫu nhiên nội dung của một file nào đó dùng randomaccessfile.
    - randomaccessfile không dẫn xuất từ inputstream hay outputstream mà nó hiện thực các interface datainput, dataoutput (có định nghĩa các phương thức i/o cơ bản).
    - randomaccessfile hỗ trợ vấn đề định vị con trỏ file bên trong một file dùng phương thức seek(long newpos).

    2, ví dụ:

    bài này lý thuyết ngắn, nhưng ví dụ hơi dài, chủ yếu là đọc code để hiểu! :d
    chương trình sau sẽ ghi 9 số kiểu double xuống file, rồi đọc lên theo thứ tự ngẫu nhiên. các bạn xem code có chỗ nào không hiểu cứ bình luận phía dưới, mọi người cùng nhau giải quyết! ^^


    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].*; class [/color][color=#0000bb]javaandroidvn [/color][color=#007700]{ public static [/color][color=#0000bb]void main[/color][color=#007700]([/color][color=#0000bb]string args[/color][color=#007700][]) [/color][color=#0000bb]throws ioexception [/color][color=#007700]{ [/color][color=#0000bb]double data[/color][color=#007700][] = {[/color][color=#0000bb]11.2[/color][color=#007700], [/color][color=#0000bb]13.6[/color][color=#007700], [/color][color=#0000bb]255.6[/color][color=#007700], [/color][color=#0000bb]117.92[/color][color=#007700], [/color][color=#0000bb]2007.96[/color][color=#007700], [/color][color=#0000bb]8.9[/color][color=#007700], [/color][color=#0000bb]9.9[/color][color=#007700], [/color][color=#0000bb]10.0[/color][color=#007700], [/color][color=#0000bb]100.6[/color][color=#007700]}; [/color][color=#0000bb]double d[/color][color=#007700]; [/color][color=#0000bb]randomaccessfile raf[/color][color=#007700]; try { [/color][color=#0000bb]raf [/color][color=#007700]= new [/color][color=#0000bb]randomaccessfile[/color][color=#007700]([/color][color=#dd0000]"e:\ andom.dat"[/color][color=#007700], [/color][color=#dd0000]"rw"[/color][color=#007700]); } catch ([/color][color=#0000bb]filenotfoundexception exc[/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]"cannot open file."[/color][color=#007700]); return; } [/color][color=#ff8000]// write values to the 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]data[/color][color=#007700].[/color][color=#0000bb]length[/color][color=#007700]; [/color][color=#0000bb]i[/color][color=#007700]++) { try { [/color][color=#0000bb]raf[/color][color=#007700].[/color][color=#0000bb]writedouble[/color][color=#007700]([/color][color=#0000bb]data[/color][color=#007700][[/color][color=#0000bb]i[/color][color=#007700]]); } catch ([/color][color=#0000bb]ioexception exc[/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]"error writing to file."[/color][color=#007700]); return; } } try {[/color][color=#ff8000]// now, read back specific values [/color][color=#0000bb]raf[/color][color=#007700].[/color][color=#0000bb]seek[/color][color=#007700]([/color][color=#0000bb]0 [/color][color=#007700]* [/color][color=#0000bb]8[/color][color=#007700]); [/color][color=#ff8000]// seek to first double [/color][color=#0000bb]d [/color][color=#007700]= [/color][color=#0000bb]raf[/color][color=#007700].[/color][color=#0000bb]readdouble[/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]"first value is " [/color][color=#007700]+ [/color][color=#0000bb]d[/color][color=#007700]); [/color][color=#0000bb]raf[/color][color=#007700].[/color][color=#0000bb]seek[/color][color=#007700]([/color][color=#0000bb]8 [/color][color=#007700]* [/color][color=#0000bb]1[/color][color=#007700]); [/color][color=#ff8000]// seek to second double [/color][color=#0000bb]d [/color][color=#007700]= [/color][color=#0000bb]raf[/color][color=#007700].[/color][color=#0000bb]readdouble[/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]"second value is " [/color][color=#007700]+ [/color][color=#0000bb]d[/color][color=#007700]); [/color][color=#0000bb]raf[/color][color=#007700].[/color][color=#0000bb]seek[/color][color=#007700]([/color][color=#0000bb]8 [/color][color=#007700]* [/color][color=#0000bb]3[/color][color=#007700]); [/color][color=#ff8000]// seek to fourth double [/color][color=#0000bb]d [/color][color=#007700]= [/color][color=#0000bb]raf[/color][color=#007700].[/color][color=#0000bb]readdouble[/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]"fourth value is " [/color][color=#007700]+ [/color][color=#0000bb]d[/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=#ff8000]//read all data [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"read all: "[/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]data[/color][color=#007700].[/color][color=#0000bb]length[/color][color=#007700]; [/color][color=#0000bb]i[/color][color=#007700]++) { [/color][color=#0000bb]raf[/color][color=#007700].[/color][color=#0000bb]seek[/color][color=#007700]([/color][color=#0000bb]8 [/color][color=#007700]* [/color][color=#0000bb]i[/color][color=#007700]); [/color][color=#ff8000]// seek to ith double [/color][color=#0000bb]d [/color][color=#007700]= [/color][color=#0000bb]raf[/color][color=#007700].[/color][color=#0000bb]readdouble[/color][color=#007700](); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#0000bb]d [/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]""[/color][color=#007700]); [/color][color=#ff8000]// now, read every other value. [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"here is every other value: "[/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]data[/color][color=#007700].[/color][color=#0000bb]length[/color][color=#007700]; [/color][color=#0000bb]i [/color][color=#007700]+= [/color][color=#0000bb]2[/color][color=#007700]) { [/color][color=#0000bb]raf[/color][color=#007700].[/color][color=#0000bb]seek[/color][color=#007700]([/color][color=#0000bb]8 [/color][color=#007700]* [/color][color=#0000bb]i[/color][color=#007700]); [/color][color=#ff8000]// seek to ith double [/color][color=#0000bb]d [/color][color=#007700]= [/color][color=#0000bb]raf[/color][color=#007700].[/color][color=#0000bb]readdouble[/color][color=#007700](); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#0000bb]d [/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]"
    "[/color][color=#007700]); } catch ([/color][color=#0000bb]ioexception exc[/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]"
    error seeking or reading."[/color][color=#007700]); } [/color][color=#0000bb]raf[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); }}[/color] 
    bài tập về nhà::d
    - tạo chương trình khi chạy lên nếu như không tồn tại file thì tạo file mới như sau:
    - tạo 1 mảng , các phần tử là các số thực. các số được ghi vào file theo kiểu ghi ngẫu nhiên.
    - nếu file đã tồn tại, hoặc là ở những lần chạy sau, sau khi đã khởi tạo giá trị, chương trình bỏ qua bước khởi tạo và dựa vào file in ra những số có chỉ số lẻ trong mảng đã nhập trên bằng cách lấy trực tiếp chỉ số khi đọc file theo cách đọc ngẫu nhiên! (không dùng cách nạp lại cả file vào mảng)

    (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
    Jun 2014
    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ã:
    import java.io.file;
    import java.io.filenotfoundexception;
    import java.io.ioexception;
    import java.io.randomaccessfile;
    import java.util.scanner;
     
    public class vdbai29b {
        public static void main(string[]args) throws ioexception{
            vdbai29b vd=new vdbai29b();
            scanner in=new scanner(system.in);
            double []aa={1,4,6.8,9.1,33,56};
            system.out.println("nhap ten file:");
            string s1=in.nextline();
            file alt=new file(s1);
            if(alt.exists()== true)
            {
                system.out.println("file "+s1+ " already.");
                vd.readfile(s1, aa);
            }
            else
            {
                vd.writefile(s1, aa);
                system.out.println("new file success");
            }
        }
        public static void writefile(string s,double []a) throws ioexception
        {
            randomaccessfile raf;
            try {
                raf = new randomaccessfile(s, "rw");
            } catch (filenotfoundexception exc) {
                system.out.println("cannot open file.");
                return;
            }
     
            // write values to the file.
            for (int i = 0; i < a.length; i++) {
                try {
                    raf.writedouble(a[i]);
     
                } catch (ioexception exc) {
                    system.out.println("error writing to file.");
                    return;
                }
            }
            raf.close();
        }
        public static void readfile(string s,double []a) throws ioexception
        {
            double d;
            randomaccessfile raf;
            try {
                raf = new randomaccessfile(s, "rw");
            } catch (filenotfoundexception exc) {
                system.out.println("cannot open file.");
                return;
            }
            try {
                // now, read every other value.
                            system.out.println("nhung so co chi so le trong mang: ");
                            for (int i = 1; i < a.length; i += 2) {
                                raf.seek(8 * i); // seek to ith double
                                d = raf.readdouble();
                                system.out.print(d + "\t");
                            }
               
                            system.out.println("
    ");
                        } catch (ioexception exc) {
                            system.out.println("error seeking or reading.");
                        }
                        raf.close();
        }
    }
    </blockquote></div>
    </div>
    </div>

  3. #3
    Ngày tham gia
    Apr 2016
    Bài viết
    0
    cảm ơn bạn !

  4. #4
    Ngày tham gia
    Jul 2014
    Bài viết
    0
    cho mình hỏi bài code ở trên 1 tí: raf.seek(0*8);
    8 là byte kiểu double, vậy cho mình hỏi là sao mình thực hiện trên kiểu int là: raf.seek(0*4); lại không được

  5. #5
    Ngày tham gia
    Apr 2016
    Bài viết
    0
    cho mình hỏi có cách nào lấy được độ dài của mảng trong file không. bài của mình trong trường hợp file đã có sẵn thì phải nhập đúng số phần tử của mảng nó mới chạy đúng:khắc phuc hộ mình với....
    <div class="bbcodeblock bbcodequote bbcmspoilerblock">
    <div class="attribution type"><noscript>bai29 (move your mouse to reveal the content)</noscript>

    bai29 (open)
    bai29 (close)

    </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]import java[/color][color=#007700].[/color][color=#0000bb]util[/color][color=#007700].[/color][color=#0000bb]scanner[/color][color=#007700];public class [/color][color=#0000bb]bai29fileranrom [/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 exception[/color][color=#007700]{[/color][color=#0000bb]string s1[/color][color=#007700];@[/color][color=#0000bb]suppresswarnings[/color][color=#007700]([/color][color=#dd0000]"resource"[/color][color=#007700])[/color][color=#0000bb]scanner 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].print([/color][color=#dd0000]"nhap ten file can tao: "[/color][color=#007700]);[/color][color=#0000bb]s1[/color][color=#007700]=[/color][color=#0000bb]sc[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700]();[/color][color=#ff8000]//nhap độ dài của mang cần nhập vào file[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#dd0000]"nhap vao so phan tu cua mang double: "[/color][color=#007700]);[/color][color=#0000bb]int n[/color][color=#007700]=[/color][color=#0000bb]sc[/color][color=#007700].[/color][color=#0000bb]nextint[/color][color=#007700]();[/color][color=#0000bb]bai29 a[/color][color=#007700]=new [/color][color=#0000bb]bai29[/color][color=#007700]();[/color][color=#0000bb]file filename[/color][color=#007700]=new [/color][color=#0000bb]file[/color][color=#007700]([/color][color=#0000bb]s1[/color][color=#007700]);if ([/color][color=#0000bb]filename[/color][color=#007700].[/color][color=#0000bb]exists[/color][color=#007700]()==[/color][color=#0000bb]true[/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 da ton tai"[/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 file:"[/color][color=#007700]+[/color][color=#0000bb]s1[/color][color=#007700]);[/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]read[/color][color=#007700]([/color][color=#0000bb]s1[/color][color=#007700],[/color][color=#0000bb]n[/color][color=#007700]);}else{[/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 chua ton tai, nhap thong tin file moi: "[/color][color=#007700]);[/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]write[/color][color=#007700]([/color][color=#0000bb]s1[/color][color=#007700],[/color][color=#0000bb]n[/color][color=#007700]);[/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]read[/color][color=#007700]([/color][color=#0000bb]s1[/color][color=#007700],[/color][color=#0000bb]n[/color][color=#007700]);}}}[/color][color=#ff8000]//ghi du lieuj vao file[/color][color=#007700]class [/color][color=#0000bb]bai29[/color][color=#007700]{[/color][color=#0000bb]string s1[/color][color=#007700];[/color][color=#0000bb]double[/color][color=#007700][] [/color][color=#0000bb]data[/color][color=#007700];[/color][color=#0000bb]void write[/color][color=#007700]([/color][color=#0000bb]string s1[/color][color=#007700],[/color][color=#0000bb]int n[/color][color=#007700]) [/color][color=#0000bb]throws exception[/color][color=#007700]{[/color][color=#0000bb]randomaccessfile rfile[/color][color=#007700];@[/color][color=#0000bb]suppresswarnings[/color][color=#007700]([/color][color=#dd0000]"resource"[/color][color=#007700])[/color][color=#0000bb]scanner 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]double data[/color][color=#007700][]=new [/color][color=#0000bb]double[/color][color=#007700][[/color][color=#0000bb]n[/color][color=#007700]];try{[/color][color=#0000bb]rfile[/color][color=#007700]=new [/color][color=#0000bb]randomaccessfile[/color][color=#007700]([/color][color=#0000bb]s1[/color][color=#007700], [/color][color=#dd0000]"rw"[/color][color=#007700]);}catch([/color][color=#0000bb]filenotfoundexception e1[/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]"khong tao duoc file"[/color][color=#007700]);return;}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]++){try{[/color][color=#0000bb]rfile[/color][color=#007700].[/color][color=#0000bb]writedouble[/color][color=#007700]([/color][color=#0000bb]data[/color][color=#007700][[/color][color=#0000bb]i[/color][color=#007700]]=[/color][color=#0000bb]sc[/color][color=#007700].[/color][color=#0000bb]nextdouble[/color][color=#007700]());}catch([/color][color=#0000bb]ioexception e2[/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 khong viet duoc file"[/color][color=#007700]);}}[/color][color=#0000bb]rfile[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700]();}[/color][color=#ff8000]//đọc các phần tử lẻ của mảng từ file[/color][color=#0000bb]void read[/color][color=#007700]([/color][color=#0000bb]string s1[/color][color=#007700],[/color][color=#0000bb]int n[/color][color=#007700])[/color][color=#0000bb]throws ioexception[/color][color=#007700]{[/color][color=#0000bb]randomaccessfile rfile[/color][color=#007700];try {[/color][color=#0000bb]rfile [/color][color=#007700]= new [/color][color=#0000bb]randomaccessfile[/color][color=#007700]([/color][color=#0000bb]s1[/color][color=#007700], [/color][color=#dd0000]"rw"[/color][color=#007700]);} catch ([/color][color=#0000bb]filenotfoundexception exc[/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]"cannot open file."[/color][color=#007700]);return;}for ([/color][color=#0000bb]int i[/color][color=#007700]=[/color][color=#0000bb]1[/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]2[/color][color=#007700]) {[/color][color=#0000bb]rfile[/color][color=#007700].[/color][color=#0000bb]seek[/color][color=#007700]([/color][color=#0000bb]8 [/color][color=#007700]* [/color][color=#0000bb]i[/color][color=#007700]); [/color][color=#0000bb]double d [/color][color=#007700]= [/color][color=#0000bb]rfile[/color][color=#007700].[/color][color=#0000bb]readdouble[/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]d [/color][color=#007700]+ [/color][color=#dd0000]" "[/color][color=#007700]);}[/color][color=#0000bb]rfile[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700]();}}[/color] 
    </blockquote></div>
    </div>
    </div>

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

    myunit29 (open)
    myunit29 (close)

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


    Mã nguồn PHP:
    [color=#000000]
    package hoc[/color][color=#007700].[/color][color=#0000bb]java[/color][color=#007700]; [/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]file[/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]randomaccessfile[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]util[/color][color=#007700].[/color][color=#0000bb]random[/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]unit29 [/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]scanner indata [/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]unit29 u [/color][color=#007700]= new [/color][color=#0000bb]unit29[/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 ten file: "[/color][color=#007700]);[/color][color=#0000bb]string file [/color][color=#007700]= [/color][color=#0000bb]indata[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700]();if ([/color][color=#0000bb]u[/color][color=#007700].[/color][color=#0000bb]checkfile[/color][color=#007700]([/color][color=#0000bb]file[/color][color=#007700]) == [/color][color=#0000bb]true[/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 da ton tai"[/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]"noi dung file: "[/color][color=#007700]);[/color][color=#0000bb]u[/color][color=#007700].[/color][color=#0000bb]inputdata[/color][color=#007700]([/color][color=#0000bb]file[/color][color=#007700]);} else {[/color][color=#0000bb]u[/color][color=#007700].[/color][color=#0000bb]outputdata[/color][color=#007700]([/color][color=#0000bb]file[/color][color=#007700]);} } public [/color][color=#0000bb]void inputdata[/color][color=#007700]([/color][color=#0000bb]string filename[/color][color=#007700]) {try {[/color][color=#0000bb]randomaccessfile raf [/color][color=#007700]= new [/color][color=#0000bb]randomaccessfile[/color][color=#007700]([/color][color=#0000bb]filename[/color][color=#007700], [/color][color=#dd0000]"rw"[/color][color=#007700]);[/color][color=#0000bb]int i [/color][color=#007700]= [/color][color=#0000bb]0[/color][color=#007700];while (([/color][color=#0000bb]i[/color][color=#007700]*[/color][color=#0000bb]4[/color][color=#007700]) < [/color][color=#0000bb]raf[/color][color=#007700].[/color][color=#0000bb]length[/color][color=#007700]()){[/color][color=#0000bb]raf[/color][color=#007700].[/color][color=#0000bb]seek[/color][color=#007700]([/color][color=#0000bb]4 [/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=#0000bb]raf[/color][color=#007700].[/color][color=#0000bb]getfilepointer[/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]raf[/color][color=#007700].[/color][color=#0000bb]readfloat[/color][color=#007700]());[/color][color=#0000bb]i[/color][color=#007700]+=[/color][color=#0000bb]2[/color][color=#007700];}[/color][color=#0000bb]raf[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); } catch ([/color][color=#0000bb]ioexception exc[/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]"co loi xay ra"[/color][color=#007700]);}} public [/color][color=#0000bb]void outputdata[/color][color=#007700]([/color][color=#0000bb]string filename[/color][color=#007700]) {[/color][color=#0000bb]float[/color][color=#007700][] [/color][color=#0000bb]arrsothuc [/color][color=#007700]= new [/color][color=#0000bb]float[/color][color=#007700][[/color][color=#0000bb]10[/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]arrsothuc[/color][color=#007700].[/color][color=#0000bb]length[/color][color=#007700]; [/color][color=#0000bb]i[/color][color=#007700]++) {[/color][color=#0000bb]arrsothuc[/color][color=#007700][[/color][color=#0000bb]i[/color][color=#007700]] = new [/color][color=#0000bb]random[/color][color=#007700]().[/color][color=#0000bb]nextfloat[/color][color=#007700]();}try {[/color][color=#0000bb]randomaccessfile raf [/color][color=#007700]= new [/color][color=#0000bb]randomaccessfile[/color][color=#007700]([/color][color=#0000bb]filename[/color][color=#007700], [/color][color=#dd0000]"rw"[/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]arrsothuc[/color][color=#007700].[/color][color=#0000bb]length[/color][color=#007700]; [/color][color=#0000bb]i[/color][color=#007700]++) {{[/color][color=#0000bb]raf[/color][color=#007700].[/color][color=#0000bb]writefloat[/color][color=#007700]([/color][color=#0000bb]arrsothuc[/color][color=#007700][[/color][color=#0000bb]i[/color][color=#007700]]);}[/color][color=#0000bb]raf[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); }} catch ([/color][color=#0000bb]ioexception exc[/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]"co loi xay ra"[/color][color=#007700]);}} public [/color][color=#0000bb]boolean checkfile[/color][color=#007700]([/color][color=#0000bb]string filename[/color][color=#007700]) {[/color][color=#0000bb]file f [/color][color=#007700]= new [/color][color=#0000bb]file[/color][color=#007700]([/color][color=#0000bb]filename[/color][color=#007700]);if ([/color][color=#0000bb]f[/color][color=#007700].[/color][color=#0000bb]exists[/color][color=#007700]())return [/color][color=#0000bb]true[/color][color=#007700];elsereturn [/color][color=#0000bb]false[/color][color=#007700];}}[/color] 
    </blockquote></div>
    </div>
    </div>

  7. #7
    Ngày tham gia
    Sep 2014
    Bài viết
    0
    Mã nguồn PHP:
    [color=#000000]
    package baitap[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/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]unit_29 [/color][color=#007700]{[/color][color=#0000bb]float a[/color][color=#007700][];[/color][color=#0000bb]int n[/color][color=#007700];public [/color][color=#0000bb]void taofile[/color][color=#007700](){[/color][color=#0000bb]scanner in[/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].print([/color][color=#dd0000]"so phan tu cua mang: "[/color][color=#007700]);[/color][color=#0000bb]n[/color][color=#007700]=[/color][color=#0000bb]in[/color][color=#007700].[/color][color=#0000bb]nextint[/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]"cac phan tu:"[/color][color=#007700]);[/color][color=#0000bb]a[/color][color=#007700]=new [/color][color=#0000bb]float[/color][color=#007700][[/color][color=#0000bb]n[/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]a[/color][color=#007700].[/color][color=#0000bb]length[/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]"a["[/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]a[/color][color=#007700]=[/color][color=#0000bb]in[/color][color=#007700].[/color][color=#0000bb]nextfloat[/color][color=#007700]();}}public [/color][color=#0000bb]void vietfile[/color][color=#007700]([/color][color=#0000bb]string file[/color][color=#007700]) [/color][color=#0000bb]throws ioexception[/color][color=#007700]{[/color][color=#0000bb]float f[/color][color=#007700];[/color][color=#0000bb]randomaccessfile raf[/color][color=#007700];try {[/color][color=#0000bb]raf[/color][color=#007700]=new [/color][color=#0000bb]randomaccessfile[/color][color=#007700]([/color][color=#0000bb]file[/color][color=#007700],[/color][color=#dd0000]"rw"[/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]"xay ra loi1: "[/color][color=#007700]+[/color][color=#0000bb]e[/color][color=#007700]);return;}[/color][color=#ff8000]//viet cac gia tri vao 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]++) {try {[/color][color=#0000bb]raf[/color][color=#007700].[/color][color=#0000bb]writefloat[/color][color=#007700]([/color][color=#0000bb]a[/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]"xay ra loi2: "[/color][color=#007700]+[/color][color=#0000bb]e[/color][color=#007700]);return;}}[/color][color=#0000bb]raf[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700]();}public [/color][color=#0000bb]void docfile[/color][color=#007700]([/color][color=#0000bb]string file[/color][color=#007700]) [/color][color=#0000bb]throws ioexception[/color][color=#007700]{[/color][color=#0000bb]randomaccessfile raf[/color][color=#007700];[/color][color=#0000bb]float f[/color][color=#007700];try {[/color][color=#0000bb]raf[/color][color=#007700]=new [/color][color=#0000bb]randomaccessfile[/color][color=#007700]([/color][color=#0000bb]file[/color][color=#007700],[/color][color=#dd0000]"rw"[/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]"cac phan tu cua chi so le:"[/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]2[/color][color=#007700]) {[/color][color=#0000bb]raf[/color][color=#007700].[/color][color=#0000bb]seek[/color][color=#007700]([/color][color=#0000bb]4[/color][color=#007700]*[/color][color=#0000bb]i[/color][color=#007700]);[/color][color=#0000bb]f[/color][color=#007700]=[/color][color=#0000bb]raf[/color][color=#007700].[/color][color=#0000bb]readfloat[/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]f[/color][color=#007700]+[/color][color=#dd0000]" "[/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]"xay ra loi4: "[/color][color=#007700]+[/color][color=#0000bb]e[/color][color=#007700]);return;}[/color][color=#0000bb]raf[/color][color=#007700].[/color][color=#0000bb]close[/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]unit_29 un [/color][color=#007700]= new [/color][color=#0000bb]unit_29[/color][color=#007700]();[/color][color=#0000bb]string link[/color][color=#007700];[/color][color=#0000bb]scanner in[/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 dia chi:"[/color][color=#007700]);[/color][color=#0000bb]link[/color][color=#007700]=[/color][color=#0000bb]in[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700]();[/color][color=#0000bb]file f[/color][color=#007700]=new [/color][color=#0000bb]file[/color][color=#007700]([/color][color=#0000bb]link[/color][color=#007700]);if([/color][color=#0000bb]f[/color][color=#007700].[/color][color=#0000bb]exists[/color][color=#007700]()==[/color][color=#0000bb]true[/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 da ton tai"[/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 file "[/color][color=#007700]+[/color][color=#0000bb]link[/color][color=#007700]+[/color][color=#dd0000]" :"[/color][color=#007700]);[/color][color=#0000bb]un[/color][color=#007700].[/color][color=#0000bb]docfile[/color][color=#007700]([/color][color=#0000bb]link[/color][color=#007700]);}else {[/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 chua duoc tao
    viet file moi "[/color][color=#007700]);[/color][color=#0000bb]f[/color][color=#007700].[/color][color=#0000bb]createnewfile[/color][color=#007700]();[/color][color=#0000bb]un[/color][color=#007700].[/color][color=#0000bb]taofile[/color][color=#007700]();[/color][color=#0000bb]un[/color][color=#007700].[/color][color=#0000bb]vietfile[/color][color=#007700]([/color][color=#0000bb]link[/color][color=#007700]);[/color][color=#0000bb]un[/color][color=#007700].[/color][color=#0000bb]docfile[/color][color=#007700]([/color][color=#0000bb]link[/color][color=#007700]);}}}[/color] 

  8. #8
    Ngày tham gia
    Nov 2014
    Bài viết
    0
    vì sao khi nhập tên file trùng rồi, mà tới khi lấy dữ liệu ra từ file đó lại k đc. mình làm theo cách trên nhưng k có dữ liệu đưa ra

  9. #9
    Ngày tham gia
    Feb 2015
    Bài viết
    0
    cho mình hỏi cách dùng seek(). mình không hiểu?

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
  •