Java 流的基本讲解

java io

流的分类:

根据数据类型:字节流,字符流

根据数据流向:输出流,输入流

区分字节流和字符流:

stream结尾都是字节流
reader和writer结尾都是字符流

字节流和字符流的内在区别
1.读写单位不同
2.处理对象不同(字节流可以出来图片,avi 等而字符流只能处理字符类型的数据)
3.字节流在操作的时候本身是不会用到缓冲区的,而字符流在操作的时候下后是会用到缓冲区的,是通过缓冲区来操作文件


字节流

列子:
字节流
http://files.jb51.net/file_images/article/201311/20131114141246.jpg?20131014141326
 常用的流
FileInputStream
构造器:constructors:
FileInputStream(File file);根据File对象打开一个文件的输入连接
FileInputStream(String name);根据文件路径打开一个对应文件的输入连接

用于对文件内容进行二进制读取,如视频、图片、声音等文本内
ObjectInputStream
构造器:
ObjectInputStream(InputStream in);

通过readObject方法反序列化出InputStream对应的对象readObject() ;
读取基本数据类型,如readInt();

BufferedInputStream
构造器:
BufferedInputStream(InputStream in) ;
BufferedInputStream(InputStream in, int size)

使用默认缓存大小BufferedInputStream(InputStream in, int size) ;
使用特定缓存大小减少IO访问次数,一次读取多个字节的数据

例子:
  fileinputstream
      //得到文件
        File file = new File("d:/test.txt");
       //创建一个字节流把文件放进去
     FileInputStream  in  = new FileInputStream(file);
       //创建一个byte容器
     byte[]  bytew=new byte[1024];
        //输出流到那个文件
      FileOutputStream  out = new FileOutputStream("F:/tetx.txt");
        //把文件读入容器
     while (in.read(bytew)!=-1) {
          //在容器中写入文件到指定位置
      ut.write(bytew);
          //结束文件
      out.flush();
           } 
objectoutputstream
    //序列化
    Student stu  = new Student();
   File file1 = new File("F:/test.txt");
   FileOutputStream  in1  = new FileOutputStream(file1);
   ObjectOutputStream  obj = new ObjectOutputStream(in1);
   obj.writeObject(stu);
    obj.flush();
   //反序列化
    FileInputStream fis = new FileInputStream(file1);  
    ObjectInputStream ois = new ObjectInputStream(fis);  
    Student st1 = (Student) ois.readObject();  
    System.out.println("name = " + st1.show());  
   ois.close();  
   fis.close();

BufferedInputStream

                      Date d=null;
             long start=0L;
             long end=0L;
             SimpleDateFormat   sdf= null;
             sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
              start=System.currentTimeMillis();
              d=new Date();
              if(d!=null){
                     d.setTime(start);
              }
               System.out.println("开始时间"+sdf.format(d));
               File f=new File("D:/360安全浏览器下载/t.zip");
             FileInputStream input=new FileInputStream(f);
             FileOutputStream  outss = new FileOutputStream("F:/aaa.zip");
              // 指定文件带缓冲区的读取流且指定缓冲区大小为2KB
              BufferedInputStream bis=new BufferedInputStream(input,100*1024);
              BufferedOutputStream  outs = new BufferedOutputStream(outss,100*1024);
              byte[] byteArray=new byte[1024];
              while(bis.read(byteArray)!=-1){ 
                    outs.write(byteArray);
              }
              outs.flush();
              end=System.currentTimeMillis();
              d=new Date();
              if(d!=null){ 
                 d.setTime(end);
              }
              System.out.println("程序执行的结束时间:"+sdf.format(d));
              System.out.println(end-start);

        
字符流:

常用的:

  filterwriter,filterreader
  用来写入字符文件的便捷类。
  inputstreamreader,outputstreamwriter
  字符流的基本类
  bufferedWriter,bufferedreader
  字符流缓冲类

例子:



          //读入文件到字符流中    
         FileReader  fr  = new FileReader(file);
              //创建一个容器
         char[]ter = new char[103];
             //把文件写到那里去
         FileWriter  fw  = new FileWriter("f:/tet.txt");
         //把文件读入到文件中再判断   
         while (fr.read(ter)!=-1) {
             fw.write(ter);
           }
                  //结束    
          fw.flush();
       
    









    


        







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值