[ Java ] Chương trình Copy file [code Java]

Ví dụ: Viết chương trình Copy file (lưu ý, bạn phải tạo file cần copy trước khi Run)




/******************************************************************************
* File : Copier.java
* Author : http://lap-trinh-may-tinh.blogspot.com/
* Description :
* Simple file copying
* Tested with : JDK 1.6
******************************************************************************/
import java.io.*;
public class Copier
{
public static void main(String[] args) throws Exception
{
String source_file="Copier.java"; // copy myself
String dest_file = "Copier.java.1"; // name of the copy
FileInputStream fis=new FileInputStream(source_file);
FileOutputStream fos=new FileOutputStream(dest_file);
int BUF_SIZE=1024;
byte[] buffer=new byte[BUF_SIZE]; // 1K copy buffer
while (true)
{
int n=fis.read(buffer); // read
if (n>0) fos.write(buffer,0,n); // copy
if (n<0) break; // EOF
}
}
}

Một số tài liệu và khoá học bổ ích dành cho bạn: 

# Tài liệu: Lập trình hướng đối tượng JAVA core dành cho người mới bắt đầu học lập trình [Click để xem]

# Khoá học online: Lập trình Java trong 4 tuần [Click để xem]

Tìm kiếm nội dung khác: