Operating system (section 7) --- file and IO operations

1 Common basic operations of File files

For the operation of the file, it is mainly demonstrated in the form of code, which is relatively simple and can be memorized;

public static void main(String[] args) {
    
    
        // 定义要操作的文件路径
        File file = new File("/Users/fuglee/Documents/TestIO");
        // 文件是否存在
        System.out.println("文件存在么? "+file.exists());
        // 查看文件是否是普通文件
        System.out.println("是普通文件么? " + file.isFile());
        // 查看文件是否是目录
        System.out.println("此文件是目录么? " + file.isDirectory());
    }

Running result:
insert image description here


The file I created is a directory file, and the common instructions are as follows:

  • Check if the file exists: file.exists;
  • Determine whether it is an ordinary file: file.isFile();
  • Determine whether it is a directory file: file.isDirectory();

Relevant operations on the directory;

  • Create a directory file:file.mkdirs();
  • Get the parent path of this directory file: file.getParent();
public static void main(String[] args) {
    
    
        File file = new File("/Users/fuglee/Documents/TestIO/test.dir");
        System.out.println("此目录文件是否存在? " + file.exists());
        // 创建目录文件
        file.mkdirs();
        System.out.println("此目录文件是否创建成功? " + file.exists());
        // 得到此目录文件的父路径
        System.out.println("父路径为: " + file.getParent());

    }

List all hierarchical file instructions in the directory: file.listFiles(); In order to avoid the failure of the operation and cause the main method to be unusable, here we create a thread to run alone;

static class MyThread implements Runnable {
    
    

        @Override
        public void run() {
    
    
            File file = new File("/Users/fuglee/Documents/TestIO");
            listAllFiles(file);
        }
    }
    public static void main(String[] args) {
    
    
        // 加入一个线程防止此操作失败阻塞 main 方法
        Thread thread = new Thread(new MyThread());
        thread.start();
        System.out.println("开始文件的输出!");
    }

    // 递归的罗列出一个目录中的所有文件
    private static void listAllFiles(File file) {
    
    
        if (file.isDirectory()) {
    
    
            //如果是目录,就把目录中包含的文件都罗列出来.
            File[] files = file.listFiles();
            for (File f : files) {
    
    
                listAllFiles(f);
            }
        } else {
    
    
            // 把这个文件的路径直接打印出来
            System.out.println(file);
        }
    }

insert image description here

2 character stream and byte stream

  The above operations are only for file and directory operations, but if you want to know the contents of the file, you need to complete the stream operation; the type of data we save in network transmission or disk is bytes, and all the data in the disk must be read into the memory before the operation can be performed. At this time, the memory will convert the bytes into characters; therefore, the most essential difference between the byte stream and the character stream: the byte stream is a native operation, while the character stream is a processed operation.

Whether it is a byte stream or a character stream, the basic operation process is similar, as follows:

  • Step 1: Create a File class object according to the file path;
  • Step 2: instantiate the parent class object according to the subclass of byte stream or character stream;
  • Step 3: Read or write data;
  • Step 4: close() closes the stream.

2.1 Byte output stream and input stream

public static void outPut(String path) throws IOException {
    
    
        File file = new File(path);
        // 需要保证父目录存在, 如果不存在就创建
        if(!file.getParentFile().exists()) {
    
    
            file.getParentFile().mkdirs();
        }
        // OutputStream 是一个抽象类, 需要通过子类进行实例化
        OutputStream outputStream = new FileOutputStream(file);
        // 输出到文件的内容
        String str = "今天天气不错";
        // 将内容转换为字节并写入
        outputStream.write(str.getBytes());
        // 一定要记得关闭
        outputStream.close();
    }
    public static void inPut(String path) throws IOException {
    
    
        File file = new File(path);
        // 判断文件是否存在, 存在才可以操作
        if (file.exists()) {
    
    
            InputStream inputStream = new FileInputStream(file);
            // 1024 为每次最多读取的数量
            byte[] msg = new byte[1024];
            int len = inputStream.read(msg);
            String result = new String(msg,0,len);
            System.out.println("文件中的内容是: " + result);
            inputStream.close();
        }
    }
    public static void main(String[] args) throws IOException {
    
    
       // 将内容写到文件中
        String path = "/Users/fuglee/Documents/TestIO/1.txt";
        outPut(path);
        // 将文件中的内容读出来
        inPut(path);
    }

operation result:
insert image description here

A few notes about the code above:

  • Since OutputStream is an abstract class, it needs to be instantiated through subclasses, here only need to care about the construction method of subclasses; if it is OutputStream, use FileOutputStream class to handle it; if it is InputStream, use FileInputStream class to handle it;
  • Convert the content to a byte array when writing to the file.

2.2 Character output stream and input stream

