The file class is the packaging class of the file name and its directory path. The file class provides an abstraction for processing the complex file and path name problems that most platforms depend on in an independent way.
The file class contains many methods for obtaining file attributes and deleting and renaming files. However, the file class does not contain methods for reading and writing file content.
Model: When we take a file as a house, the object of the file class represents the exterior characteristics of the House, which can change the exterior color of the House, but cannot operate on the interior of the house.
.
You can use a file name as a parameter to create an object, or use a path as a parameter to create an object.
Four constants of the file class: to adapt to the running of different platforms, we usually do not have to write "/", ":" and other file separators and path separators in the program. The file class in Java provides four constants for us.
Field Abstract |
static String
|
pathSeparator
System-related path separator, which is expressed as a string for convenience. |
static char |
pathSeparatorChar
System-related path separator. |
static String
|
separator
Default name separator related to the system. For convenience, it is represented as a string. |
static char |
separatorChar
Default name separator related to the system. |
Pathseparator is the path separator, ";" on the window, and ":" On Unix ":"
Separator is the name separator, "/" on the window, and "/" on Unix. In fact, "/" can also be used in Windows programs or "//"
What are the differences between pathseparator and pathseparatorchar, and separator and separatorchar? In fact, we can see from the above that one is string type and one is char type. separator is a string that represents separatorchar. The same pathseparator is a string that represents pathsepartorchar, there is little difference in Application
Constructor Summary |
File
(File parent, String child)
According Parent abstract path name and child path name string to create a newFile Instance. |
File
(String pathname)
Creates a newFile Instance. |
File
(String parent, String child)
According Create a new parent path name string and child path name stringFile Instance. |
File
(URI uri)
ByFile: Uri is converted to an abstract path to create a newFile Instance. |
Note: Do not directly use the absolute directory name and file name in the program. If "C: // book // test. dat, which can be run in windows but cannot be run in other systems. Use the following string to replace C: // book // test. dat"
New file ("."). GetCanonicalPath () + "book" + file. Separator + "test. dat)
File (Java 2 platform SE 6)