In order to conveniently represent the concept of the file and to store some basic operations for the file, a specialized class--file class is designed in the java.io package.
The file class contains most of the function methods for files and operations, and the object of this class can represent a specific file or folder, so it has been suggested that the class name of the class be modified to filepath, because the class can also represent a folder, more accurately, it can represent a file path.
1. Create a file
1) Boolean java.io.File.createNewFile () throws IOException used to create the file, if the file exists, the creation fails and returns false;
2) New file ("A.txt"); does not create a document entity, just creates a reference to "A.txt".
3) path delimiter: file.separator
2. Delete Files
1) Delete (): Delete file successfully returns True, delete failed return False (Boolean java.io.File.delete ())
2) Deleteonexit (): Automatically delete files when the program exits. Typically used to operate on temporary files created by a program and deleted upon exit. (void Java.io.File.deleteOnExit ())
3. Determine if the file exists
Exists (): Determine if the file exists (Boolean java.io.File.exists ())
4. Create a folder
1) mkdir (): Only "First Level directory" (Boolean Java.io.File.mkdir ()) can be created;
2) mkdirs (): You can create a multi-level directory (Boolean java.io.File.mkdirs ()).
5. File type judgment
1) exists (): Determine whether the file exists, note: must first judge this;
2) Isdirectory (): Determine whether it is a folder;
3) Isfile (): Determine whether it is a file;
4) Ishidden (): Determine whether it is a hidden file;
5) Isabsolute (): Determine whether the absolute path, here regardless of the existence of the file can be judged.
6. Get File information
1) getName (): Get the file name;
2) getParent (): Gets the file parent directory;
3) GetPath (): Get file path;
4) GetAbsolutePath (): Gets the absolute path of the file;
5) LastModified (): Gets the last time the file was modified;
6) Length (): Gets the file size;
7) Renameto (): File cut, cut the file F1 and paste it into F2 (equivalent to right-click f1-> Cut-and-paste->f2 in the same directory)
7. Get the file name under directory
1) listroots (): Get system letter;
2) List (): Gets all the file names under the "x:\\" directory, including hidden files and folders (when invoking the list () method, you must first encapsulate a directory and must exist.) )
3) List (FilenameFilter filter): When listing file names, you can filter operations (such as listing files with the suffix. txt).
4) Listfiles (): Lists the file name under the directory, not including the folder.
5) Listfiles (filefileter filter), list file, filter.
Filter interface: Filenamefileter filefileter
Hidden files cannot be written.
10. Properties Class
1) Common basic operation, set the key value, get the value.
Prop.setproperty (String, string);
Load (inputstream in);
The second parameter is a comment, can write not write, the write will automatically add # prop.store (FOS, "comment");
List (OutputStream out);
Set<string> Stringpropertynames ()
11. Sequenceinputstream class: Connecting multiple Streams
Description: Enumeration (list)
Public interface Enumeration<e> implements the enumeration interface object, which generates a series of elements, one at a time. Successive calls to the Nextelement method will return a series of contiguous elements.
For example, to output all elements of vector<e> V, you can use the following methods:
for (enumeration<e> E = v.elements (); e.hasmoreelements ();)
System.out.println (E.nextelement ());
These methods are enumerated primarily through the elements of the vector, the keys of the Hashtable, and the values in the hash table. The enumeration is also used to assign an input stream to Sequenceinputstream.
Note: The functionality of this interface is duplicated with the functionality of the Iterator interface. Additionally, the Iterator interface adds an optional remove operation and uses a shorter method name. The new implementation should prioritize the use of the Iterator interface instead of the enumeration interface.
There are two ways of doing this:
hasMoreElements () tests whether this enumeration contains more elements.
Nextelement () returns the next element of this enumeration if there is at least one element that can be supplied by this enumeration object.
12, Pipe Flow:
1) usually two stream reads and writes are through the memory variable, carries on the data operation;
2) This introduces pipeline flow, open two threads, a read stream, a write stream, directly docking two streams.
PipedInputStream in = new PipedInputStream ();
PipedOutputStream out = new PipedOutputStream ();
Similar: Path and files are JAVA7 added, it is more convenient than file.
Dark Horse programmer--java Learning Note 11 (File Operation files)