Java高级--IO流

File类的使用

java.io.File类:文件和文件目录路径的抽象表示形式

File类的一个对象,代表一个文件或一个文件目录(俗称:文件夹)

File类中涉及到关于文件或文件目录的创建、删除、重命名、修改时间、文件大小等方法,并未涉及到写入或读取文件内容的操作。如果需要读取或写入文件内容,必须使用IO流来完成

后续File类的对象常会作为参数传递到流的构造器中,指明读取或写入的”终点“

File的创建:

  • 1、创建File类的对象
    • 相对路径:相较于某个路径下,指明的路径
    • 绝对路径:包含盘符在内的文件或文件目录的路径

路径分隔符

  • windows和DOS系统默认使用 \ 来表示
  • UNIX,MacOS和URL使用 / 来表示
//构造器1 File(String filepath)
File file1 = new File("hello.txt");//相对路径
File file2 = new File("D:\\Java基础学习");//绝对路径

//构造器2 File(parent parentPath,child:childPath)
File file3 = new File("D:\\Java基础学习","File测试");
System.out.println(file3);

//构造器3 File(File parentFile,String childPath)
File file4 = new File(file3,"h1.txt");
System.out.println(file4);

File类常用方法

File类的获取功能

  • public String getAbsolutePath():获取绝对路径
  • public String getPath() :获取路径
  • public String getName() :获取名称
  • public String getParent():获取上层文件目录路径。若无,返回null
  • public long length() :获取文件长度(即:字节数)。不能获取目录的长度。
  • public long lastModified() :获取最后一次的修改时间,毫秒值

如下两个方法适用于文件目录

  • public String[] list() :获取指定目录下的所有文件或者文件目录的名称数组
  • public File[] listFiles() :获取指定目录下的所有文件或者文件目录的File数组

File类的重命名功能

  • public boolean renameTo(File dest):把文件重命名为指定的文件路径
    要想保证返回true,需要file1在硬盘中是存在的,且file2不能再硬盘中存在
 @Test
    public void test2(){
        File file1 =new File("Hello.txt");
        File file2 = new File("D:\\Java基础学习\\hi.txt");

        boolean renameTo = file2.renameTo(file1);
        System.out.println(renameTo);
    }

File类的判断功能

  • public boolean isDirectory():判断是否是文件目录
  • public boolean isFile() :判断是否是文件
  • public boolean exists() :判断是否存在
  • public boolean canRead() :判断是否可读
  • public boolean canWrite() :判断是否可写
  • public boolean isHidden() :判断是否隐藏

File类的创建功能

  • public boolean createNewFile() :创建文件。若文件存在,则不创建,返回false
  • public boolean mkdir() :创建文件目录。如果此文件目录存在,就不创建了。如果此文件目录的上层目录不存在,也不创建。
  • public boolean mkdirs() :创建文件目录。如果上层文件目录不存在,一并创建
    注意事项:如果你创建文件或者文件目录没有写盘符路径,那么,默认在项目路径下。

File类的删除功能

  • public boolean delete():删除文件或者文件夹
    :Java中的删除不走回收站。删除一个文件目录,请注意该文件目录内不能包含文件或者文件目录

IO流原理及流的分类

I/O是Input/Output的缩写,用于处理设备之间的数据传输;对于数据的输入/输出操作以“流”的方式进行;获取不同种类的数据,通过标准的方法输入或输出数据

流的分类

  • 按操作数据单位的不同分为:字节流(8bit)字符流(16bit)
  • 按数据流的流向不同分为:输入流输出流
  • 按流的角色的不同分为:节点流处理流
    在这里插入图片描述
    在这里插入图片描述
    重点关注其中蓝色的
    在这里插入图片描述
    在这里插入图片描述

节点流(或文件流)

  • 对于文本文件(.txt、.java、.c、.cpp),使用字符流处理
  • 对于非文本文件(.jpg、.mp3、.mp4、.avi、.doc、.ppt…),使用字节流处理

FileReader操作

