|
|
Danh hiệu: Administration
Nhóm: Administrators
Gia nhập: 23-07-2013(UTC) Bài viết: 6,117   Đến từ: Vietnam Cảm ơn: 10 lần Được cảm ơn: 2 lần trong 2 bài viết
|
Mã:/*
Copy binary file using Streams
This example shows how to copy a binary file using Java FileInputStream
and FileOutputStream classes. If you want to copy text file use
FileReader and FileWriter classes instead of FileInputStream and
FileOutputStream classes.
*/
import java.io.FileNotFoundException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyBinaryFile {
public static void main(String[] args) {
String strSourceFile="C:/FileIO/source.dat";
String strDestinationFile="C:/FileIO/dest.dat";
try
{
//create FileInputStream object for source file
FileInputStream fin = new FileInputStream(strSourceFile);
//create FileOutputStream object for destination file
FileOutputStream fout = new FileOutputStream(strDestinationFile);
byte[] b = new byte[1024];
int noOfBytes = 0;
System.out.println("Copying file using streams");
//read bytes from source file and write to destination file
while( (noOfBytes = fin.read(b)) != -1 )
{
fout.write(b, 0, noOfBytes);
}
System.out.println("File copied!");
//close the streams
fin.close();
fout.close();
}
catch(FileNotFoundException fnf)
{
System.out.println("Specified file not found :" + fnf);
}
catch(IOException ioe)
{
System.out.println("Error while copying file :" + ioe);
}
}
}
/*
Typical output would be
Copying file using streams
File copied!
*/
|
|
|
|
|
|
Di chuyển
Bạn không thể tạo chủ đề mới trong diễn đàn này.
Bạn không thể trả lời chủ đề trong diễn đàn này.
Bạn không thể xóa bài của bạn trong diễn đàn này.
Bạn không thể sửa bài của bạn trong diễn đàn này.
Bạn không thể tạo bình chọn trong diễn đàn này.
Bạn không thể bỏ phiếu bình chọn trong diễn đàn này.