1. What is a stream?
The flow in Java is an abstract concept, and when a program needs to read data from a data source, it opens a stream of data that can be a file, memory, or network, and so on.
2. Manipulating files or directory properties using the file class
public class Kt {
public static void Main (string[] args) {
File f= New file ("Src/zhu.txt");---> Create a File Object
"Src/zhu.txt" indicates the file path name pointed to
System.out.println (f);
Determine if a file exists
Boolean flag = F.exists ();
SYSTEM.OUT.PRINTLN (flag);
Determine if it is a file
Flag = F.isfile ();
SYSTEM.OUT.PRINTLN (flag);
Whether it is a directory
Flag = F.isdirectory ();
SYSTEM.OUT.PRINTLN (flag);
Get File relative path
String path = F.getpath ();
SYSTEM.OUT.PRINTLN (path);
Gets the absolute path of the file
Path =f.getabsolutepath ();
SYSTEM.OUT.PRINTLN (path);
Get the name
String name = F.getname ();
SYSTEM.OUT.PRINTLN (name);
Delete files or directories
Flag = F.delete ();
SYSTEM.OUT.PRINTLN (flag);
Return file length
Long L = f.length ();
System.out.println (l);
}
}
Operation Result:
E:\Zhu.txt
True
True
False
E:\Zhu.txt
E:\Zhu.txt
Zhu.txt
True
0
Note: 1) First create a text file
Example: Zhu.txt
2) in the Windows operating system, the delimiter in the file path name can use a forward slash "/" such as "Src/zhu.txt"
You can also use the backslash "\", but it must be written as "\ \" where the first one represents an escape character, such as "Src\\zhu.txt"
3) file paths are divided into two types:
Relative paths: Non-web projects are relative to the project as a starting point,< > (example: Src/a.txt)
Absolute path: D:/bin/a.txt (starts with a drive letter)
4) file cannot manipulate the contents of files
File in Java