Usage of File class and InputStream, OutputStream

Table of contents

1. File system operation 

File class

2. File content operation 

InputStream 

1. Create an InputStream class object

 2. Read the file

OutputStream

1. Create an OutputStream class object

2. Write a file


      In the previous blog, we introduced the relevant knowledge of the file system in detail. In the file system operation, since the file is on the hard disk, it is not easy to directly write code, so we create a File class object and call the file class object to indirectly access the file on the hard disk.

1. File system operation 

File class

1. Create a File class object

  File file=new File("d:/xixi.jpg");

Initialize by file absolute/relative path.

2. Common methods of the File class:

Code demo:

public static void main(String[] args) throws IOException {
        //1.创建一个file类
        File file=new File("d:/xixi.jpg");
        /*
        File file=new File("d:/xigua/xixi.jpg");
        System.out.println(file.getName());//返回文件/目录名  xixi.jpg
        System.out.println(file.getParent());//返回父目录    d:/xigua
        System.out.println(file.getPath());//返回路径名字符串  d:/xigua/xixi.jpg
        System.out.println(file.getAbsolutePath());//返回绝对路径名字符串 d:/xigua/xixi.jpg
        System.out.println(file.getCanonicalPath());//返回规范路径名字符串D:/xigua/xixi.jpg*/ 
        System.out.println(file.exists());//文件是否存在 true
        System.out.println(file.isDirectory());//文件是否为目录 false
        System.out.println(file.isFile());//文件是否为普通文件 true
    }


2. File content operation 

The InputStream and OutputStream classes deal with binary files, with byte stream as the basic unit.

  • InputStream 

1. Create an InputStream class object

Observing the source code, it is found that InputStream is an abstract class and cannot directly new objects. We create an object by instantiating its subclass FileInputStream.

InputStream inputStream=null;
try {
    inputStream=new FileInputStream("d:/student.txt");
}finally {
    inputStream.close();
}

Be careful not to forget the final close operation. But the above code can be optimized. In Java, there is also the operation of try with resources. It is a try operation with resources, and it will automatically perform a close operation after the end of the try code block.

try(InputStream inputStream=new FileInputStream("d:/student.txt")){
}
指定一个要读的文件

This is due to the fact that InputStream implements the Closeable interface 

 

 2. Read the file

First, the content of the file student.txt we want to read is:

We use the read() method and the while loop to read. When the end of the file is read, it ends and jumps out of the loop.

try( InputStream inputStream=new FileInputStream("d:/student.txt")){
    while(true){
        int ret=inputStream.read();//无参相当于一次读一个字节
        if(ret==-1){
            break;
        }else{
            System.out.println(ret);//ascii码值 
                
        }
   }
}

 result:

 According to the ascii code table, the data can be parsed as 20201209004cjw.

If it is a Chinese character: three bytes for each Chinese character (one byte for each number). (utf-8)

Modify the content of student.txt to Xiaocao, run it, and find the result:

We check the utf-8 character encoding through the website, and print the byte content read above in hexadecimal format.

System.out.printf("%x\n",ret);

View Results:

It is aligned with the above-mentioned byte-by-byte reading.

  • OutputStream

1. Create an OutputStream class object

 try {
      OutputStream outputStream=new FileOutputStream("d:/student2.txt");
 }catch (IOException e){
      e.printStackTrace();
}

2. Write a file

 try {
      OutputStream outputStream=new FileOutputStream("d:/student2.txt");
      outputStream.write(97);
 }catch (IOException e){
      e.printStackTrace();
}

View student2.txt:

Note: read() can read one byte or multiple bytes at a time; write() can also write one byte or multiple bytes at a time


total

The File class is based on file system operations, mainly used to create/delete files, create directories, determine whether the current file is a sea or a directory, return absolute/relative paths, etc. InputStream and OutputStream are based on file content, open files, read content, and write content.

Guess you like

Origin blog.csdn.net/m0_71690645/article/details/130548972
Recommended