Java IO流

Io流

1、什么是IO流,IO流分类

  • 方向:

    1. 输入流
    2. 输出流
  • 操作单位:

    1. 字节流
    2. 字符流

2、字节流

2.1、应用场景

  • 只要涉及流操作,它都可以用

2.2、字节输入流

2.2.1、InputStream
  • 字节输入流超类
  • 成员方法:close 、read
2.2.2、FileInputStream
  • 继承InputStream,读硬盘内容到内存

  • 构造方法:FileInputStream(File file); FileInputStream(String name);

  • 主要方法:

    1. int read(); //从此输入流中读取一个数据字节
    2. int read(byte[] b); //从此输入流中将最多b.length个字节的数据读入一个byte数据中
    3. int read(byte[] b,int off,int length); //从此输入流中将最多length个字节的数据读入一个byte数组中
    4. void close(); //关闭此输入流并释放于此流有关的所有系统资源
  • 使用步骤

    1. 创建对象,确定数据源
    2. 调用read方法
    3. close方法释放资源

2.3、字节输出流

2.3.1、OutputStream
  • 字节输出流超类
  • 成员方法:close、write、fluse
2.3.2、FileOutputStream
  • 继承OutputStream,数据写入文件

  • 构造方法

    1. FileOutputStream(File file); //默认写在开始处
    2. FileOutputStream(File file,boolean append); //append true 表示追加
  • 主要方法:

    1. void write(int b); //将指定字节写入此文件输出流
    2. void wirte(byte[] b); //将b.length个字节从指定byte 数组写入
    3. void write(byte[] b,int off,int length);//off开始的length 个字节
    4. void close();//关闭
  • 使用步骤:

    1. 创建对象,确定写入文件
    2. 调用write方法
    3. 调用flush方法刷新
    4. close
//用字节流完成文件复制
public class Test2 {
    public void copyFile(File file1,File file2){
        if(!file2.exists()){
            file2.mkdirs();
        }
        File[] files = file1.listFiles();
        if(files!=null){
            for (File file:files) {
                if(file!=null){
                    File file3=new File(file2,file.getName());
                    if(file.isFile()&&file.getName().endsWith(".mp4")){
                        Copy(file,file3);
                    }else {
                        copyFile(file,file3);
                    }
                }
            }
        }
    }
    public void Copy(File file1,File file2){
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream=new FileInputStream(file1);
            fileOutputStream=new FileOutputStream(file2);
            int length;
            byte[] b=new byte[1024];
            while ((length=fileInputStream.read(b))!=-1){
                fileOutputStream.write(b);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            CloseStream.Close(fileInputStream,fileOutputStream);
        }
    }
}
//关流工具类
public class CloseStream {
    public static void Close(InputStream inputStream, OutputStream outputStream){
        try {
            if(outputStream!=null){
                    outputStream.close();
            }
            if(inputStream!=null){
                    inputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void Close(Reader reader, Writer writer){
        try {
            if(writer!=null){
                    writer.close();
            }
            if(reader!=null){
                    reader.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

//测试
public class Test {
    //将vedio中以MP4结尾的文件复制到vedio1
    public static void main(String[] args) {
        File file = new File("D:\\java");
        File file1 = new File(file, "\\video");
        File file2 = new File(file,"\\video1");
        new Test2().copyFile(file1,file2);
    }
}

3、字符流

  • 应用场景:读取和写入中文

3.1、字符输入流

  • 继承Reader //Reader成员方法 close、read
  • FileReader
    1. 构造方法:FileReader(File file); FileReader(String name);
    2. 成员方法:继承父类
    3. 使用步骤:
      1. 创建对象,确定数据源
      2. 调用read方法:int read();int read(char[] chars);int read(char[] b,int off,int length);
      3. close

3.2、字符输出流

  • 继承Wirter //成员方法 close、write、flush

  • FileWirter:

    构造方法:

    • FileWriter(File file);
    • FileWriter(File file,boolean append);
    • FileWirter(String name);
    • FileWriter(String name,boolean append);

​ 成员方法:继承父类

​ 使用步骤:

  1. 创建对象,写入文件

  2. 调用wirte方法

    • int wirte(int i)
    • int wirte(char[] b)
    • int wirte(char[] b,int off,int length)
    • int wirte(String str)
    • int Wirte(String str,int off,int length)
    • 追加写和换行
  3. close

4、缓冲流

  1. BufferedReader

  2. 独有方法 :readLine()读取一行数据 判断的依据 返回的字符串是否为null

  3. BufferedWriter

  4. 独有方法:newLine()换行符

  5. BufferedInputStream

  6. BufferedOutputStream

    public class Test1 {
        public void copyFile(File file1,File file2){
            BufferedInputStream bufferedInputStream=null;
            BufferedOutputStream bufferedOutputStream=null;
            try {
                File[] files = file1.listFiles();
                if(files!=null){
                    for (File file:files) {
                        if(file!=null&&file.isFile()&&file.getName().endsWith("mp4")){
                            File file3 = new File(file2, file.getName());
                            bufferedInputStream= new BufferedInputStream(new FileInputStream(file));
                            bufferedOutputStream= new BufferedOutputStream(new FileOutputStream(file3));
                            int length;
                            byte[] bytes = new byte[1024*1024];
                            while ((length=bufferedInputStream.read(bytes))!=-1){
                                bufferedOutputStream.write(bytes,0,length);
                            }
                        }else {
                            copyFile(file,file2);
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                CloseStream.Close(bufferedInputStream,bufferedOutputStream);
            }
        }
    }
    

5、转换流

OutputStreamWriter
InputStreamReader

6、jdk1.7以后关闭流新方式

语法:
try(
	创建流
){
	可能有一次的业务代码
}catch(){

}

目前用到的所有流 都是实现了AutoCloseable
实现了这个接口 就支持自动关流

redOutputStream);
}
}
}




## 5、转换流

OutputStreamWriter 
InputStreamReader

## 6、jdk1.7以后关闭流新方式

```java
语法:
try(
 创建流
){
 可能有一次的业务代码
}catch(){

}

目前用到的所有流 都是实现了AutoCloseable
实现了这个接口 就支持自动关流

注意:有局限,一般还是用之前的close方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值