步骤:

  • 1.实例化File类的对象,指明要操作的文件
  • 2.FileReader流的实例化
  • 3.数据的读入
  • 4.流的关闭操作

说明:

  • 1.read()的理解:返回读入的一个字符。如果达到文件末尾,返回-1
  • 2.异常的处理:为了保证流资源一定可以执行关闭操作。需要使用try-catch-finally处理
  • 3.读入的文件一定要存在,否则就会报FileNotFoundException

一次读一个字符

/*
    说明:
    1.read()的理解:返回读入的一个字符。如果达到文件末尾,返回-1
    2.异常的处理:为了保证流资源一定可以执行关闭操作。需要使用try-catch-finally处理
    3.读入的文件一定要存在,否则就会报FileNotFoundException
     */
    @Test
    public void testFileReader(){
        FileReader fr=null;
        try {
            //1.实例化File类的对象,指明要操作的文件
            File file = new File("hello.txt");//相较于当前Module的路径
            System.out.println(file);

            //2.提供具体的流
            fr = new FileReader(file);

            //3.数据的读入
            //read():返回读入的一个字符。如果达到文件末尾,返回-1
//        int data = fr.read();
//        while(data  != -1 ){
//            System.out.print((char)data);
//            data = fr.read();//读文件中的下一个字符
//        }
            int data;
            while((data = fr.read()) != -1){
                System.out.print((char)data);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.流的关闭操作
            try {
                if(fr != null){
                    fr.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

一次读多个字符,存入数组中

@Test
    public void testFileReader1() {

        FileReader fr=null;
        try {
            //1.实例化File类的对象,指明要操作的文件
            File file = new File("hello.txt");

            //2.流的实例化
            fr = new FileReader(file);

            //3.读入的操作
            char[] cbuf = new char[5];
            int len;
            while((len=fr.read(cbuf)) != -1){
                for(int j=0 ;j<len ;j++){
                    System.out.print(cbuf[j]);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.关闭资源
            try {
                if(fr != null){
                    fr.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

FileWriter操作

步骤:

  • 1.实例化File类的对象,指明要操作的文件
  • 2.FileWriter流的实例化
  • 3.数据的写入
  • 4.流的关闭操作

说明:

  • 1、输出操作,对应的File可以不存在,并不会报异常
  • 2、File对应的硬盘中的文件如果不存在,在输出的过程中,会自动创建文件
    File对应的硬盘中的文件如果存在:
    • 如果流使用的构造器是:FileWriter(file,false)/FileWriter(file):对原有的文件进行覆盖
    • 如果流使用的构造器是:FileWriter(file,true):不会对原有文件覆盖,而是在原文件内容后面追加写入的内容
@Test
    public void testFileWriter(){
        FileWriter fw =  null;
        FileWriter fw1 = null;
        try {
            //1.提供File类的对象
            File file = new File("hello1.txt");
            File file1 = new File("hello.txt");

            //2.流的实例化
            fw = new FileWriter(file);//覆盖
            fw1 = new FileWriter(file1,true);//追加

            //3.写入操作
            fw.write("hsiefjnkd\n");
            fw.write("balbalbalba");
            fw1.write("\n8899");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.关闭流
            try {
                if(fw != null){
                    fw.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                if(fw1 != null){
                    fw1.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

读一个文件,并将里面的内容写入到一个新文件(文件的复制)

@Test
    public void testFileReaderWriter(){
        FileReader fr = null;
        FileWriter fw = null;
        try {
            //1.实例化File类
            File file1 = new File("hello.txt");
            File file2 = new File("hello2.txt");

            //2.实例化流
            fr = new FileReader(file1);
            fw = new FileWriter(file2);

            //3.读写操作
            char[] cbuf = new char[5];
            int len;
            while((len = fr.read(cbuf)) != -1){
                fw.write(cbuf,0,len);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            //4.关闭流
            try {
                if(fw != null){
                    fw.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

            try {
                if(fr != null){
                    fr.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

FileInputStream&FileOutputStream流

读取图片,写入到另一个地方(复制图片)

@Test
    public void testFileInputOutputTest(){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            //1.实例化File类
            File srcFile = new File("壮志凌云2.png");
            File destFile = new File("壮志凌云22.png");

            //2.实例化流
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(destFile);

            //3.读写操作(复制)
            byte[] buffer = new byte[5];
            int len;
            while ((len = fis.read(buffer)) != -1){
                fos.write(buffer,0,len);
            }
            System.out.println("复制成功");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.关闭流
            try {
                if(fos != null)
                    fos.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                if(fos != null)
                    fis.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

处理流一:缓冲流

BufferedInputStream、BufferedOutputStream、BufferedReader、BufferedWriter
缓冲流是处理流中的一种
作用:提升流的读取、写入的速度
原因:内部提供了一个缓冲区

说明:
缓冲流是处理流的一种,是包在节点流外部,下面解释称为外部和内部
步骤:

  • 1、实例化File类
  • 2、实例化节点流(内部)
  • 3、实例化缓冲流(外部)
  • 4、关闭流(先关闭外部的流在关闭内部的。在关闭外部的流后内部的流会自动关闭,内部的流就可以省略手动关闭)

BufferedInputStream&BufferedOutputStream

使用缓冲流复制电影

public void copyFileWithBuffered(String srcPath,String destPath){
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            //1.实例化File类
            File srcFile  = new File(srcPath);
            File destFile = new File(destPath);

            //2.1实例化流(内层流)
            FileInputStream fis = new FileInputStream(srcFile);
            FileOutputStream fos = new FileOutputStream(destFile);

            //2.2实例化缓冲流(外层流)
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);

            //3.复制的细节:读取、写入
            byte[] buffer = new byte[2048];
            int len;
            while((len = bis.read(buffer)) != -1){
                bos.write(buffer,0,len);
            }

            System.out.println("复制成功");
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            //4.关闭缓冲流(关闭外层流的时候,内层流也会自动的进行关闭)
            try {
                if(bos != null)
                    bos.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                if(bos != null)
                    bis.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Test
    public void testCopyFile(){
        long start = System.currentTimeMillis();

        String srcPath="D:\\jianpianDownload\\星际穿越BD中英双字.mp4";
        String destPath="C:\\Users\\兰淋川\\Desktop\\星际穿越BD中英双字.mp4";

        copyFileWithBuffered(srcPath,destPath);

        long end = System.currentTimeMillis();

        //数组长度为10是,花费7952ms
        //数组长度为1024是,花费4328ms
        //数组长度为2048是,花费4295ms
        System.out.println(end - start);
    }

BufferedReader&BufferedWriter

处理流二:转换流(属于字符流)

InputStreamReader:将一个字节的输入流转换为字符的输入流
OutputStreamWriter:将一个字符的输出流转换为字节的输出流
作用:提供字节流与字符流之间的转换
解码:字节、字节数组—>字符数组、字符串
编码:字符数组、字符串—>字节、字节数组

InputStreamReader&OutputStreamWriter的混合使用

@Test
    public void test2(){
    //1.实例化File
        InputStreamReader isr = null;
        OutputStreamWriter osr = null;
        try {
            File file1 = new File("dbcp.txt");
            File file2 = new File("dbcp_gbk.txt");
	//实例化节点流
            FileInputStream fis = new FileInputStream(file1);
            FileOutputStream fos = new FileOutputStream(file2);

	//3.实例化转换流
//          InputStreamReader isr = new InputStreamReader(fis);//使用系统默认的字符集
            //参数2指明了字符集,具体使用哪个字符集,取决于文件dbcp.txt保存时使用的字符集
            isr = new InputStreamReader(fis,"utf-8");
            osr = new OutputStreamWriter(fos,"gbk");//输出时指定为gbk

		//4.转换细节
            char[] cbuf = new char[20];
            int len;
            while((len = isr.read(cbuf)) != -1){
                osr.write(cbuf,0,len);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
        //5.关闭流
            try {
                if(isr != null)
                    isr.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

            try {
                if(osr != null)
                    osr.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

处理流三:标准输入、输出流(了解)

System.in:标准的输入流,默认从键盘输入
System.out:标准的输出流,默认从控制台输出
System类的setIn(InputStream is)/setOut(PrintStream ps)方式重新指定输入和输出的位置(从控制台到文件…)

打印流(了解)

实现将基本数据类型的数据格式转化为字符串输出
PrintStream、PrintWriter

数据流(了解)

用于读取或写出基本数据类型和String类型的数据
DataInputStream、DataOutputStream

在这里插入图片描述

注:

  • 每写入一条数据,就要调用flush()方法,刷新
  • 读取数据的顺序,要与写入数据的顺序一致

对象流

作用:用于存储和读取基本数据类型数据或对象的处理流
ObjectInputStream、ObjectOutputStream

  • 序列化:用ObjectOutputStream类保存基本类型数据或对象到数据源中
  • 反序列化:用ObjectInputStream类读取基本类型数据或对象到数据源中

对象序列化机制:允许把内存中的Java对象转换成平台无关的二进制流,从而允许把这种二进制流持久的保存到磁盘上,或通过网络将这种二进制流传输到一个网络节点。(序列化过程)。当其它程序获取了这种二进制流,就可以恢复成原来的Java对象。(反序列化)

满足可序列化的类的对象的要求

  • 1、该类需要实现接口:Serializable
  • 2、当前类提供一个全局常量:serialVersionUID。如果没有这个常量标识,序列化后进行反序列化可能会失败
  • 3、除了当前Person类需要实现Serializable接口之外,还必须保证其内部所有属性也必须时可序列化的。(默认情况下,基本数据类型可序列化)
  • 补充:ObjectOutputStream和ObjectInputStream不能序列化static和transient修饰的成员变量

ObjectInputStream&ObjectOutputStream示例:

Person类:

public class Person implements Serializable {
	public static final long serialVserionUID = 345335432499L;

    private String name;
    private int age;

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

序列化与反序列化过程:

public class ObjectInputOutputStreamTest {

    /*
    序列化:将内存中的java对象转换成二进制流并存入磁盘中
    ObjcetOutputStream
     */
    @Test
    public void ObjcetOutputStream(){

        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("object.txt"));

            oos.writeObject(new String("离开的好媳妇女巫鸟巢上空"));
            oos.flush();

            oos.writeObject(new Person("王芳芳",24));
            oos.flush();

        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if(oos != null)
                    oos.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    //反序列化:将磁盘文件中的对象还原为内存中的一个java对象
    //ObjectInputStream
    @Test
    public void ObjcetInputStream(){
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream("object.txt"));

            Object obj = ois.readObject();
            String str = (String)obj;

            Person p = (Person) ois.readObject();

            System.out.println(str);
            System.out.println(p);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if(ois != null)
                    ois.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

随机存取文件流

RandomAccessFile

  • 1、RandomAccessFile直接继承于java.lang.Object类,实现了DataInput和DataOutput接口
  • 2、RandomAccessFile既可以作为一个输入流,又可以作为一个输出流
  • 3、如果RandomAccessFile作为输出流时,写出到的文件如果不存在,则在执行过程中自动创建文件
    如果写出到的文件存在,则会对原有文件内容进行覆盖。(默认情况下,从最开头覆盖)
  • 4、可以通过相关的操作,实现RandomAccessFile"插入"数据的效果

NIO.2中Path、Paths、Files类的使用

Java NIO(New IO,Non-Blocking IO)
NIO支持面向缓冲区的(IO是面向流的)、基于通道的IO操作。NIO将以更高效的方式进行文件的读写操作
Java API提供了两套NIO,一套是针对标准输入输出NIO;另一个套是网络编程NIO

java.nio.channels.Channel

  • FileChannel:处理本地文件
  • SocketChannel:TCP网络编程的客户端的Channel
  • ServerSocketChannel:TCP网络编程的服务器端的Channel
  • DatagramChannel:UDP网络编程中发送端和接收端的Channel
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值