java File class and IO stream

File class

The abstract representation of files and folders (file paths) is specifically used to identify files or folders on the disk.

Construction method

method

Return boolean

creatNewFile()

Generate a file if and only if a file with that name does not already exist

public class Demo02 {

    public static void main(String[] args) throws IOException {
        File file = new File("C:\\Users\\Air\\Desktop\\test\\2.txt");
        // 生成一个2.txt文件
        System.out.println(file.createNewFile());
    }
}
mkdir() / mkdirs()

Generate directory

public class Demo02 {

    public static void main(String[] args) throws IOException {
        File file1 = new File("C:\\Users\\Air\\Desktop\\test\\aa");
       // 生成目录   生成一个aa目录
        System.out.println(file1.mkdir());
    }
}
delete() deletes empty files and directories
isFile() determines whether it is a file
isDirectory() determines whether it is a directory
isHidden() determines whether it is a hidden file
isAbsoute() determines whether it is an absolute path
exists() determines whether a file or folder exists

Return String

getName() gets the name of a file or directory
getPath() gets the path of the current object
getParent() Gets the upper-level directory of the current file object

I流

File upload and download

buffer concept

The computer reads the file data on the disk through the CPU memory, reading 1 byte at a time. But you can add the concept of buffering and read 4kb each time. The efficiency will be higher

IO stream classification: input stream and output stream

Input stream: read from disk to memory

Output stream: written from memory to disk

input stream

The input stream is divided into byte input stream and character input stream

Byte input stream [Important]

There is a 1.txt file on the disk. Now we need to read the contents of the 1.txt file into the memory (java code)

FileInputStream file input stream (byte input stream)

BufferedInputStream() buffer stream

public class Demo {
    public static void main(String[] args) throws IOException {
        // 找到 文件
        File file = new File("C:\\Users\\Air\\Desktop\\test\\1.txt");
        // 将我们找到的文件转为字节输入流的形式  ,之后可以按照流的形式写入到内存中  FileInputStream 不具备缓冲效果
        FileInputStream fis = new FileInputStream(file);
        // 加上缓冲效果
        // BufferedInputStream为另一个输入流添加了功能,即缓冲输入和支持mark和reset方法的功能。 当创建BufferedInputStream时,将创建一个内部缓冲区数组。
        BufferedInputStream bis = new BufferedInputStream(fis);
    }
}
public class Demo02 {
    public static void main(String[] args) throws IOException {
        // 创建File对象
        File file = new File("C:\\Users\\Air\\Desktop\\test\\1.txt");
        // 创建字节流对象
        FileInputStream fileInputStream = new FileInputStream(file);
        // 缓冲
        BufferedInputStream bis = new BufferedInputStream(fileInputStream);
        // 声明一个缓冲数组
        byte[] bytes = new byte[1024 * 4];

        // 开始使用 read 读取
        int length ;
        while ((length = bis.read(bytes))!= -1) {
            // 打印
            System.out.println(new String(bytes,0,length));
        }
        // 关闭流
        bis.close();
        fileInputStream.close();
    }
}
Character input stream [not often used]

It is also an input stream, reading a file from the disk into memory (in java code)

FileReader() file input stream (character input stream)

BufferedReader() buffered stream

It is a convenience class for reading character files. It is specially designed to handle character files, such as txt files. Audio, video and pictures cannot use this stream

public class Demo06 {
    public static void main(String[] args) throws IOException {
        File file = new File("C:\\Users\\Air\\Desktop\\test\\11.txt");
        // 新建一个字符流对象
        FileReader fr = new FileReader(file);
        // 添加缓冲效果
        BufferedReader br = new BufferedReader(fr);
        // 添加字符数组
        char[] chars = new char[4 * 1024];

        int length = -1;

        while ((length = br.read(chars)) != -1) {
            System.out.println(new String(chars,0,length));
        }
        br.close();
        fr.close();
    }
}

output stream

Byte output stream [Important]

Write data in java to disk (in file)

Use write() to write. After writing, you must refresh the stream and then close the stream. Open first, then close, then open, and then close. The sequence cannot be messed up.

FileOutputStream file input stream

BufferedOutputStream buffer

import java.io.*;

public class Demo {
    public static void main(String[] args) throws IOException {
        File file = new File("C:\\Users\\Air\\Desktop\\test\\2.txt");
        // 实例化字节流输出对象
        FileOutputStream fos = new FileOutputStream(file);
        // 添加缓冲效果
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        // 将字符串写入文件中
        String str = "hello world";
        // 将字符串转为字节数组
        byte[] bytes = str.getBytes();
        bos.write(bytes);
        // 写完后一定要刷新流然后关闭流
        bos.flush();   // 没有实际意义,可以不写
        //关闭流
        // 先开的后关 后开的先关 顺序不能乱
        bos.close();
        fos.close();
    }
}

Character output stream [not often used]

Write data in java to disk (in file)

FileWriter character output stream

public class Demo07 {
    public static void main(String[] args) throws IOException {

        File file = new File("C:\\Users\\Air\\Desktop\\test\\22.txt");
        // 创建字符输出流对象
        FileWriter fw = new FileWriter(file);
        // 缓冲效果
        BufferedWriter bw = new BufferedWriter(fw);

        String str = "hello world 你好 世界";

        // 将字符串转为 chat类型数组 toCharArray
        char[] chars = str.toCharArray();

        bw.write(chars);

        // 关闭流
        bw.close();
        fw.close();
    }
}

Guess you like

Origin blog.csdn.net/m0_62336865/article/details/131371545