IO框架

java.io包里面主要有三个类:
1.File 文件和目录类

2.InputStream
OutputStream 都是抽象类不能实例化
字节流读写类
这里写图片描述

一.File类
File类的对象,表示了磁盘上的文件或者目录
Flie类不能对文件内容读写操作,直接处理文件或文件系统

构造方法
File(目录名)
这里写图片描述
这里写图片描述
文件输入输出流
FileInputStream 为InputStream的具体子类。 从文件去读数据到内存
三个read()方法
FileOutputStream 将内存的数据写入到文件
三个write()方法

例子:将e:/zhangsan/song.mp3复制到e://b.mp3

package com.jlu.cn.inputoutput;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class InputOutputDemo {
     /**
      * @param args
      * @throws IOException
      */
     public static void main(String[] args) {
          // TODO Auto-generated method stub

          try {
              FileUploadDemo.inputOutput(new File("e:\\zhangsan\\song.mp3"),new File("e:\\b.mp3"));
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }


     }

}
class FileUploadDemo
{    
     public static void inputOutput(File src,File des) throws IOException
     {
          FileInputStream fin=new FileInputStream(src);
          FileOutputStream fou=new FileOutputStream(des);

          long time1=System.currentTimeMillis();

          byte[] by=new byte[1024*1024];
          int len;
          while((len=fin.read(by))!=-1  )
          {
              fou.write(by, 0, len);
          }
          fin.close();
          fou.close();


          long time2=System.currentTimeMillis();

          System.out.println("完成这个文件的复制所需要的时间是"+(time2-time1));

     }




}

字节输入输出流,操作的是字节数组
ByteArrayInputStream两个构造方法,包含一个内部缓冲区,相当于从 他内部的冲缓区读到缓冲区
ByteArrayInputStream(byte array[])
ByteArrayInputStream(byte array[],int start,int numBytes)

ByteArrayOutputStream有两个构造方法,相当于从缓冲区写到他内部的缓冲区
ByteArrayOutputStream()//创建一个新的byte数组输出流
ByteArrayOutputStream(int numBytes) //创建一个新的byte数组输出流,具有指定大小缓冲区(字结尾单位)
这里写图片描述
ByteArrayInputStream中的close方法无效,因为调用不了底层资源

package com.jlu.cn.inputoutput;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class ByteArrayInputOutputStreamDemo {
     /**
      * @param args
      * @throws IOException
      */
     public static void main(String[] args) throws IOException {
          // TODO Auto-generated method stub
          String str="hello ShangHai";
          //数据源就是字符数组,将字符串转换为对应的字符数组,用getBytes(),从字节数组里面读取字节
          ByteArrayInputStream bis=new ByteArrayInputStream(str.getBytes());
          int data=-1;
          while((data=bis.read())!=-1)
          {
              System.out.print((char)data);
          }


          ByteArrayOutputStream bio=new ByteArrayOutputStream();
          bio.write(67);
          bio.write(99);
          bio.write("hello world".getBytes());
          byte[] by=bio.toByteArray();
          for(byte a:by)
          {
              System.out.print((char)a);
          }

          //将ByteArrayOutputStream内部缓存区的数据写入到文件输出流当中,参数true代表内容不会被覆盖,追加
          FileOutputStream fil=new FileOutputStream("e:\\ab.txt",true);
          bio.writeTo(fil);
          fil.close();
     }
}

使用FileOutputStream流往一个文件里面写入数据

package com.jlu.cn.inputoutput;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class OutputStream02Test {
    public static void main(String[] args) throws IOException {
        File file = new File("d:/zhangsan/cy44.txt");//1.建立联系
        //2.判断父路径是否存在
        if(!file.getParentFile().exists()){
            file.getParentFile().mkdirs();
        }
        //3.对象上转型,父类声明子类实例化
        OutputStream os = null;//因为os是局部变量需要显性赋值
        try {
            os = new FileOutputStream(file,true);//追加数据
            //4.执行写入操作
            String hello = "\r\n张三";
            //如何换行,请注意 \r\n 是换行,getBytes()方法将字符串转换为字节数组
            os.write(hello.getBytes());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }finally {
            //5.关闭资源
            if(os!=null){
                os.close();
            }
        }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值