• Java-IO流学习


    前言

    学习了简单的java反序列化后,看到了Java-IO流的有关知识,了解一下。

    1.Java-IO流简介

    Java中I/O操作主要是指使用Java进行输入,输出操作. Java所有的I/O机制都是基于数据流进行输入输出,这些数据流表示了字符或者字节数据的流动序列。Java的I/O流提供了读写数据的标准方法。任何Java中表示数据源的对象都会提供以数据流的方式读写它的数据的方法。

    IO又分为流IO(java.io)和块IO(java.nio)

    Java.io是大多数面向数据流的输入/输出类的主要软件包。此外,Java也对块传输提供支持,在核心库 java.nio中采用的便是块IO。

    2.流的基本概念

    标准输入输出,文件的操作,网络上的数据流,字符串流,对象流,zip文件流等等,java中将输入输出抽象称为流,就好像水管,将两个容器连接起来。将数据从外存中读取到内存中的称为输入流,将数据从内存写入外存中的称为输出流。

    我的理解是:从eclipse输出到文本文件txt中叫输出流,而从文本文件txt输入到eclipse叫作输入流。

    操作 IO 流的模板:

    ①、创建源或目标对象

    输入:把文件中的数据流向到程序中,此时文件是 源,程序是目标

    输出:把程序中的数据流向到文件中,此时文件是目标,程序是源

    ②、创建 IO 流对象

    输入:创建输入流对象

    输出:创建输出流对象

    ③、具体的 IO 操作

    ④、关闭资源

    输入:输入流的 close() 方法

    输出:输出流的 close() 方法

    3.创建文件的三种方式

    3.1根据一个文件路径直接创建一个文件。

    代码如下:

    CreatFile.java

    package IO;
    
    import java.io.File;
    import java.io.IOException;
    
    public class CreatFile{
        public static void main(String[] args) throws IOException {
            createFile();
        }
        public static void createFile() throws IOException{
            File file = new File("C:\\Users\\Administrator\\Desktop\\feng.txt");//创建的文件路径
            file.createNewFile();
            System.out.println("Create Successfully");
            }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    运行代码,会返现桌面中会多出一个空的feng.txt。

    3.2 根据父目录 File 对象,在子路径创建一个文件

    利用到的方法: new File(File parent, String child)

    CreatFile2.java

    package IO;
    
    import java.io.File;
    import java.io.IOException;
    
    // 根据父目录File对象,在子路径创建一个文件
    public class CreatFile2 {
        public static void main(String[] args) throws IOException {
            createFile();
        }
    
        public static void createFile() throws IOException {
            File parentFile = new File("C:\\Users\\Administrator\\Desktop");
            File file = new File(parentFile, "feng2.txt");
            file.createNewFile();
            System.out.println("Create Successfully");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    3.3 根据父目录路径,在子路径下生成文件

    代码如下:

    package IO;
    
    import java.io.File;
    import java.io.IOException;
    
    // 根据父目录路径,在子路径下生成文件
    public class newfile3 {
        public static void main(String[] args) throws IOException {
            createFile();
        }
    
        public static void createFile() throws IOException {
            String parentPath = "C:\\Users\\Administrator\\Desktop";
            String fileName = "feng3.txt";
            File file = new File(parentPath, fileName);
            file.createNewFile();
            System.out.println("Create Successfully");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    4.获取文件信息

    首先在feng.txt中写入内容。

    然后利用File类来获取feng.txt文件的信息。

    package IO;
    
    import java.io.File;
    
    public class showFile {
        public static void main(String[] args) {
            getFileContents();
        }
    
        public static void getFileContents(){
            File file = new File("C:\\Users\\Administrator\\Desktop\\feng.txt");
            System.out.println("文件名称为:" + file.getName());
            System.out.println("文件的绝对路径为:" + file.getAbsolutePath());
            System.out.println("文件的父级目录为:" + file.getParent());
            System.out.println("文件的大小为:" + file.length());
            System.out.println("这是不是一个文件:" + file.isFile());
            System.out.println("这是不是一个目录:" + file.isDirectory());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    image-20221115143451861

    5.目录与文件操作

    5.1文件删除

    使用file.delete(文件)

    package IO;
    
    import java.io.File;
    import java.lang.reflect.Field;
    
    public class fileDelete {
        public static void main(String[] args) {
            deleteFile();
        }
        public static void deleteFile(){
            File file = new File("C:\\Users\\Administrator\\Desktop\\feng2.txt");
            System.out.println(file.delete() ? "Delete Successfully":"Delete failed");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    运行后便可以删除之前创建的feng2.txt

    5.2目录删除

    还是使用file.delete(目录)方法,注意只能删除空目录

    package IO;
    
    import java.io.File;
    
    public class directoryDelete {
        public static void main(String[] args) {
            deleteDirectory();
        }
        public static void deleteDirectory(){
            File file = new File("C:\\Users\\Administrator\\Desktop\\123");
            System.out.println(file.delete()? "Delete Successfully":"Delete failed");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    5.3创建单级目录

    package IO;
    
    import java.io.File;
    
    
    public class CreatDirectory {
        public static void main(String[] args) {
            createSingleDir();
        }
        public static void createSingleDir(){
            File file = new File("C:\\Users\\Administrator\\Desktop\\feng");
            System.out.println(file.mkdir() ? "Create Successfully":"Create failed");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    5.4 创建多级目录

    方法 file.mkdirs

    package IO;
    
    import java.io.File;
    
    
    public class MultiDirectory {
        public static void main(String[] args) {
            createMultiDir();
        }
    
        public static void createMultiDir(){
            File file = new File("C:\\Users\\Administrator\\Desktop\\feng2");
            System.out.println(file.mkdirs() ? "Create Successfully":"Create failed");
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    6.IO 知识点 —— IO 流分类

    按照操作数据单位不同分为:字节流字符流

    • 字节流(8bit,适用于二进制文件)
    • 字符流(按字符,因编码不同而异,适用于文本文件)

    操作 IO 流的模板:

    ①、创建源或目标对象

    输入:把文件中的数据流向到程序中,此时文件是 源,程序是目标

    输出:把程序中的数据流向到文件中,此时文件是目标,程序是源

    ②、创建 IO 流对象

    输入:创建输入流对象

    输出:创建输出流对象

    ③、具体的 IO 操作

    ④、关闭资源

    输入:输入流的 close() 方法

    输出:输出流的 close() 方法

    6.1利用FileInputStream.read读取文件

    代码:

    FileInputRead.java

    package IO;
    
    import java.io.FileInputStream;
    import java.io.IOException;
    
    public class FileInputRead {
        public static void main(String[] args) throws IOException{
            readFile();
        }
        public static void readFile() throws IOException{
            String filePath = "C:\\Users\\Administrator\\Desktop\\feng.txt";
            FileInputStream fileInputStream = null;
            int readData = 0;
            fileInputStream = new FileInputStream(filePath);
            while((readData = fileInputStream.read())!=-1){
                System.out.print((char)readData);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    进行调试,可以清楚的理解代码的作用,然后利用read一个一个读取字符流来输出读取到的文件的内容。

    跟进一下read:

    image-20221115191941515

    发现其作用便是一个一个读取字符

    public int read() throws IOException
    从此输入流中读取一个数据字节。如果没有输入可用,则此方法将阻塞。
    指定者:
    类 InputStream 中的 read
    返回:
    下一个数据字节;如果已到达文件末尾,则返回 -1。

    所以可以用while((readData = fileInputStream.read())!=-1)方法来输出读取到的内容。

    6.2利用Runtime中的内置Exec方法进行命令执行并输出

    RuntimeExec.java

    package IO;
    
    import java.io.ByteArrayOutputStream;
    import java.io.InputStream;
    
    // 使用 Runtime 类进行命令执行
    public class RuntimeExec {
        public static void main(String[] args) throws Exception {
            InputStream inputStream = Runtime.getRuntime().exec("whoami").getInputStream();
            byte[] cache = new byte[1024];
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            int readLen = 0;
            while ((readLen = inputStream.read(cache))!=-1){
                byteArrayOutputStream.write(cache, 0, readLen);
            }
            System.out.println(byteArrayOutputStream);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    利用的是inputStream.read来读取输出流,6.1是读取文件输出流。

    6.3向文件中写入内容

    利用write(byte[] b) 方法。

    package IO;
    
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    // write(byte[] b) 方法
    public class filewrite {
        public static void main(String[] args) throws IOException{
            writeFile();
        }
    
        public static void writeFile() throws IOException{
            String filePath = "C:\\Users\\Administrator\\Desktop\\feng.txt";
            FileOutputStream fileOutputStream = null;
            fileOutputStream = new FileOutputStream(filePath);
            String content = "You are successful!";
            fileOutputStream.write(content.getBytes());
            fileOutputStream.close();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    注意,该代码是直接将原文件赋值,原来的文件内容直接被覆盖。

    或者如果想要写入的数据不被覆盖,可以设置 FileOutputStream 的构造方法 append 参数设置为 true

    6.4文件拷贝

    需要拷贝的文件feng.png

    image-20221116100322345

    利用FileInputStream和fileOutputStream,然后就是fileInputStream.read一个一个读取,fileOutputStream.write再写入。

    package IO;
    
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    
    public class filecopy {
        public static void main(String[] args) throws IOException{
            copyFile();
        }
        public static void copyFile() throws IOException{
            String srcFilename = "C:\\Users\\Administrator\\Desktop\\feng.png";
            String desFilename = "C:\\Users\\Administrator\\Desktop\\feng2.png";
            FileInputStream fileInputStream = null;
            FileOutputStream fileOutputStream = null;
            fileInputStream = new FileInputStream(srcFilename);
            fileOutputStream = new FileOutputStream(desFilename);
            byte[] cache = new byte[1024];
            int readLen = 0;
            while((readLen = fileInputStream.read(cache)) != -1){
                fileOutputStream.write(cache, 0, readLen);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    运行代码即可发现图片中的内容被复制到了feng2.PNG当中

    image-20221116100401433

    小结

    IO流其实就是基于数据流进行输入输出,java在读取和写入文件是都可以将其转化成数据流来进行操作。

  • 相关阅读:
    深度学习入门篇(一):环境搭建(PyTorch+Anaconda3+CUDA+PyCharm )
    编译器一日一练(DIY系列之词法分析)
    【路径规划-VRP问题】基于混合K-Means和蚁群算法求解带容量车辆路径规划CVRP问题附matlab代码
    IOI 2022 简要题解
    C++模板类用作参数传递
    集合collection listmapset
    Day 266/300 ThreeJs 如何废置对象?
    【allegro 17.4软件操作保姆级教程九】布线后检查与调整
    区块链应用(去中心化应用)是什么样的?
    运放:运放+TL431+MOS 构成的恒流电路
  • 原文地址:https://blog.csdn.net/akxnxbshai/article/details/127879411