IO流的用法

IO流的分类:

- 从方向上来分:输入流、输出流

- 从传输单位上分:字节流、字符流

- 从功能上分:基础流、包装流

IO流的种类:

1.字节输入流(InputStream)、字节输出流(OutputStream)

2.字符输入流(Reader)、字符输出流(Writer)

以上是4个抽象的基类

1.如果管子搭在文件上:

        文件字节输入流(FileInputStream)、

        文件字节输出流(FileOutputStream)

        文件字符输入流(FileReader)

        文件字符输出流(FileWriter)

 2. 如果管子搭在字节数组上 ByteArray

        ByteArrayInputStream、

        ByteArrayOutputStream、

        CharArrayInputStream、

        CharArrayOutputStream、

3....

1. 字节输入流  读取文件

public class Demo9 {
    public static void main(String[] args) throws IOException {
        InputStream inputStream = new FileInputStream("D:\\SGUIGU\\ownnote\\Day5\\Day18\\src\\hello");

        //read()表示读取一个字节,返回int类型的值,例如,a返回97,h返回104
        //如果返回的是-1 ,表示没有读取到数据,已经到达文件末尾
        while(true){
            int data = inputStream.read();
            if(data == -1){
                break;
            }
            System.out.print((char)data);
        }
    }
}
public class Demo10 {
    public static void main(String[] args) throws IOException {
        InputStream inputStream = new FileInputStream("Day18/src/hello");
//        InputStream inputStream = new FileInputStream("D:/SGUIGU/ownnote/Day5/Day18/src/hello.txt");

        //准备一辆小车子
        byte[] bytes = new byte[10];

        //用来表示每次实际读取到的字节数
        int len = -1;

        while ((len= inputStream.read(bytes))!=-1){
            //len表示实际读取到的字节数
            //将len长度的字节数组装成字符串,然后打印
            String str = new String(bytes,0,len);
            System.out.println(str);
        }

        //关闭流
        inputStream.close();
    }
}

2. 文件字节输出流  写入文件

public class Demo11 {
    public static void main(String[] args) throws IOException {

        //OutputStream 字节输出流的抽象基类
        OutputStream os = new FileOutputStream("Day18/src/hello11.txt");

        Scanner input = new Scanner(System.in);
        System.out.print("请输入需要写入到文件中的内容:");
        String msg = input.nextLine();

        //将数据输出到文件中
        os.write(msg.getBytes());
        os.close();
    }
}

案例:复制视频文件:

思想:

程序先读入:(文件字节输入流FileInputStream

程序后输出:(文件字节输出流FileOutputStream

public class Demo12 {
    public static void main(String[] args) throws IOException {
        Scanner input = new Scanner(System.in);

        System.out.println("请输入视频源路径:");
        String srcFilePath = input.nextLine();

        File srcFile = new File(srcFilePath);

        if(!srcFile.exists() ||  !srcFile.isFile() || !srcFile.canRead()){
            throw new IOException("文件非法");
        }

        //1.创建  文件字节输入流
        FileInputStream fis = new FileInputStream(srcFilePath);

        System.out.print("请输入视频的输出目录:");
        String targetDirPath  = input.nextLine();
        File targetDir = new File(targetDirPath);

        //如果目录不存在,先创建目录
        if(!targetDir.exists()){
            targetDir.mkdirs();
        }

        //2. 创建文件字节输出流
        File targetFile = new File(targetDir,srcFile.getName());
        FileOutputStream fos = new FileOutputStream(targetFile);

        //3.准备运输的小车子,以及len
        byte[] bytes = new byte[1024];
        int len = -1;
        System.out.println("正在复制。。。");
        while ((len=fis.read(bytes))!=-1){
            fos.write(bytes,0,len);
        }
        fos.close();
        fis.close();
        System.out.println("复制成功");
    }
}

案例:复制文件(夹)

import java.io.*;
import java.util.Scanner;

public class Demo13 {
    public static void main(String[] args) throws IOException {
        Scanner input = new Scanner(System.in);

        System.out.println("请输入源目录路径:");
        String srcDirPath = input.nextLine();

        System.out.println("请输入输出目录:");
        String targetParentDirPath = input.nextLine();

        File srcDir = new File(srcDirPath);
        if(!srcDir.exists()){
            throw new IOException("目录非法,程序终止");
        }

        System.out.println("正在复制...");
        copy(srcDir,targetParentDirPath);
        System.out.println("复制成功!");

    }

    public static void copy(File srcFile,String targetDir) throws IOException {
        if(srcFile.isFile()){
            copyfile(srcFile,targetDir);
        }else {
            String srcDirName = srcFile.getName();
            String targetDirPath = targetDir+"/"+srcDirName;
            File targetDirFile = new File(targetDirPath);

            //1)完成文件夹的创建
            targetDirFile.mkdirs();
            //2) 再去复制文件夹下的内容
            File[] childFileArr = srcFile.listFiles();
            for (int i = 0; i <childFileArr.length ; i++) {
                File childFile = childFileArr[i];
                copy(childFile,targetDirFile.getAbsolutePath());
            }
        }
    }

    //你告诉我源文件以及目标目录,我给你完成文件的复制
    //完成文件的复制
    public static void copyfile(File srcFile,String targetDir) throws IOException {
        InputStream is = new FileInputStream(srcFile);
        byte[] bytes = new byte[1024];
        int len = -1;
        OutputStream os = new FileOutputStream(targetDir+"/"+srcFile.getName());

        while ((len=is.read(bytes))!=-1){
            os.write(bytes,0,len);
        }

        os.close();
        is.close();
    }

}
//输入:D:\\SGUIGU\\ownnote\\Day5\\Day18\\src\\a
//复制到:D:\\SGUIGU\\ownnote\\Day5\\Day18\\src\\copy

3. 文件 字符输入流 

reader  writer

public class Demo15 {
    public static void main(String[] args) throws IOException {
        /*
        InputStream is = new FileInputStream("pro01_io/src/hello.txt");
        byte[] bytes = new byte[10];
        int len = -1 ;

        while((len=is.read(bytes))!=-1){
            String str = new String(bytes,0,len);
            System.out.print(str);
        }

        is.close();
        */

        //字符流 : Reader , Writer
        Reader reader = new FileReader("Day18/src/hello");
        char[] chars = new char[2];
        int len =-1;
        while((len=reader.read(chars))!=-1){
            String str = new String(chars,0,len);
            System.out.print(str);
        }
        reader.close();
    }
}

4. 文件 字符输出流 

//16.演示字符输出流
public class Demo16 {
    public static void main(String[] args) throws IOException {
        Writer writer = new FileWriter("Day18/src/hello16.txt");
        writer.write("你好,我是大数据开发人员\n");
        writer.write("我的特长是大数据开发");
        writer.close();
    }
}

5.append

在FileOutputStream / FileWriter中有一个构造方法,参数为: (String path,boolean append) append为true,表示是往文件中追加字节或字符; 反之,表示覆盖文件中的内容

OutputStream os = new FileOutputStream("Day18/src/hello17.txt",true);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值