The operation of character stream and byte stream is similar, and the basic process is almost the same;

g result = new String(data, 0 , len);
            System.out.println("文件中的内容为: " + result);
            in.close();
        }
    }
    public static void myWriter(String path) throws IOException {
    
    
        File file = new File(path);
        // 要保证父目录存在
        if (!file.getParentFile().exists()) {
    
    
            // 如果不存在就创建
            file.getParentFile().mkdirs();
        }
        String mas = "明天去上班";
        Writer out = new FileWriter(file);
        out.write(mas);
        out.close();
    }
    public static void main(String[] args) throws IOException {
    
    
        String path = "/Users/fuglee/Documents/TestIO/1.txt";
        myWriter(path);
        // 读取内容
        myReader(path);
    }

Running result:
insert image description here
Writer and Reader are also abstract classes, which need to be instantiated through subclasses.

2.3 About the difference between byte stream and character stream

  • From the above code, it can be seen that the actual usage steps of byte stream and character stream are not too different, but in the actual development process, it is recommended to use byte stream to read or fetch data, because character stream is aimed at processing Chinese, and character stream is only considered when processing Chinese;
  • All characters are processed through the memory buffer. For all character stream operations, whether it is writing or output, the data is first stored in the cache. If the character stream is not closed, the data may be stored in the buffer and not output to the target source. In this case, it must be refreshed to get complete data;
  • If you want to process pictures/music, etc., you can use byte streams, but character streams cannot;
  • Conversion between character streams and byte streams is possible:
  • OutputStreamWriter: can convert byte output stream into character output stream; InputStreamReader can convert byte input stream into character input stream.


3 Several versions of the copy of the document

  Regarding file copying, it is mainly displayed in the form of byte streams. The frequency of actual development and use of byte streams is higher. After all, character streams still have limitations;

Version one:

public static void main(String[] args) throws IOException {
    
    
        copyFIle();
    }
    private static void copyFIle() {
    
    
        try (FileInputStream fileInputStream = new FileInputStream("/Users/fuglee/Documents/TestIO/1.txt");
             FileOutputStream fileOutputStream = new FileOutputStream("/Users/fuglee/Documents/TestIO/2.txt")) {
    
    
            byte[] buffer = new byte[1024];
            int len = -1;
            while ((len = fileInputStream.read(buffer)) != -1) {
    
    
                fileOutputStream.write(buffer,0,len);
            }
        } catch ( IOException e) {
    
    
            e.printStackTrace();
        }
    }

The operation steps of the byte stream are roughly the same, and the code description is as follows:

  • Open the file first to be able to read and write operations; there is an upper limit to the content of the word read, if you want to read the entire file, you need to use it with a loop;
  • If len is equal to -1, it means that the reading is over, and the loop is over;
  • After reading successfully, write the read content to another file.

Version two:

public static void main(String[] args) throws IOException {
    
    
        copyFile();
    }

    private static void copyFile() {
    
    
        try( BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("/Users/fuglee/Documents/TestIO/1.txt"));
             BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("/Users/fuglee/Documents/TestIO/2.txt"))) {
    
    
            int len = -1;
            byte[] buffer = new byte[1024];
            while (( len = bufferedInputStream.read(buffer)) != -1) {
    
    
                bufferedOutputStream.write(buffer,0,len);
            }
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }

The buffer is used here. Reading a relatively small file may not show the advantage of the buffer, but for a relatively large file, it will take a long time if the buffer is not used, so adding the buffer can reduce the time consumption; other steps are no different from version 1.

Appendix: The file copy code about the character stream is as follows:

Version 1: Normal file copy

public static void main(String[] args) {
    
    
        copyFile();
    }

    private static void copyFile() {
    
    
        try (BufferedReader bufferedReader = new BufferedReader(new FileReader("/Users/fuglee/Documents/TestIO/1.txt"));
             BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("/Users/fuglee/Documents/TestIO/2.txt"))) {
    
    
            char[] buffer = new char[1024];
            int len = -1;
            while ((len = bufferedReader.read(buffer)) != -1) {
    
    
                bufferedWriter.write(buffer, 0, len);
            }
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }

Version 2: Read by row (can be used to limit only the first n rows of data)

public static void main(String[] args) {
    
    
        copyFile();
    }

    private static void copyFile() {
    
    
        try (BufferedReader bufferedReader = new BufferedReader(new FileReader("/Users/fuglee/Documents/TestIO/1.txt"));
             BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("/Users/fuglee/Documents/TestIO/2.txt"))) {
    
    
            String line = " ";
            while ((line = bufferedReader.readLine()) != null) {
    
    
                System.out.println("line: " + line);
                bufferedWriter.write(line + "\n");
            }
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }

operation result:

insert image description here

Guess you like

Origin blog.csdn.net/Onion_521257/article/details/129555591