import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/*
-
编写一个java程序实现文件复制功能,
-
要求将e:/io/copysrc.doc中的内容复制到
-
f:/io/copydes.doc中。 * */
public class Test2 {
public static void main(String[] args) {
copyFile();
}public static void copyFile() {
File src = new File(“G:\io\copysrc.doc”);
File dest = new File(“H:\Hbuilder\copyesrc.doc”);
// 定义文件输入流和输出流对象
FileInputStream fis = null;// 输入流
FileOutputStream fos = null;// 输出流try { fis = new FileInputStream(src);//文件输入流 fos = new FileOutputStream(dest);//文件输出 byte[] bs = new byte[1024];//定义一个byte数组存放 while (true) { int len = fis.read(bs, 0, bs.length);//读出这个文件的全部文件 if (len == -1) {//-1值得是负数就是读不到数据是时候 break; } else { fos.write(bs, 0, len); //输出将读到的数据存放在bs的数组中 } } System.out.println("复制文件成功"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } }
}
-