-
07-23-2013, 07:00 AM #1Junior Member
- Ngày tham gia
- Apr 2015
- Bài viết
- 0
Bài 27: Đọc và ghi file dùng luồng byte 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í"- với cách đọc ghi này ta tạo một luồng byte gắn với file chỉ định dùng fileinputstream và fileoutputstream.
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/byte-stream-jpg.18381/" border="0" alt="" />
- để mở một file, đơn giản chỉ cần tạo một đối tượng của những lớp này, tên file cần mở là thông số trong constructor. khi file mở, việc đọc và ghi dữ liệu trên file được thực hiện một cách bình thường thông qua các phương thức cung cấp trong luồng.
1, đọc dữ liệu từ file dùng luồng byte:
- mở một file để đọc dữ liệu fileinputstream(string filename) throws filenotfoundexception. nếu file không tồn tại: thì ném ra filenotfoundexception.
- đọc dữ liệu: dùng phương thức read():
int read( ) throws ioexception: đọc từng byte từ file và trả về giá trị của byte đọc được. trả về -1 khi hết file, và ném ra ioexception khi có lỗi đọc.
- đóng file: dùng phương thức close():
void close( ) throws ioexception: sau khi làm việc xong cần đóng file để giải phóng tài nguyên hệ thống đã cấp phát cho file.
ví dụ:
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]ioexception[/color][color=#007700]; [/color][color=#ff8000]/*tạo file androidvn.txt ở ổ e, gõ nội dung văn bản vào đó rồi lưu lại!hiển thị nội dung của một file tên androidvn.txt lưu tại e:\androidvn.txt*/[/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 args[/color][color=#007700][]) [/color][color=#0000bb]throws ioexception [/color][color=#007700]{ [/color][color=#0000bb]fileinputstream f[/color][color=#007700]; try { [/color][color=#0000bb]f [/color][color=#007700]= new [/color][color=#0000bb]fileinputstream[/color][color=#007700]([/color][color=#dd0000]"e:\\androidvn.txt"[/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 not found"[/color][color=#007700]); return; } catch ([/color][color=#0000bb]arrayindexoutofbound***ception 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]"usage: showfile file"[/color][color=#007700]); return; } [/color][color=#ff8000]// đọc cho tới cuối file [/color][color=#0000bb]int i[/color][color=#007700]; do { [/color][color=#0000bb]i [/color][color=#007700]= [/color][color=#0000bb]f[/color][color=#007700].[/color][color=#0000bb]read[/color][color=#007700](); if ([/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].print(([/color][color=#0000bb]char[/color][color=#007700]) [/color][color=#0000bb]i[/color][color=#007700]); } } while ([/color][color=#0000bb]i [/color][color=#007700]!= -[/color][color=#0000bb]1[/color][color=#007700]); [/color][color=#0000bb]f[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); }}[/color]
- mở một file để ghi dữ liệu fileoutputstream(string filename) throws filenotfoundexception
nếu file không tạo được: thì ném ra filenotfoundexception
- ghi dữ liệu xuống: dùng phương thức write():
void write(int byteval) throws ioexception: ghi một byte xác định bởi tham số byteval xuống file, và ném ra ioexception khi có lỗi ghi.
- đóng file: dùng phương thức close():
void close( ) throws ioexception: sau khi làm việc xong cần đóng file để giải phóng tài nguyên hệ thống đã cấp phát cho file.
ví dụ: chương trình sẽ tự tạo file "e:\\output.txt", ghi vào các ký tự từ a -> z
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]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]; 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]fileoutputstream fout[/color][color=#007700]; [/color][color=#ff8000]// tạo file mới! [/color][color=#007700]try { [/color][color=#0000bb]fout [/color][color=#007700]= new [/color][color=#0000bb]fileoutputstream[/color][color=#007700]([/color][color=#dd0000]"e:\\output.txt"[/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]"error opening outputfile "[/color][color=#007700]); return; } [/color][color=#ff8000]// ghi file theo từng ký tự từ a -> z [/color][color=#0000bb]int i [/color][color=#007700]= [/color][color=#dd0000]'a'[/color][color=#007700]; [/color][color=#0000bb]int j [/color][color=#007700]= [/color][color=#dd0000]'z'[/color][color=#007700]; for ([/color][color=#0000bb]i [/color][color=#007700]= [/color][color=#dd0000]'a'[/color][color=#007700]; [/color][color=#0000bb]i [/color][color=#007700]<= [/color][color=#0000bb]j[/color][color=#007700]; [/color][color=#0000bb]i[/color][color=#007700]++) { [/color][color=#0000bb]fout[/color][color=#007700].[/color][color=#0000bb]write[/color][color=#007700]([/color][color=#0000bb]i[/color][color=#007700]); } [/color][color=#0000bb]fout[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); }}[/color]
dưới đây là 1 chương trình đơn giản để copy file trong java, nó chưa thực sự tối ưu vì mình thấy tốc độ của nó rất chậm. bạn có thể dùng để copy bất cứ định dạng nào, không phải chỉ riêng file văn bản! nó dùng cách đọc ghi file dùng luồng byte như trên!
Mã nguồn PHP:[color=#000000]
package javaandroidvn[/color][color=#007700]; [/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]fileinputstream[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]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]; 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]fileinputstream fin[/color][color=#007700]; [/color][color=#0000bb]fileoutputstream fout[/color][color=#007700]; try {[/color][color=#ff8000]// open input file [/color][color=#007700]try { [/color][color=#0000bb]fin [/color][color=#007700]= new [/color][color=#0000bb]fileinputstream[/color][color=#007700]([/color][color=#dd0000]"e:\\in.mp3"[/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]"input file not found"[/color][color=#007700]); return; } [/color][color=#ff8000]// open output file [/color][color=#007700]try { [/color][color=#0000bb]fout [/color][color=#007700]= new [/color][color=#0000bb]fileoutputstream[/color][color=#007700]([/color][color=#dd0000]"e:\\out.mp3"[/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]"error opening outputfile "[/color][color=#007700]); return; } } catch ([/color][color=#0000bb]arrayindexoutofbound***ception 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]"usage: copyfile from to"[/color][color=#007700]); return; } [/color][color=#ff8000]// copy file [/color][color=#0000bb]int i[/color][color=#007700]; try { do { [/color][color=#0000bb]i [/color][color=#007700]= [/color][color=#0000bb]fin[/color][color=#007700].[/color][color=#0000bb]read[/color][color=#007700](); if ([/color][color=#0000bb]i [/color][color=#007700]!= -[/color][color=#0000bb]1[/color][color=#007700]) { [/color][color=#0000bb]fout[/color][color=#007700].[/color][color=#0000bb]write[/color][color=#007700]([/color][color=#0000bb]i[/color][color=#007700]); } } while ([/color][color=#0000bb]i [/color][color=#007700]!= -[/color][color=#0000bb]1[/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]"file error"[/color][color=#007700]); } [/color][color=#0000bb]fin[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); [/color][color=#0000bb]fout[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); }}[/color]
bài tập về nhà::d
biến màn hình console thành màn hình lệnh như sau:
- nếu bạn gõ từ "copy", thì chương trình sẽ hỏi địa chỉ file cần copy, bạn gõ địa chỉ file cần copy vào, nếu không tồn tại sẽ bắt nhập lại, nếu tồn tại sẽ hỏi địa chỉ file đích, sau đó chương trình sẽ copy file đó. tương tự với các lệnh "move", "delete"
- gợi ý cách xóa file:
thêm 2 dòng lệnh như ví dụ này là bạn có thể xóa được 1 file:
Mã nguồn PHP:[color=#000000]
file f [/color][color=#007700]= new [/color][color=#0000bb]file[/color][color=#007700]([/color][color=#dd0000]"e:\\in.mp3"[/color][color=#007700]);[/color][color=#0000bb]f[/color][color=#007700].[/color][color=#0000bb]delete[/color][color=#007700]();[/color]
** làm xong, đừng quên share lên forum để mọi người 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
-
09-21-2013, 07:00 AM #2Junior Member
- Ngày tham gia
- May 2015
- Bài viết
- 0
chào các bạn, đây là bài của mình, mọi người xem và nhận xét nhé
<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][[/color][color=#0000bb]code[/color][color=#007700]][/color][color=#ff8000]/** to change this template, choose tools | templates* and open the template in the editor.*/[/color][color=#0000bb]package javabasic[/color][color=#007700]; [/color][color=#ff8000]/**** @author ha*/[/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].*;public class [/color][color=#0000bb]javabasic [/color][color=#007700]{ [/color][color=#ff8000]/** * @param args the command line arguments */ [/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]switchchosen[/color][color=#007700](); } public static [/color][color=#0000bb]void switchchosen[/color][color=#007700](){ try { [/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]string a[/color][color=#007700], [/color][color=#0000bb]lenh[/color][color=#007700], [/color][color=#0000bb]nguon[/color][color=#007700], [/color][color=#0000bb]dich[/color][color=#007700]; [/color][color=#0000bb]int daucach2[/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 lenh ban muon thuc hien:"[/color][color=#007700]); [/color][color=#0000bb]a [/color][color=#007700]= [/color][color=#0000bb]input[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700]().[/color][color=#0000bb]trim[/color][color=#007700](); [/color][color=#0000bb]int daucach1 [/color][color=#007700]= [/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]indexof[/color][color=#007700]([/color][color=#dd0000]" "[/color][color=#007700]); [/color][color=#0000bb]lenh [/color][color=#007700]= [/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]substring[/color][color=#007700]([/color][color=#0000bb]0[/color][color=#007700], [/color][color=#0000bb]daucach1[/color][color=#007700]); [/color][color=#0000bb]int b[/color][color=#007700]; if([/color][color=#0000bb]lenh[/color][color=#007700].[/color][color=#0000bb]equalsignorecase[/color][color=#007700]([/color][color=#dd0000]"copy"[/color][color=#007700])) { [/color][color=#0000bb]b [/color][color=#007700]= [/color][color=#0000bb]1[/color][color=#007700]; } else if ([/color][color=#0000bb]lenh[/color][color=#007700].[/color][color=#0000bb]equalsignorecase[/color][color=#007700]([/color][color=#dd0000]"move"[/color][color=#007700])){ [/color][color=#0000bb]b [/color][color=#007700]= [/color][color=#0000bb]2[/color][color=#007700]; } else if ([/color][color=#0000bb]lenh[/color][color=#007700].[/color][color=#0000bb]equalsignorecase[/color][color=#007700]([/color][color=#dd0000]"delete"[/color][color=#007700])){ [/color][color=#0000bb]b [/color][color=#007700]= [/color][color=#0000bb]3[/color][color=#007700]; } else [/color][color=#0000bb]b [/color][color=#007700]= [/color][color=#0000bb]4[/color][color=#007700]; switch ([/color][color=#0000bb]b[/color][color=#007700]){ case [/color][color=#0000bb]1[/color][color=#007700]: { [/color][color=#0000bb]daucach2 [/color][color=#007700]= [/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]substring[/color][color=#007700]([/color][color=#0000bb]daucach1 [/color][color=#007700]+ [/color][color=#0000bb]1[/color][color=#007700]).[/color][color=#0000bb]indexof[/color][color=#007700]([/color][color=#dd0000]" "[/color][color=#007700]) + [/color][color=#0000bb]1 [/color][color=#007700]+ [/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]substring[/color][color=#007700]([/color][color=#0000bb]0[/color][color=#007700],[/color][color=#0000bb]daucach1[/color][color=#007700]).[/color][color=#0000bb]length[/color][color=#007700](); [/color][color=#0000bb]nguon [/color][color=#007700]= [/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]substring[/color][color=#007700]([/color][color=#0000bb]daucach1 [/color][color=#007700]+ [/color][color=#0000bb]1[/color][color=#007700], [/color][color=#0000bb]daucach2[/color][color=#007700]); [/color][color=#0000bb]dich [/color][color=#007700]= [/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]substring[/color][color=#007700]([/color][color=#0000bb]daucach2 [/color][color=#007700]+ [/color][color=#0000bb]1[/color][color=#007700]); [/color][color=#0000bb]copyfile[/color][color=#007700]([/color][color=#0000bb]nguon[/color][color=#007700],[/color][color=#0000bb]dich[/color][color=#007700]); break; } case [/color][color=#0000bb]2[/color][color=#007700]: [/color][color=#0000bb]daucach2 [/color][color=#007700]= [/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]substring[/color][color=#007700]([/color][color=#0000bb]daucach1 [/color][color=#007700]+ [/color][color=#0000bb]1[/color][color=#007700]).[/color][color=#0000bb]indexof[/color][color=#007700]([/color][color=#dd0000]" "[/color][color=#007700]) + [/color][color=#0000bb]1 [/color][color=#007700]+ [/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]substring[/color][color=#007700]([/color][color=#0000bb]0[/color][color=#007700],[/color][color=#0000bb]daucach1[/color][color=#007700]).[/color][color=#0000bb]length[/color][color=#007700](); [/color][color=#0000bb]nguon [/color][color=#007700]= [/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]substring[/color][color=#007700]([/color][color=#0000bb]daucach1 [/color][color=#007700]+ [/color][color=#0000bb]1[/color][color=#007700], [/color][color=#0000bb]daucach2[/color][color=#007700]); [/color][color=#0000bb]dich [/color][color=#007700]= [/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]substring[/color][color=#007700]([/color][color=#0000bb]daucach2 [/color][color=#007700]+ [/color][color=#0000bb]1[/color][color=#007700]); [/color][color=#0000bb]movefile[/color][color=#007700]([/color][color=#0000bb]nguon[/color][color=#007700], [/color][color=#0000bb]dich[/color][color=#007700]); break; case [/color][color=#0000bb]3[/color][color=#007700]: [/color][color=#0000bb]nguon [/color][color=#007700]= [/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]substring[/color][color=#007700]([/color][color=#0000bb]daucach1 [/color][color=#007700]+ [/color][color=#0000bb]1[/color][color=#007700]); [/color][color=#0000bb]deletefile[/color][color=#007700]([/color][color=#0000bb]nguon[/color][color=#007700]); break; case [/color][color=#0000bb]4[/color][color=#007700]: [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#dd0000]"lenh nhap vao sai!"[/color][color=#007700]); break; } } 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=#dd0000]"loi nay la loi gi? " [/color][color=#007700]+ [/color][color=#0000bb]ex[/color][color=#007700]); } } public static [/color][color=#0000bb]void movefile [/color][color=#007700]([/color][color=#0000bb]string soufile[/color][color=#007700], [/color][color=#0000bb]string desfile[/color][color=#007700]) [/color][color=#0000bb]throws exception[/color][color=#007700]{ [/color][color=#0000bb]fileinputstream filein [/color][color=#007700]= [/color][color=#0000bb]null[/color][color=#007700]; [/color][color=#0000bb]fileoutputstream fileout [/color][color=#007700]= [/color][color=#0000bb]null[/color][color=#007700]; [/color][color=#0000bb]file file [/color][color=#007700]= [/color][color=#0000bb]null[/color][color=#007700]; try { [/color][color=#0000bb]filein [/color][color=#007700]= new [/color][color=#0000bb]fileinputstream[/color][color=#007700]([/color][color=#0000bb]soufile[/color][color=#007700]); [/color][color=#0000bb]file [/color][color=#007700]= new [/color][color=#0000bb]file[/color][color=#007700]([/color][color=#0000bb]soufile[/color][color=#007700]); [/color][color=#0000bb]fileout [/color][color=#007700]= new [/color][color=#0000bb]fileoutputstream[/color][color=#007700]([/color][color=#0000bb]desfile[/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=#dd0000]"loi khoi tao file xuat hien: " [/color][color=#007700]+ [/color][color=#0000bb]ex[/color][color=#007700]); } [/color][color=#0000bb]int i[/color][color=#007700]; try { do { [/color][color=#0000bb]i [/color][color=#007700]= [/color][color=#0000bb]filein[/color][color=#007700].[/color][color=#0000bb]read[/color][color=#007700](); if ([/color][color=#0000bb]i [/color][color=#007700]!= -[/color][color=#0000bb]1[/color][color=#007700]){ [/color][color=#0000bb]fileout[/color][color=#007700].[/color][color=#0000bb]write[/color][color=#007700]([/color][color=#0000bb]i[/color][color=#007700]); } } while ([/color][color=#0000bb]i [/color][color=#007700]!= -[/color][color=#0000bb]1[/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=#dd0000]"loi doc va ghi file: " [/color][color=#007700]+ [/color][color=#0000bb]ex[/color][color=#007700]); } [/color][color=#0000bb]filein[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); [/color][color=#0000bb]fileout[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); [/color][color=#0000bb]file[/color][color=#007700].[/color][color=#0000bb]delete[/color][color=#007700](); } public static [/color][color=#0000bb]void copyfile[/color][color=#007700]([/color][color=#0000bb]string inputfile[/color][color=#007700], [/color][color=#0000bb]string outputfile[/color][color=#007700]) [/color][color=#0000bb]throws ioexception[/color][color=#007700]{ [/color][color=#0000bb]fileinputstream fin [/color][color=#007700]= [/color][color=#0000bb]null[/color][color=#007700]; [/color][color=#0000bb]fileoutputstream fout [/color][color=#007700]= [/color][color=#0000bb]null[/color][color=#007700]; [/color][color=#ff8000]// mo file input [/color][color=#007700]try { [/color][color=#0000bb]fin [/color][color=#007700]= new [/color][color=#0000bb]fileinputstream[/color][color=#007700]([/color][color=#0000bb]inputfile[/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].print([/color][color=#dd0000]"co loi xay ra voi input"[/color][color=#007700]); } [/color][color=#ff8000]// tao file output [/color][color=#007700]try { [/color][color=#0000bb]fout [/color][color=#007700]= new [/color][color=#0000bb]fileoutputstream[/color][color=#007700]([/color][color=#0000bb]outputfile[/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=#dd0000]"co loi xay ra voi output"[/color][color=#007700]); } [/color][color=#ff8000]// doc va ghi file [/color][color=#0000bb]int i[/color][color=#007700]; do { [/color][color=#0000bb]i [/color][color=#007700]= [/color][color=#0000bb]fin[/color][color=#007700].[/color][color=#0000bb]read[/color][color=#007700](); if([/color][color=#0000bb]i [/color][color=#007700]!= -[/color][color=#0000bb]1[/color][color=#007700]){ [/color][color=#0000bb]fout[/color][color=#007700].[/color][color=#0000bb]write[/color][color=#007700]([/color][color=#0000bb]i[/color][color=#007700]); } } while ([/color][color=#0000bb]i [/color][color=#007700]!= -[/color][color=#0000bb]1[/color][color=#007700]); [/color][color=#0000bb]fin[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); [/color][color=#0000bb]fout[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); } public static [/color][color=#0000bb]void deletefile[/color][color=#007700]([/color][color=#0000bb]string soufile[/color][color=#007700]){ try { [/color][color=#0000bb]string yes[/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]file file [/color][color=#007700]= new [/color][color=#0000bb]file[/color][color=#007700]([/color][color=#0000bb]soufile[/color][color=#007700]); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#dd0000]"ban chac chan xoa? nhap 1 neu dong y, nhap 0 neu huy xoa: "[/color][color=#007700]); [/color][color=#0000bb]yes [/color][color=#007700]= [/color][color=#0000bb]input[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700](); if ([/color][color=#0000bb]yes[/color][color=#007700].[/color][color=#0000bb]equals[/color][color=#007700]([/color][color=#dd0000]"1"[/color][color=#007700])){ [/color][color=#0000bb]file[/color][color=#007700].[/color][color=#0000bb]delete[/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]"xoa thanh cong!"[/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]"ban da huy xoa file!"[/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=#dd0000]"loi khi xoa file: " [/color][color=#007700]+ [/color][color=#0000bb]ex[/color][color=#007700]); } }}[/[/color][color=#0000bb]code[/color][color=#007700]][/color]
</div>
</div>
-
09-21-2013, 07:00 AM #3Junior Member
- Ngày tham gia
- Jan 2015
- Bài viết
- 0
mình không hiểu trong catch sao lại có return
-
10-01-2013, 07:00 AM #4Junior Member
- Ngày tham gia
- Apr 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ã:import java.io.file; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import java.util.scanner; public class btbai27 { public static void main(string[]args) throws ioexception{ btbai27 bt=new btbai27(); system.out.println("menu:"); int i; system.out.println("nhap yeu cau: "); scanner in=new scanner(system.in); i=in.nextint(); in.nextline(); string s1,s2; switch(i) { case 1: { system.out.println("this is copy"); system.out.println("dia chi file can copy: "); s1=in.nextline(); system.out.println("dia chi file dich: "); s2=in.nextline(); copy(s1,s2); break; } case 2: { system.out.println("this is move:"); system.out.println("dia chi file can move: "); s1=in.nextline(); system.out.println("dia chi file dich: "); s2=in.nextline(); try{ copy(s1,s2); delete(s1); }catch(exception exc){ system.out.println("error file"); } break; } case 3: { system.out.println("this is delete"); system.out.println("dia chi file can xoa:"); s1=in.nextline(); delete(s1); break; } default: { system.out.println("try again"); break; } } } 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(); } public static void delete(string s) { try{ file f=new file(s); f.delete(); }catch(exception exc){ system.out.println("file error "); return; } } }
</div>
</div>
-
10-05-2013, 07:00 AM #5Banned
- Ngày tham gia
- Feb 2014
- Bài viết
- 0
Gửi bởi haihack
-
10-06-2013, 07:00 AM #6Junior Member
- Ngày tham gia
- Apr 2016
- Bài viết
- 0
Gửi bởi cuongbk
-
10-06-2013, 07:00 AM #7Junior Member
- Ngày tham gia
- Apr 2016
- 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>import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.util.scanner;
import java.io.file;
class copy
{
public void copyfile()throws ioexception
{
fileinputstream f = null;
string s;
int i=0;
scanner sc=new scanner(system.in);
do{
i=0;
system.out.print("nhập địa chỉ file cần copy: ");
s=sc.nextline().trim();
try{
f=new fileinputstream(s);
}catch(filenotfoundexception e){
i=1;
system.out.println("không tìm thấy file, vui lòng nhập lại: ");
}
}while(i==1);
fileoutputstream fo = null;
do{
i=0;
system.out.print("nhập địa chỉ file đích: ");
string st=sc.nextline().trim();
try{
fo=new fileoutputstream(st);
}catch(filenotfoundexception e){
i=1;
system.out.println("địa chỉ file đích sai, vui lòng nhập lại: ");
}
}while(i==1);
int j=0;
do
{
j=f.read();
if(j!=-1)
fo.write(j);
}while(j!=-1);
f.close();
fo.close();
}
}
class move
{
public void move()throws ioexception
{
file f2;
scanner sc=new scanner(system.in);
fileinputstream f=null;
string s=null;
int i=0;
do
{
i=0;
system.out.print("nhập địa chỉ file muốn di chuyển: ");
s=sc.nextline().trim();
try{
f=new fileinputstream(s);
}catch(filenotfoundexception e)
{
i=1;
system.out.println("không tìm thấy file, nhập lại: ");
}
}while(i==1);
file f1=new file(s);
fileoutputstream f0=null;
do
{
i=0;
system.out.print("nhập địa chỉ muốn chuyển file đến : ");
string ss=sc.nextline().trim()+"\\\\"+f1.getname();
try{
f0=new fileoutputstream(ss);
}catch(filenotfoundexception e)
{
i=1;
system.out.println("địa chỉ sai, nhập lại: ");
}
}while(i==1);
int j=0;
do
{
j=f.read();
if(j!=-1)
f0.write(j);
}while(j!=-1);
f.close();
f0.close();
f1.delete();
}
}
class delete
{
public void delete()throws ioexception
{
int i=0;
string s;
scanner sc=new scanner(system.in);
file f;
fileinputstream f1=null;
do{
i=0;
system.out.print("nhập file cần xóa: ");
s=sc.nextline().trim();
try
{
f1=new fileinputstream(s);
}catch(filenotfoundexception e)
{
i=1;
system.out.println("không tìm thấy file, nhập lại: ");
}
}while(i==1);
f1.close();
f=new file(s);
f.delete();
}
}
public class doghifile {
public static void main(string[] args)throws exception {
scanner s=new scanner(system.in);
system.out.println("---------------------------menu---------------------------");
system.out.println("mời bạn nhập: copy/move/delete file: ");
string st=s.nextline().trim();
if(st.comparetoignorecase("copy")==0)
{
copy c=new copy();
c.copyfile();
}
else if(st.comparetoignorecase("move")==0)
{
move m=new move();
m.move();
}
else if(st.comparetoignorecase("delete")==0)
{
delete d=new delete();
d.delete();
}
else
{
system.out.println("bạn đã nhập sai");
system.exit(0);
}
}
}
</blockquote></div>
</div>
</div>
-
10-12-2013, 07:00 AM #8Junior Member
- Ngày tham gia
- Mar 2015
- Bài viết
- 0
<div class="bbcodeblock bbcodequote bbcmspoilerblock">
<div class="attribution type"><noscript>spoiler (move your mouse to the spoiler area to reveal the content)</noscript>
show spoiler
hide spoiler
</div>
<div class="quotecontent">
<div class="bbcm_spoiler_noscript"><blockquote>
Mã nguồn PHP:[color=#000000]
package bytestream[/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]ioexception[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]lang[/color][color=#007700].[/color][color=#0000bb]string[/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=#ff8000]/**** @author admin*/[/color][color=#007700]public class [/color][color=#0000bb]readfile [/color][color=#007700]{ public static [/color][color=#0000bb]void read[/color][color=#007700]([/color][color=#0000bb]string input[/color][color=#007700]) [/color][color=#0000bb]throws ioexception[/color][color=#007700]{ [/color][color=#ff8000]//scanner nhap=new scanner(system.in); [/color][color=#0000bb]fileinputstream fin [/color][color=#007700]= [/color][color=#0000bb]null[/color][color=#007700]; [/color][color=#ff8000]//system.out.println("nhap vao file can doc: "); //string a =nhap.nextline(); [/color][color=#007700]try{ [/color][color=#0000bb]fin[/color][color=#007700]=new [/color][color=#0000bb]fileinputstream[/color][color=#007700]([/color][color=#0000bb]input[/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]arrayindexoutofbound***ception 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]"usage: showfile file"[/color][color=#007700]); } [/color][color=#0000bb]int i[/color][color=#007700]; do { [/color][color=#0000bb]i[/color][color=#007700]=[/color][color=#0000bb]fin[/color][color=#007700].[/color][color=#0000bb]read[/color][color=#007700](); if([/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=#0000bb]char[/color][color=#007700]) [/color][color=#0000bb]i[/color][color=#007700]); } } while ([/color][color=#0000bb]i[/color][color=#007700]!=-[/color][color=#0000bb]1[/color][color=#007700]); [/color][color=#0000bb]fin[/color][color=#007700].[/color][color=#0000bb]close[/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=#ff8000]/**** @author admin*/[/color][color=#007700]public class [/color][color=#0000bb]writefile [/color][color=#007700]{ public static [/color][color=#0000bb]void write[/color][color=#007700]([/color][color=#0000bb]string output[/color][color=#007700]) [/color][color=#0000bb]throws ioexception[/color][color=#007700]{ [/color][color=#0000bb]fileoutputstream fout [/color][color=#007700]= [/color][color=#0000bb]null[/color][color=#007700]; try { [/color][color=#0000bb]fout [/color][color=#007700]=new [/color][color=#0000bb]fileoutputstream[/color][color=#007700]([/color][color=#0000bb]output[/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]); } [/color][color=#0000bb]fout[/color][color=#007700].[/color][color=#0000bb]close[/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=#ff8000]/**** @author admin*/[/color][color=#007700]public class [/color][color=#0000bb]copyfile [/color][color=#007700]{ public static [/color][color=#0000bb]void copy[/color][color=#007700]([/color][color=#0000bb]string input[/color][color=#007700],[/color][color=#0000bb]string output[/color][color=#007700]) [/color][color=#0000bb]throws ioexception[/color][color=#007700]{ [/color][color=#0000bb]fileinputstream fin[/color][color=#007700]=[/color][color=#0000bb]null[/color][color=#007700]; [/color][color=#0000bb]fileoutputstream fout[/color][color=#007700]=[/color][color=#0000bb]null[/color][color=#007700]; try{ [/color][color=#0000bb]fin[/color][color=#007700]= new [/color][color=#0000bb]fileinputstream[/color][color=#007700]([/color][color=#0000bb]input[/color][color=#007700]); [/color][color=#0000bb]fout[/color][color=#007700]=new [/color][color=#0000bb]fileoutputstream[/color][color=#007700]([/color][color=#0000bb]output[/color][color=#007700]); [/color][color=#0000bb]byte[/color][color=#007700][] [/color][color=#0000bb]buffer[/color][color=#007700]=new [/color][color=#0000bb]byte[/color][color=#007700][[/color][color=#0000bb]1024[/color][color=#007700]]; [/color][color=#0000bb]int lenght[/color][color=#007700]; while(([/color][color=#0000bb]lenght[/color][color=#007700]=[/color][color=#0000bb]fin[/color][color=#007700].[/color][color=#0000bb]read[/color][color=#007700]([/color][color=#0000bb]buffer[/color][color=#007700]))>[/color][color=#0000bb]0[/color][color=#007700]){ [/color][color=#0000bb]fout[/color][color=#007700].[/color][color=#0000bb]write[/color][color=#007700]([/color][color=#0000bb]buffer[/color][color=#007700], [/color][color=#0000bb]0[/color][color=#007700], [/color][color=#0000bb]lenght[/color][color=#007700]); } [/color][color=#0000bb]fin[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); [/color][color=#0000bb]fout[/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]"thanh cong"[/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]"error: "[/color][color=#007700]+[/color][color=#0000bb]e[/color][color=#007700].[/color][color=#0000bb]tostring[/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=#ff8000]/**** @author admin*/[/color][color=#007700]public class [/color][color=#0000bb]demo [/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]readfile readfile[/color][color=#007700]=new [/color][color=#0000bb]readfile[/color][color=#007700](); [/color][color=#0000bb]writefile wfile[/color][color=#007700]=new [/color][color=#0000bb]writefile[/color][color=#007700](); [/color][color=#0000bb]copyfile cfile[/color][color=#007700]=new [/color][color=#0000bb]copyfile[/color][color=#007700](); [/color][color=#0000bb]scanner nhap[/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]"******menu******"[/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]"1.doc file"[/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]"2.ghi file"[/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]"3.copy file"[/color][color=#007700]); [/color][color=#0000bb]int chon[/color][color=#007700]=[/color][color=#0000bb]nhap[/color][color=#007700].[/color][color=#0000bb]nextint[/color][color=#007700](); [/color][color=#0000bb]nhap[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700](); switch ([/color][color=#0000bb]chon[/color][color=#007700]) { case [/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]"nhap file can doc: "[/color][color=#007700]); [/color][color=#0000bb]string input[/color][color=#007700]=[/color][color=#0000bb]nhap[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700](); [/color][color=#0000bb]readfile[/color][color=#007700].[/color][color=#0000bb]read[/color][color=#007700]([/color][color=#0000bb]input[/color][color=#007700]); break; case [/color][color=#0000bb]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]"nhap file output: "[/color][color=#007700]); [/color][color=#0000bb]string output[/color][color=#007700]=[/color][color=#0000bb]nhap[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700](); [/color][color=#0000bb]wfile[/color][color=#007700].[/color][color=#0000bb]write[/color][color=#007700]([/color][color=#0000bb]output[/color][color=#007700]); case [/color][color=#0000bb]3[/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 file input: "[/color][color=#007700]); [/color][color=#0000bb]string vao[/color][color=#007700]=[/color][color=#0000bb]nhap[/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]"nhap file output: "[/color][color=#007700]); [/color][color=#0000bb]string ra[/color][color=#007700]=[/color][color=#0000bb]nhap[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700](); [/color][color=#0000bb]cfile[/color][color=#007700].[/color][color=#0000bb]copy[/color][color=#007700]([/color][color=#0000bb]vao[/color][color=#007700], [/color][color=#0000bb]ra[/color][color=#007700]); default: [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"ban nhap sai roi."[/color][color=#007700]); } } }[/color]
</div>
</div>
-
10-14-2013, 07:00 AM #9Junior Member
- Ngày tham gia
- Oct 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]
[/color][color=#ff8000]//biến màn hình console thành màn hình lệnh như sau://- nếu bạn gõ từ "copy", thì chương trình sẽ hỏi địa chỉ file cần copy,//bạn gõ địa chỉ file cần copy vào, nếu không tồn tại sẽ bắt nhập lại,//nếu tồn tại sẽ hỏi địa chỉ file đích,//sau đó chương trình sẽ copy file đó. tương tự với các lệnh "move", "delete"[/color][color=#0000bb]package bai27[/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];[/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];public class [/color][color=#0000bb]bai27 [/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]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]string a[/color][color=#007700],[/color][color=#0000bb]nguon[/color][color=#007700],[/color][color=#0000bb]dich[/color][color=#007700],[/color][color=#0000bb]nguon1 [/color][color=#007700]= [/color][color=#0000bb]null[/color][color=#007700],[/color][color=#0000bb]dich1[/color][color=#007700],[/color][color=#0000bb]xoa [/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]"nhập lệnh muốn thực hiện(copy/move/delete): "[/color][color=#007700]); [/color][color=#0000bb]a [/color][color=#007700]= [/color][color=#0000bb]in[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700](); [/color][color=#0000bb]fileoutputstream fout [/color][color=#007700]= [/color][color=#0000bb]null[/color][color=#007700]; [/color][color=#0000bb]fileinputstream fin [/color][color=#007700]= [/color][color=#0000bb]null[/color][color=#007700]; if([/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]equals[/color][color=#007700]([/color][color=#dd0000]"copy"[/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]1[/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]"nhập địa chỉ file cần copy: "[/color][color=#007700]); [/color][color=#0000bb]nguon [/color][color=#007700]= [/color][color=#0000bb]in[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700](); try { [/color][color=#0000bb]fin [/color][color=#007700]= new [/color][color=#0000bb]fileinputstream[/color][color=#007700]([/color][color=#0000bb]nguon[/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]"địa chỉ file không tồn tại. nhập lại"[/color][color=#007700]); [/color][color=#0000bb]i[/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]1[/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]"copy đến: "[/color][color=#007700]); [/color][color=#0000bb]dich [/color][color=#007700]= [/color][color=#0000bb]in[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700](); try { [/color][color=#0000bb]fout [/color][color=#007700]= new [/color][color=#0000bb]fileoutputstream[/color][color=#007700]([/color][color=#0000bb]dich[/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]"địa chỉ file không tồn tại. nhập lại"[/color][color=#007700]); [/color][color=#0000bb]i[/color][color=#007700]--; }catch([/color][color=#0000bb]arrayindexoutofbound***ception 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]"lỗi"[/color][color=#007700]); } } try{ [/color][color=#0000bb]int x[/color][color=#007700]; do{ [/color][color=#0000bb]x [/color][color=#007700]= [/color][color=#0000bb]fin[/color][color=#007700].[/color][color=#0000bb]read[/color][color=#007700](); if([/color][color=#0000bb]x [/color][color=#007700]!=-[/color][color=#0000bb]1[/color][color=#007700]){ [/color][color=#0000bb]fout[/color][color=#007700].[/color][color=#0000bb]write[/color][color=#007700]([/color][color=#0000bb]x[/color][color=#007700]); } }while([/color][color=#0000bb]x [/color][color=#007700]!= -[/color][color=#0000bb]1[/color][color=#007700]); }catch([/color][color=#0000bb]ioexception ex[/color][color=#007700]){ [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"error file"[/color][color=#007700]); } [/color][color=#0000bb]fin[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); [/color][color=#0000bb]fout[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); }else if([/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]equals[/color][color=#007700]([/color][color=#dd0000]"move"[/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]1[/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]"nhập địa chỉ file cần di chuyển: "[/color][color=#007700]); [/color][color=#0000bb]nguon1 [/color][color=#007700]= [/color][color=#0000bb]in[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700](); try { [/color][color=#0000bb]fin [/color][color=#007700]= new [/color][color=#0000bb]fileinputstream[/color][color=#007700]([/color][color=#0000bb]nguon1[/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]"địa chỉ file không tồn tại. nhập lại"[/color][color=#007700]); [/color][color=#0000bb]i[/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]1[/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]"chuyển đến: "[/color][color=#007700]); [/color][color=#0000bb]dich1 [/color][color=#007700]= [/color][color=#0000bb]in[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700](); try { [/color][color=#0000bb]fout [/color][color=#007700]= new [/color][color=#0000bb]fileoutputstream[/color][color=#007700]([/color][color=#0000bb]dich1[/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]"địa chỉ file không tồn tại. nhập lại"[/color][color=#007700]); [/color][color=#0000bb]i[/color][color=#007700]--; }catch([/color][color=#0000bb]arrayindexoutofbound***ception 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]"lỗi"[/color][color=#007700]); } } try{ [/color][color=#0000bb]int x[/color][color=#007700]; do{ [/color][color=#0000bb]x [/color][color=#007700]= [/color][color=#0000bb]fin[/color][color=#007700].[/color][color=#0000bb]read[/color][color=#007700](); if([/color][color=#0000bb]x [/color][color=#007700]!=-[/color][color=#0000bb]1[/color][color=#007700]){ [/color][color=#0000bb]fout[/color][color=#007700].[/color][color=#0000bb]write[/color][color=#007700]([/color][color=#0000bb]x[/color][color=#007700]); } }while([/color][color=#0000bb]x [/color][color=#007700]!= -[/color][color=#0000bb]1[/color][color=#007700]); }catch([/color][color=#0000bb]ioexception ex[/color][color=#007700]){ [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"error file"[/color][color=#007700]); } [/color][color=#0000bb]fin[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); [/color][color=#0000bb]fout[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); try{ [/color][color=#0000bb]file file [/color][color=#007700]= new [/color][color=#0000bb]file[/color][color=#007700]([/color][color=#0000bb]nguon1[/color][color=#007700]); if([/color][color=#0000bb]file[/color][color=#007700].[/color][color=#0000bb]delete[/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]file[/color][color=#007700].[/color][color=#0000bb]getname[/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]"không thể xóa file gốc"[/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]"lỗi"[/color][color=#007700]+[/color][color=#0000bb]e[/color][color=#007700]); } }else if([/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]equals[/color][color=#007700]([/color][color=#dd0000]"delete"[/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]1[/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]"file cần xóa là: "[/color][color=#007700]); [/color][color=#0000bb]xoa [/color][color=#007700]= [/color][color=#0000bb]in[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700](); try{ [/color][color=#0000bb]fin [/color][color=#007700]= new [/color][color=#0000bb]fileinputstream[/color][color=#007700]([/color][color=#0000bb]xoa[/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]"file không tồn tại. nhập lại!!!"[/color][color=#007700]); [/color][color=#0000bb]i[/color][color=#007700]--; } } [/color][color=#0000bb]fin[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); try{ [/color][color=#0000bb]file file1 [/color][color=#007700]= new [/color][color=#0000bb]file[/color][color=#007700]([/color][color=#0000bb]xoa[/color][color=#007700]); if([/color][color=#0000bb]file1[/color][color=#007700].[/color][color=#0000bb]delete[/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]file1[/color][color=#007700].[/color][color=#0000bb]getname[/color][color=#007700]()+[/color][color=#dd0000]" xóa"[/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]"không thể xóa file"[/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=#dd0000]"lỗi "[/color][color=#007700]+ [/color][color=#0000bb]ex[/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]"nhập sai lệnh !!!"[/color][color=#007700]); }}}[/color]
</div>
</div>
-
10-14-2013, 07:00 AM #10Junior Member
- Ngày tham gia
- Nov 2014
- Bài viết
- 0
bạn nào giải thích hộ mình tại sao lại có return trong catch() thế?
Một trong những điểm mạnh của Công ty TNHH SX TM Thép Đông Anh chính là sự linh hoạt trong việc đáp ứng nhu cầu đa dạng của khách hàng. Chúng tôi hiểu rằng mỗi công trình xây dựng đều có những yêu...
Lưới thép hàn Đông Anh - Được các nhà thầu tin cậy và lựa chọn