java高级特性-File/IO

0 前言

       File和IO是java提供的用于处理文件和输入输出的类,它们位于java.io包中,包含了各种流类和文件类,以及一些辅助类。在本文中,我们将介绍以下几种File和IO的类:File、FileInputStream、FileOutputStream、BufferedReader、BufferedWriter等。我们将学习它们的定义、特点、用法和注意事项。File类是文件和目录路径名的抽象表示,可以用于创建、查找和删除文件和目录。FileInputStream和FileOutputStream是用于从文件读取和向文件写入数据的字节流类,它们继承了InputStream和OutputStream抽象类。BufferedReader和BufferedWriter是用于从字符输入流读取和向字符输出流写入文本数据的缓冲流类,它们继承了Reader和Writer抽象类。

1 File的简介

1.1 生活中文件

定义:一堆数据的集合
作用:持久化(瞬时状态的对立)
文件存储的位置:磁盘、硬盘、光盘、U盘等等。

1.2 计算机中的文件

定义:java.io中的File类
创建File:newFile(); //创建文件实例
File的属性:文件位置,文件名称和文件内容。

1.3 File文件的增删查

//新增文件
    public void addFile(File file) throws Exception {
        //调用file对象的createNewFile()方法,获得返回值
        //如果某个磁盘路径没有对应的文件,则可以创建文件,返回值为true
        //如果已经存在,则不能创建文件,返回值为false

        //判断某路径下,文件名是否已存在
        if(file.exists()){
            System.out.println("文件已存在");
        }else {
            boolean flag = file.createNewFile();
            if(flag){
                System.out.println("创建文件成功");
            }else {
                System.out.println("创建文件失败");
            }
        }
    }
    //查询文件
    public void findFile(File file){
        System.out.println(file.getName());//获取文件的名称
    }
    //删除文件
    public void deleteFile(File file){
        boolean flag = file.delete();//删除文件,并判断文件是否删除成功
        if(flag){
            System.out.println("删除成功");
        }else {
            System.out.println("删除失败");
        }
    }
    public static void main(String[] args) throws Exception {
        //实例化File文件对象对象中参数存放有文件的路径和文件名称
        //文件的路径D:\\ 或D:/
        File file = new File("D:/zlt.txt");
        Demo001 demo001 = new Demo001();
        demo001.addFile(file);
        //demo001.findFile(file);
        //demo001.deleteFile(file);
    }

1.4 生活/计算机中的流

生活中:
人流、车流:人或是车的集合
计算机中:
定义:数据的集合【机制:先进先出】
流传递的方向:源数据源 传递到 目标数据源

1.5 流的指向

读入【文件 读入到 程序中】
写出【程序 写出到 文件中】

1.6 流的分类

按流向分:
输出流:OutputStream和Writer
输入流:InputStream和Reader
按按照处理单元分:
字节流:OutputStream和InputStream
字符流:Writer和Reader

2 字节流

2.1 FileInputStream字节输入流实现读取

//1.实例化FileInputStream对象
FileInputStream fis = new FileInputStream("D:/zlt.txt");
//2.定义临时变量date
int data = 0;
//3.循环遍历文件中的read(),判断如果值是-1,则跳出循环
while((date = fis.read()) != -1){
	System.out.print((char)date);
}

2.2 FileInputStream字节输入流实现数组读取

//1.实例化FileInputStream对象
FileInputStream fis = new FileInputStream("D:/zlt.txt");
//2.定义临时变量date
int data = 0;
//3.定义数组
byte[] bytes = new byte[1024];
//4.循环读取到数组中
while ((data = fis.read(bytes)) != -1){
    for(int i = 0;i < data;i++){
        System.out.print((char) bytes[i]);
    }
}

