package com.gec.hello.Exer10; import org.junit.Test; import java.io.*; public class FileInputOutputTest { @Test public void test() { FileInputStream fis=null; FileOutputStream fos=null; try { //创建相关的File类对象 File srcFile = new File("001.png"); File destFile = new File("001-c.png"); //创建相关的字节流 fis = new FileInputStream(srcFile); fos = new FileOutputStream(destFile); //数据的读入和写出 byte[] buffer = new byte[1024]; int len;//记录每次读入到buffer中字节的个数 while((len=fis.read(buffer))!=-1) { fos.write(buffer, 0, len); } }catch (IOException e){ e.printStackTrace(); }finally { //关闭流资源 try { if(fis!=null) fis.close(); }catch (IOException e){ e.printStackTrace(); }try { if(fos!=null) fos.close(); }catch (IOException e){ e.printStackTrace(); } } } }
java-io流之FileInputStream和FileOutputStream使用
最新推荐文章于 2024-07-31 19:38:21 发布