Java IO流小例

来源:互联网 发布:俄罗斯域名注册 编辑:程序博客网 时间:2024/06/08 09:48

测试io流实现复制,上传等时使用图片进行测试较好...

import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;public class Main {public static void main(String[] args) {File file = new File("add.bmp");InputStream in = null;OutputStream out = null;try {in = new FileInputStream(file);out = new FileOutputStream("x.bmp");byte [] buff = new byte[1024];int i = -1;                        // 每次读取一定大小的块,循环读取                        while((i=in.read(buff))!=-1){out.write(buff,0,i);String str = new String(buff,0,i);System.out.println(str);};} catch (Exception e) {e.printStackTrace();}finally{try {if(in!=null){in.close();}if(out!=null){out.close();}} catch (Exception e2) {e2.printStackTrace();}}}}


原创粉丝点击