当前位置: 代码迷 >> 综合 >> Java文件复制操作(FileInputStream、FileOutputStream)
  详细解决方案

Java文件复制操作(FileInputStream、FileOutputStream)

热度:11   发布时间:2023-11-25 15:28:26.0

Java文件复制(FileInputStream、FileOutputStream)

package IO流;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;/*** 复制文件* */
public class Test2 {public static void main(String[] args) {//定义变量FileInputStream fi=null;FileOutputStream fo=null;try {//初始化IO流对象fi=new FileInputStream("d:/temp/test.zip");fo=new FileOutputStream("d:/temp2/test2.zip");//定义字节数组,用来存放输入流读取的字节byte[] b=new byte[1024];//定义一个整型len,主要用来记录输入流读取字节长度,//1 判断文件是否读取完毕 2 确定输出流写入到文件的字节数int len=0;//输入流读取文件到末尾时,返回-1while((len=fi.read(b))!=-1) {//输出流将字节数组写入到文件中去fo.write(b, 0, len);}System.out.println("复制完成!");} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {//关闭IO流if(fi!=null) {try {fi.close();} catch (IOException e) {e.printStackTrace();}}if(fo!=null) {try {fo.close();} catch (IOException e) {e.printStackTrace();}}}}}

运行结果

复制完成!

理解字节输入流、以及字节输出流原理

  相关解决方案