IO stream – file management

File f = new File(“test.txt”);

The constructor of file will not create a new file when the file does not exist. The creation of a file from the file object is completed by the constructor of the file stream or the createnewfile method of the file class. This method will only create a nonexistent file and return a Boolean value whether the file was created successfully.

The file class has two other constructors:

File (string path, string name): create a file object with name in the path directory

File (file dir, string name): create a new file object under directory dir

API:

Java. io. File

(1) Boolean canRead()

(2) Boolean canWrite()

(3) Boolean canexecute(): whether the file is readable, writable, or executable

(4) boolean setReadable(boolean state,boolean ownerOnly)

(5) boolean setWritable(boolean state,boolean

ownerOnly)

(6) boolean setExecutable(boolean state,boolean ownerOnly):

(4) (5) (6) set the readable, writable or executable status of this file. If owneronly is true, the status setting is only valid for the file owner; otherwise, it is valid for everyone. These methods return true after setting the status successfully.

(7) static boolean createTempFile(String prefix,String suffix)

(8) static boolean createTempFile(String prefix,String suffix,File directory)

Create a temporary file in the default temporary directory of the system or a given directory, and use the given prefix or suffix to generate the file name

Parameter: prefix prefix string at least 3 characters long

Suffix optional suffix. If it is null, it is used tmp

Directory creates the directory of the file. If NULL, the file is created in the current working directory

(9) Boolean delete(): try to delete the file. If the file is deleted, return true; otherwise, return false.

(10) Void deleteonexit(): request to delete the file when the virtual machine is shut down.

(11) Boolean exists() returns true if the file or directory exists, otherwise false.

(12) String getabsolutepath(): returns a string containing an absolute pathname. Tip: you should use getcanonicalpath instead.

(13) File getcanonicalfile(): returns the file object containing the canonical pathname of the file. In particular, it removes redundant "." Directory, use the appropriate directory separator, and obtain the case processing method selected by the underlying file system.

(14) String getcanonicalpath() returns a string containing the canonical pathname of the file. In particular, it removes redundant "." Directory, use the appropriate directory separator, and obtain the case processing method selected by the underlying file system.

(15) String getname(): returns a string containing the file name of the file object (excluding the path)

Information).

(16) String getparent(): returns the string of the parent name of the file object. If the file object is a file, its parent is the directory containing it; If it is a directory, its father is its parent directory. If it has no parent directory, its father is null.

(17) File getparentfile(): returns the file object of the parent directory of this file directory

(18) String getpath(): returns a string containing the pathname of the file.

(19) Boolean isdirectory(): if this file represents a directory, it returns true; otherwise, it returns false.

(20) Long lastmodified() returns the time (timestamp, milliseconds) when the file was last modified

(21) long length() returns the length of the file measured in bytes, or 0 when the file does not exist.

(22) string [] list() returns a string array consisting of the file name and directory name contained in the file object, or null when the file does not represent a directory.

(23) string [] list (filenamefilter) returns a string array consisting of file names and directory names that meet the filter conditions contained in the file object, or in the case that there is no qualified file

Null is returned when loading.

(24) file [] listfiles() returns an array of file objects corresponding to the files and directories contained in the file object, or null if the file does not represent a directory.

(25) file [] listfiles (filenamefilter): returns an array of file objects corresponding to the files and directories that meet the filter conditions contained in this file object, or null when there is no qualified content.

(26) static file [] listroots(): returns an array of file objects corresponding to all available file roots (for example, on Windows systems, you can get multiple file objects representing the installed hard disk drive, including local drives and network mapped drivers. On UNIX systems, you can only get "/")

(27) Boolean mkdir(): create a subdirectory with the name given by the file object. If the subdirectory is created successfully, it returns true; otherwise, it returns false.

(28) Boolean mkdirs(): unlike MKDIR, this method will create a parent directory when necessary. As long as a required directory cannot be created successfully, it will return false.

(29) Boolean renameto (file newname): returns true if the file name is modified; otherwise, returns false.

(30) Boolean setlastmodified (long time) sets the last modification time of this file. If

If the setting is successful, it returns true; otherwise, it returns false. (time stamp)

(31) Boolean setreadonly(): set this file as read-only. If the setting is successful, it returns true; otherwise, it returns false.

(32) URL to URL () converts the file object into the URL of a file

Java. io. FilenameFilter

Boolean accept (file dir, string name) should be defined to return true when the file can match the filter criteria. Parameter: dir, a file object indicating the directory containing the file, name, and file name

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>