2.3 FileInputStream字节输入流FileOutputStream字节输出流实现复制

    public static void main(String[] args) throws IOException {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream("D:/zlt.txt");
            fos = new FileOutputStream("D:/zlt_copy.txt");
            //1.定义临时变量
            int data = 0;
            //2.定义字节数据
            byte[] bytes = new byte[1024];
            //3.循环读取输入流fis对象并写出到输出流fos对象中,实现复制的效果
            while ((data = fis.read(bytes)) != -1) {
                fos.write(bytes, 0, data);
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try{
                if (fos != null){
                    fos.close();
                }
            } catch (IOException e){
                e.printStackTrace();
            }
            try{
                if (fis != null){
                    fis.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }

3 字符流

3.1 字节流乱码问题

处理中文不要用字节流
在这里插入图片描述

3.2 FileReader字符流实现读取

	public static void main(String[] args) {
        FileReader fileReader = null;
        try{
            //1.实例化FileReader对象
            fileReader = new FileReader("D:/zlt.txt");
            //2.定义临时变量
            int data = 0;
            //3.定义数组
            char[] chars = new char[1024];
            //4.循环读取数组中的数据
            while ((data = fileReader.read(chars)) != -1){
                //5.输出
                for(int i = 0;i < data; i++){
                    System.out.println(chars[i]);
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            //6.判断流是否存在
            if (fileReader != null){
                try{
                    //7.关闭字符流
                    fileReader.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
    }

3.3 FileWriter字符流实现写出

	public static void main(String[] args) {
        FileWriter fw = null;
        //1.拓展1 new FileWriter("",true);中的true代表每次拼接字符串到文件中
        try{
            fw = new FileWriter("D:/zlt.txt",true);
            fw.write("母亲");
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try{
                //2.拓展2 字符输出流写出的时候必须close(),否则方法失效
//                fw.close();
                //3. 拓展3 如果不写close(),可以添加flush()强制刷新,也可以实现写出效果
                assert fw != null;
                fw.flush();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }

4 缓冲流

4.1 字符流FileReader加BufferReader实现读取

核心要点:BufferReader类似于FileReader外层套一层“管道”(增加效率)

	public static void main(String[] args) {
        //1.实例化FileReader对象
        Reader fr = null;
        //2.实例化BufferReader对象,参数为FileReader对象
        BufferedReader br = null;
        try{
            fr = new FileReader("D:/zlt.txt");
            br = new BufferedReader(fr);
            //3.定义String类临时变量,通过readLine方法一行一行的读取
            String data = null;
            while ((data = br.readLine()) != null){
                //4.输出
                System.out.println(data);
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            //5.关闭流对象
            try {
                br.close();
            }catch (IOException e){
                e.printStackTrace();
            }
            try {
                fr.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }

4.2 字符流FileWriter加BufferedWriter实现写出

	public static void main(String[] args) {
        Writer fw = null;
        BufferedWriter bw = null;
        try{
            fw = new FileWriter("D:/zlt.txt");
            bw = new BufferedWriter(fw);
            bw.write("sadas撒打算");
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try{
                fw.close();
                bw.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }

5 复制

5.1 字符流实现复制

	public static void main(String[] args) {
        FileReader fileReader = null;
        BufferedReader bufferedReader = null;
        FileWriter fileWriter = null;
        BufferedWriter bufferedWriter = null;
        try{
            fileReader = new FileReader("D:/zlt.txt");
            bufferedReader = new BufferedReader(fileReader);
            fileWriter = new FileWriter("D:/ZLT.txt");
            bufferedWriter = new BufferedWriter(fileWriter);
            String line = null;
            StringBuffer stringBuffer = new StringBuffer();
            while ((line = bufferedReader.readLine()) != null){
                stringBuffer.append(line+"\n");
            }
            bufferedWriter.write(stringBuffer.toString());
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try{
                bufferedWriter.close();
                fileWriter.close();
                bufferedReader.close();
                fileReader.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }

5.2 复制二进制文件(图片,视频)

	public static void main(String[] args) {
        FileInputStream fileInputStream = null;
        DataInputStream dataInputStream = null;
        FileOutputStream fileOutputStream = null;
        DataOutputStream dataOutputStream = null;
        try{
            fileInputStream = new FileInputStream("D:/11.png");
            dataInputStream = new DataInputStream(fileInputStream);
            fileOutputStream = new FileOutputStream("D:/22.png");
            dataOutputStream = new DataOutputStream(fileOutputStream);
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = dataInputStream.read(bytes)) != -1){
                dataOutputStream.write(bytes,0,len);
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try{
                dataOutputStream.close();
                fileOutputStream.close();
                dataInputStream.close();
                fileInputStream.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值