Java IO stream (study notes of teacher Sun Tan's tutorial at station B)

Click to view the collection of Java IO streams.
In Java, a file/folder is an object, and a folder is considered a special file

File class

Constructor

 public File(String pathname);//输入文件路径
 public File(String parent, String child);//父目录和子文件
 public File(File parent, String child);//文件夹对象,和子文件

Common method-create function

//创建文件,需要目录存在,若不存在将会报错。如果创建成功则返回true,如果创建失败则返回false(文件已经存在)
public boolean createNewFile() throws IOException;

//创建文件夹,需要父目录存在。成功则返回true。如果文件夹已存在或父目录不存在则返回false
public boolean mkdir();
//创建文件夹,如果父目录不存在则会同时创建父目录和文件夹。如果文件夹已存在则返回false
public boolean mkdirs();

When executing createNewFile, if the directory does not exist, the following exception will be thrown.
Insert picture description here

Common method-delete function

//删除文件
public boolean delete();

If the deletion is successful, it returns true. If there are files/folders under the folder, it cannot be deleted, and false is returned at this time. It will also return false if the file/folder of File does not exist.

Common method-rename/move files

public boolean renameTo(File dest);

There are two situations for the renameTo operation:
1. The file to be renamed is the same as the parent directory of the file passed in, only the file/folder name is different

File file1 = new File("F:\\a.txt");
file1.renameTo(new File("F:\\b.txt"));

At this time a.txt will become b.txt
2. The file to be renamed is different from the directory of the incoming File

File file1 = new File("F:\\a.txt");
file1.renameTo(new File("D:\\b.txt"));

The a.txt under F drive will be cut to D drive. But note that if a is a folder, there can be no files or folders in the folder

Common method-judgment method

File file1 = new File("F:\\a.txt");
System.out.println("是否是文件夹:"+file1.isDirectory());//是否是文件夹
System.out.println("是否是文件:"+file1.isFile());//是否是文件
System.out.println("是否存在:"+file1.exists());//是否存在
System.out.println("是否可读:"+file1.canRead());//是否可读
System.out.println("是否可写:"+file1.canWrite());//是否可写
System.out.println("是否隐藏:"+file1.isHidden());//是否隐藏

Insert picture description here

File file1 = new File("F:\\a");
file1.mkdir();
System.out.println("是否是文件夹:"+file1.isDirectory());//是否是文件夹
System.out.println("是否是文件:"+file1.isFile());//是否是文件
System.out.println("是否存在:"+file1.exists());//是否存在
System.out.println("是否可读:"+file1.canRead());//是否可读
System.out.println("是否可写:"+file1.canWrite());//是否可写
System.out.println("是否隐藏:"+file1.isHidden());//是否隐藏

Insert picture description here

Common method-get features

File file1 = new File("test/a.txt");
System.out.println("绝对路径:"+file1.getAbsolutePath());//获取绝对路径 (相对于盘符的路径)
System.out.println("相对路径:"+file1.getPath());//获取相对路径(相对于项目的路径。如果文件不在项目路径里,则返回绝对路径)
System.out.println("名字:"+file1.getName());//获得文件/文件夹名字
System.out.println("长度(字节):"+file1.length());//获取长度(字节数) 如果文件不存在则返回0。如果file1是文件夹,则不能得到正确的大小。
System.out.println("最后一次修改时间(毫秒):"+file1.lastModified());//获取最后一次的修改时间(毫秒)如果文件不存在则返回0

Insert picture description here

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

    File file1 = new File("src");
    String[] list = file1.list();//返回目录下所有文件/文件夹的名称数组
    for (String s : list) {
    
    
        System.out.println(s);
    }

    File[] files = file1.listFiles();//返回目录下所有文件/文件夹的File数组
    System.out.println(getSize(files));//打印文件夹大小

    }
    //获取文件夹大小
static int getSize(File[] files){
    
    
     int size = 0;
     for (File file : files) {
    
    
         //获取文件夹的大小
         if(file.isFile()){
    
    
             size += file.length();
         }else {
    
    
             size += getSize(file.listFiles());
         }
     }
     return size;
 }

IO style

Data can be divided into character data and byte data. Character data means that we can read it after being converted into characters, such as text information. Byte stream data means that we can't understand it after being converted into characters. Such as audio, video and pictures.
Byte input stream: InputStream
Byte output stream: OutputStream

Character input stream: Reader
character output stream: Writer

Regardless of byte data or character data, bytes are actually stored. Character data can be directly converted into characters that can be seen by adults through the code table. And byte data needs to use the corresponding software to convert the byte into something that the corresponding person can look at.
In fact, a character stream can also be regarded as a byte stream.

FileOutPutStream

class FileOutputStream extends OutputStream
/*File file = new File("a.txt");
OutputStream os = new FileOutputStream(file);*/

OutputStream os = new FileOutputStream("a.txt");
os.write("你好".getBytes());
os.close();

Guess you like

Origin blog.csdn.net/qq_30033509/article/details/114782969