-
08-21-2013, 07:00 AM #1Junior Member
- Ngày tham gia
- Nov 2014
- Bài viết
- 0
Bài 31: Đọc/ghi file dùng luồng ký tự 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í"- khi chúng ta thao tác với văn bản, có thể sử dụng luồng byte. tuy nhiên thì sự lựa chọn tốt nhất không phải là nó mà là luồng ký tự, việc sử dụng luồng ký tự có ưu điểm là thao tác trực tiếp trên các ký tự unicode.
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/144887966-png.22669/" border="0" alt="" />
- cụ thể ở bài này mình sẽ giới thiệu 2 lớp luồng thường dùng cho việc đọc , ghi dữ liệu file là filereader và filewriter
1, ghi dữ liệu xuống file văn bản!
ví dụ: nhập danh sách tên của 1 lớp học và ghi chúng xuống file tên là "danhsach.txt". việc đọc và ghi kết thúc khi người dùng nhập vào chuỗi "stop".
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]bufferedreader[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]filewriter[/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]inputstreamreader[/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]string str[/color][color=#007700]; [/color][color=#0000bb]filewriter fw[/color][color=#007700]; [/color][color=#0000bb]bufferedreader br [/color][color=#007700]= new [/color][color=#0000bb]bufferedreader[/color][color=#007700]( new [/color][color=#0000bb]inputstreamreader[/color][color=#007700]([/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]in[/color][color=#007700])); try { [/color][color=#0000bb]fw [/color][color=#007700]= new [/color][color=#0000bb]filewriter[/color][color=#007700]([/color][color=#dd0000]"e:\\danhsach.txt"[/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]"có lỗi xảy ra"[/color][color=#007700]); return; } [/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 danh sách tên: ('stop' để kết thúc chương trình)"[/color][color=#007700]); do { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#dd0000]": "[/color][color=#007700]); [/color][color=#0000bb]str [/color][color=#007700]= [/color][color=#0000bb]br[/color][color=#007700].[/color][color=#0000bb]readline[/color][color=#007700](); if ([/color][color=#0000bb]str[/color][color=#007700].[/color][color=#0000bb]compareto[/color][color=#007700]([/color][color=#dd0000]"stop"[/color][color=#007700]) == [/color][color=#0000bb]0[/color][color=#007700]) { break; } [/color][color=#0000bb]str [/color][color=#007700]= [/color][color=#0000bb]str [/color][color=#007700]+ [/color][color=#dd0000]"
"[/color][color=#007700]; [/color][color=#ff8000]//dùng để xuống dòng khi nhập xong 1 chuỗi! [/color][color=#0000bb]fw[/color][color=#007700].[/color][color=#0000bb]write[/color][color=#007700]([/color][color=#0000bb]str[/color][color=#007700]); } while ([/color][color=#0000bb]str[/color][color=#007700].[/color][color=#0000bb]compareto[/color][color=#007700]([/color][color=#dd0000]"stop"[/color][color=#007700]) != [/color][color=#0000bb]0[/color][color=#007700]); [/color][color=#0000bb]fw[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"bạn mở file ở đường dẫn e:\\danhsach.txt, dũ liệu đã được lưu!"[/color][color=#007700]); }}[/color]
ví dụ: đọc và hiển thị nội dung của file "danhsach.txt" lên màn hình. trong đó file là file văn bản, trong là danh sách tên ghi theo từng dòng! chương trình sẽ đọc file văn bản từng dòng!
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]bufferedreader[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]filereader[/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]; 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]{ try ([/color][color=#0000bb]filereader fr [/color][color=#007700]= new [/color][color=#0000bb]filereader[/color][color=#007700]([/color][color=#dd0000]"e:\\danhsach.txt"[/color][color=#007700])) { [/color][color=#ff8000]//bạn có thể tạo dữ liệu văn bản dạng utf-8 - tiếng việt [/color][color=#0000bb]bufferedreader br [/color][color=#007700]= new [/color][color=#0000bb]bufferedreader[/color][color=#007700]([/color][color=#0000bb]fr[/color][color=#007700]); [/color][color=#0000bb]string s[/color][color=#007700]; [/color][color=#ff8000]//đọc từng dòng văn bản! [/color][color=#007700]while (([/color][color=#0000bb]s [/color][color=#007700]= [/color][color=#0000bb]br[/color][color=#007700].[/color][color=#0000bb]readline[/color][color=#007700]()) != [/color][color=#0000bb]null[/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]s[/color][color=#007700]); } }catch([/color][color=#0000bb]ioexception e[/color][color=#007700]){ [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"đã có lỗi xảy ra!"[/color][color=#007700]); } }}[/color]
viết chương trình copy nội dung file "input.txt" sang file "output.txt" sử dụng cách đọc ghi văn bản!
bạn nào làm xong cố gắng bớt chút ít thời gian đăng lên cho các bạn khác cùng tham khảo nhé!
(chú ý: các bạn nên tự giác làm bài tập, hoàn thiện rồi đăng lên, các bài bạn đã làm, nên để code trong thẻ code - chọn thẻ php cho rõ ràng và để ẩn code trong thẻ ẩn spoiler, để khi click vào mới hiện code lên , trước khi thử sức tự làm, bạn không nên nhìn code của các bạn khác đã đăng nhé)
my facebook: https://www.facebook.com/tiasangmoi
bài viết đăng lên diễn đàn khác vui lòng ghi rõ nguồn: android.vn
-
08-29-2013, 07:00 AM #2Junior Member
- Ngày tham gia
- Apr 2014
- Bài viết
- 0
cảm ơn chủ thớt rất nhiều...
-
10-04-2013, 07:00 AM #3Junior Member
- 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.bufferedreader; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.filereader; import java.io.filewriter; import java.io.ioexception; import java.io.inputstreamreader; import java.util.scanner; public class btbai31 { public static void main(string[]args) throws ioexception{ btbai31 bt=new btbai31(); string input="d:\\input.txt"; string output="d:\\output.txt"; system.out.println("ghi noi dung file input:"); bt.writefile(input); system.out.println("copy file input sang output"); bt.copy(input, output); system.out.println("doc noi dung file output:"); bt.readfile(output); } public static void writefile(string str) throws ioexception { filewriter fw; bufferedreader br = new bufferedreader( new inputstreamreader(system.in)); try { fw = new filewriter(str); } catch (ioexception exc) { system.out.println("có lỗi xảy ra"); return; } system.out.println("nhập danh sách tên: ('stop' để kết thúc chương trình)"); do { system.out.print(": "); str = br.readline(); if (str.compareto("stop") == 0) { break; } str = str + " "; //dùng để xuống dòng khi nhập xong 1 chuỗi! fw.write(str); } while (str.compareto("stop") != 0); fw.close(); } public static void readfile(string str) { try (filereader fr = new filereader(str)) { //bạn có thể tạo dữ liệu văn bản dạng utf-8 - tiếng việt bufferedreader br = new bufferedreader(fr); string s; //đọc từng dòng văn bản! while ((s = br.readline()) != null) { system.out.println(s); } }catch(ioexception e){ system.out.println("đã có lỗi xảy ra!"); } } public static void copy(string s1,string s2) throws ioexception { fileinputstream fin; fileoutputstream fout; try{ // open input file fin=new fileinputstream(s1); }catch(filenotfoundexception exc){ system.out.println("file not found"); return; } // open output file try{ fout=new fileoutputstream(s2); }catch(filenotfoundexception exc){ system.out.println("error opening outputfile"); return; }catch(arrayindexoutofbound***ception exc){ system.out.println("usage: copyfile from to"); return; } // copy file int i; try{ do{ i=fin.read(); if(i != -1) { fout.write(i); } }while(i != -1); }catch(ioexception exc){ system.out.println("file error "); return; } // close file fin.close(); fout.close(); } }
</div>
</div>
-
-
-
10-08-2013, 07:00 AM #6Junior Member
- Ngày tham gia
- Apr 2016
- Bài viết
- 0
Mã nguồn PHP:[color=#000000]
import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]bufferedreader[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]filewriter[/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]filereader[/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];class [/color][color=#0000bb]copy[/color][color=#007700]{ public [/color][color=#0000bb]void read[/color][color=#007700]()[/color][color=#0000bb]throws ioexception[/color][color=#007700]{ [/color][color=#0000bb]bufferedreader buff[/color][color=#007700]; [/color][color=#0000bb]filewriter f[/color][color=#007700]; [/color][color=#0000bb]fileinputstream ff[/color][color=#007700]; try{ [/color][color=#0000bb]f[/color][color=#007700]=new [/color][color=#0000bb]filewriter[/color][color=#007700]([/color][color=#dd0000]"g:\\java\\output.txt"[/color][color=#007700]); [/color][color=#0000bb]buff[/color][color=#007700]=new [/color][color=#0000bb]bufferedreader[/color][color=#007700](new [/color][color=#0000bb]filereader[/color][color=#007700]([/color][color=#dd0000]"g:\\java\\input.txt"[/color][color=#007700])); }catch([/color][color=#0000bb]ioexception e[/color][color=#007700]){ [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"error open file"[/color][color=#007700]); return; } [/color][color=#0000bb]string s[/color][color=#007700]; try{ while(([/color][color=#0000bb]s[/color][color=#007700]=[/color][color=#0000bb]buff[/color][color=#007700].[/color][color=#0000bb]readline[/color][color=#007700]())!=[/color][color=#0000bb]null[/color][color=#007700]){ [/color][color=#0000bb]f[/color][color=#007700].[/color][color=#0000bb]write[/color][color=#007700]([/color][color=#0000bb]s[/color][color=#007700]); } }catch([/color][color=#0000bb]ioexception e[/color][color=#007700]){ [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"error reading file"[/color][color=#007700]); } [/color][color=#0000bb]f[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); }}public class [/color][color=#0000bb]read_vanban [/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]copy c[/color][color=#007700]=new [/color][color=#0000bb]copy[/color][color=#007700](); [/color][color=#0000bb]c[/color][color=#007700].[/color][color=#0000bb]read[/color][color=#007700](); }}[/color]
-
02-10-2014, 07:00 AM #7Junior Member
- Ngày tham gia
- Jul 2015
- Bài viết
- 0
<div class="bbcodeblock bbcodequote bbcmspoilerblock">
<div class="attribution type"><noscript>spoiler (move your mouse to the spoiler area to reveal the content)</noscript>
show spoiler
hide spoiler
</div>
<div class="quotecontent">
<div class="bbcm_spoiler_noscript"><blockquote>
Mã nguồn PHP:[color=#000000]
[/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]filewriter fw [/color][color=#007700]= new [/color][color=#0000bb]filewriter[/color][color=#007700]([/color][color=#dd0000]"d:\\output.txt"[/color][color=#007700]); [/color][color=#0000bb]filereader fr[/color][color=#007700]; [/color][color=#0000bb]string s[/color][color=#007700]; try { [/color][color=#0000bb]fr [/color][color=#007700]= new [/color][color=#0000bb]filereader[/color][color=#007700]([/color][color=#dd0000]"d:\\input.txt"[/color][color=#007700]); } catch ([/color][color=#0000bb]filenotfoundexception ex[/color][color=#007700]) { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"co loi: " [/color][color=#007700]+ [/color][color=#0000bb]ex[/color][color=#007700]); return; } [/color][color=#0000bb]bufferedreader br [/color][color=#007700]= new [/color][color=#0000bb]bufferedreader[/color][color=#007700]([/color][color=#0000bb]fr[/color][color=#007700]); while ( ([/color][color=#0000bb]s [/color][color=#007700]= [/color][color=#0000bb]br[/color][color=#007700].[/color][color=#0000bb]readline[/color][color=#007700]()) != [/color][color=#0000bb]null [/color][color=#007700]) { [/color][color=#0000bb]fw[/color][color=#007700].[/color][color=#0000bb]write[/color][color=#007700]([/color][color=#0000bb]s [/color][color=#007700]+ [/color][color=#dd0000]"
"[/color][color=#007700]); } [/color][color=#0000bb]fr[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); [/color][color=#0000bb]fw[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); } [/color]
</div>
</div>
-
03-05-2014, 07:00 AM #8Junior Member
- Ngày tham gia
- Dec 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ã nguồn PHP:[color=#000000]
package nhansu[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]filereader[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]bufferedreader[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]filewriter[/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]util[/color][color=#007700].[/color][color=#0000bb]scanner[/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];public class [/color][color=#0000bb]copyluongkitu [/color][color=#007700]{ public [/color][color=#0000bb]void copy[/color][color=#007700]([/color][color=#0000bb]string st1[/color][color=#007700],[/color][color=#0000bb]string st2[/color][color=#007700])[/color][color=#0000bb]throws ioexception[/color][color=#007700]{ [/color][color=#0000bb]bufferedreader br[/color][color=#007700]; [/color][color=#0000bb]filereader fr[/color][color=#007700]; try{ [/color][color=#0000bb]fr[/color][color=#007700]=new [/color][color=#0000bb]filereader[/color][color=#007700]([/color][color=#0000bb]st1[/color][color=#007700]); [/color][color=#0000bb]br[/color][color=#007700]=new [/color][color=#0000bb]bufferedreader[/color][color=#007700]([/color][color=#0000bb]fr[/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]"file nguon khong ton tai"[/color][color=#007700]); return; } [/color][color=#0000bb]string s[/color][color=#007700]; [/color][color=#0000bb]filewriter fw[/color][color=#007700]; try{ [/color][color=#0000bb]fw[/color][color=#007700]=new [/color][color=#0000bb]filewriter[/color][color=#007700]([/color][color=#0000bb]st2[/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]"khong mo duoc file"[/color][color=#007700]); return; } while(([/color][color=#0000bb]s[/color][color=#007700]=[/color][color=#0000bb]br[/color][color=#007700].[/color][color=#0000bb]readline[/color][color=#007700]())!=[/color][color=#0000bb]null[/color][color=#007700]){ try{ [/color][color=#0000bb]s[/color][color=#007700]=[/color][color=#0000bb]s[/color][color=#007700]+[/color][color=#dd0000]"
"[/color][color=#007700]; [/color][color=#0000bb]fw[/color][color=#007700].[/color][color=#0000bb]write[/color][color=#007700]([/color][color=#0000bb]s[/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]"loi!"[/color][color=#007700]); return; } } [/color][color=#0000bb]fw[/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]copyluongkitu a[/color][color=#007700]=new [/color][color=#0000bb]copyluongkitu[/color][color=#007700](); [/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]"+chuong trinh thuc hien lenh copy:"[/color][color=#007700]); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#dd0000]" duong dan file nguon:"[/color][color=#007700]); [/color][color=#0000bb]string st1[/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]" duong dan file dich:"[/color][color=#007700]); [/color][color=#0000bb]string st2[/color][color=#007700]=[/color][color=#0000bb]input[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700](); [/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]copy[/color][color=#007700]([/color][color=#0000bb]st1[/color][color=#007700],[/color][color=#0000bb]st2[/color][color=#007700]); }}[/color]
</div>
</div>
-
06-09-2014, 07:00 AM #9Junior Member
- Ngày tham gia
- Apr 2014
- Bài viết
- 0
<div class="bbcodeblock bbcodequote bbcmspoilerblock">
<div class="attribution type"><noscript>copy file sử dụng filereader/writer (move your mouse to reveal the content)</noscript>
copy file sử dụng filereader/writer (open)
copy file sử dụng filereader/writer (close)
</div>
<div class="quotecontent">
<div class="bbcm_spoiler_noscript"><blockquote>
Mã nguồn PHP:[color=#000000]
package test[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]bufferedreader[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]filewriter[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]filereader[/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]inputstreamreader[/color][color=#007700]; public class [/color][color=#0000bb]test[/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]bufferedreader br [/color][color=#007700]= [/color][color=#0000bb]null[/color][color=#007700]; [/color][color=#0000bb]filereader fr [/color][color=#007700]= [/color][color=#0000bb]null[/color][color=#007700]; [/color][color=#0000bb]filewriter fw [/color][color=#007700]= [/color][color=#0000bb]null[/color][color=#007700]; [/color][color=#0000bb]string s[/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]"mo mot file da duoc luu"[/color][color=#007700]); try { [/color][color=#0000bb]fr [/color][color=#007700]= new [/color][color=#0000bb]filereader[/color][color=#007700]([/color][color=#dd0000]"c:\\01.txt"[/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]"mo file de ghi"[/color][color=#007700]); try { [/color][color=#0000bb]fw [/color][color=#007700]= new [/color][color=#0000bb]filewriter[/color][color=#007700]([/color][color=#dd0000]"d:\\02.txt"[/color][color=#007700]); }catch ([/color][color=#0000bb]ioexception e[/color][color=#007700]) { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"da co loi xay ra."[/color][color=#007700]); } } catch ([/color][color=#0000bb]ioexception e[/color][color=#007700]) { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"da co loi xay ra."[/color][color=#007700]); } [/color][color=#0000bb]br [/color][color=#007700]= new [/color][color=#0000bb]bufferedreader[/color][color=#007700]([/color][color=#0000bb]fr[/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]"chep vao file 02.txt"[/color][color=#007700]); do { [/color][color=#0000bb]s [/color][color=#007700]= [/color][color=#0000bb]br[/color][color=#007700].[/color][color=#0000bb]readline[/color][color=#007700](); if ([/color][color=#0000bb]s [/color][color=#007700]== [/color][color=#0000bb]null[/color][color=#007700]) { break; } else { [/color][color=#0000bb]s [/color][color=#007700]+= [/color][color=#dd0000]"
"[/color][color=#007700]; [/color][color=#0000bb]fw[/color][color=#007700].[/color][color=#0000bb]write[/color][color=#007700]([/color][color=#0000bb]s[/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]s[/color][color=#007700]); } } while ([/color][color=#0000bb]s [/color][color=#007700]!= [/color][color=#0000bb]null [/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 đã được chép!"[/color][color=#007700]); }}[/color]
</div>
</div>
các bro xem giùm em, phần thân, tại sao khi chạy, in ra console được nhưng lại không thể viết vào file sử dụng fw. em mày mò cả buổi rồi mà không thông.
//code có chỗ nào chưa hay thì chỉ ra giùm em với :d
em cám ơn :d
-
07-17-2014, 07:00 AM #10Junior Member
- Ngày tham gia
- Sep 2014
- Bài viết
- 0
Gửi bởi funnyclock
while((s = b.readline()) != null)
{
s = s + "
";
fw.write(s);
}
Dự án cao cấp Altara Residences Quy Nhơn được xây dựng bởi Công ty CP Foodinco Quy Nhơn hiện đại đẳng cấp đầy phong cách nhịp sống hài hòa. Altara Residences Quy Nhơn hiện đại đẳng cấp khu phát...
Chung cư cao cấp Altara Residences Quy Nhơn hệ thống đồng bộ