20th Day -01-io stream (file overview)
File Class:
Used to encapsulate files or folders into objects for easy Operation.
The file object can be passed as a parameter to the constructor of the stream Object.
The stream object cannot manipulate the folder; The stream object cannot manipulate the File's property information (rwx, Etc.) and can only manipulate the File's Data.
Construction Method:
File (file parent, String child) Creates a new file instance from a parent abstract pathname and a child pathname String.
File (String Pathname) creates a new File instance by converting the given pathname String to an abstract pat Hname.
File (string parent, string Child) creates a new File instance from a parent pathname String and a child Pathna Me string.
packagebxd;Importjava.io.File; public classFiledemo { public Static voidConsmethod () {//encapsulates a a.txt as a file object, which can be encapsulated into an object, either by an existing or not created "files or folders."File file1 =NewFile ("a.txt"); File file2=NewFile ("/users/eric/documents/ideaprojects/servlet_jsp/jdbcdemo/b.txt"); String Parent= "/users/eric/documents/ideaprojects/servlet_jsp/jdbcdemo"; File File3=NewFile (parent, "c.txt"); /*static String separator The system-dependent default Name-separator character, represented as a str ing for convenience. */File file4=NewFile (file.separator + "abc" + file.separator + "xyz.txt"); //the parameters passed in when the file object was created are printed as a string and will not Change.SOP ("file1:" +file1); SOP ("file2:" +file2); SOP ("file3:" +file3); SOP ("file4:" +file4); } public Static voidmain (string[] Args) {consmethod (); } public Static voidSOP (object Object) {System.out.println (object); }}
20th Day -02-io Stream (file object feature-create and Delete)
Boolean CreateNewFile ()
Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet Exi St.
Boolean mkdir ()
Creates the directory named by this abstract Pathname.
Boolean mkdirs ()
Creates the directory named by this abstract pathname, including any necessary but nonexistent parent Directories.
Static File createtempfile (string prefix, string suffix)
Creates an empty file under the default Temporary-file directory, using the given prefix and suffix to generate its name.
Static file Createtempfile (string prefix, string suffix, File Directory)
Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its Name.
Boolean Delete ()
Deletes the file or directory denoted by this abstract pathname.
void Deleteonexit ()
Requests that the file or directory denoted by this abstract pathname is deleted when the virtual machine terminates.
packagebxd;Importjava.io.File;Importjava.io.IOException;/*Common methods of the file Class: 1. Create a Boolean createnewfile () to create a file at the specified Location. returns true if the creation succeeds, or False if the creation fails or if the file already Exists. By comparison, a new output stream object creates a file immediately and overwrites the source file if it encounters a file that already exists. Boolean mkdir () creates the specified directory, boolean mkdirs () to create the specified directory, including all required but nonexistent parent directory 2. Deleting a boolean delete () delete fails returns false. The void Deleteonexit () file may be being accessed by the program and cannot be deleted, at which point the file can be set to be deleted when the program Exits.*/ public classFileDemo1 { public Static voidMethod_1 ()throwsioexception {file File=NewFile ("file.txt"); SOP ("create file.txt:" +File.createnewfile ()); } public Static voidMethod_2 ()throwsIOException {File dir=NewFile ("dir_abc"); Dir.mkdir (); } public Static voidmethod_3 () {file file=NewFile ("file.txt"); SOP ("delete file.txt:" +File.delete ()); } public Static voidMain (string[] Args)throwsIOException {method_2 (); } public Static voidSOP (object Object) {System.out.println (object); }}
20th Day -03-io Stream (file object Function-judging)
Boolean CanExecute () Tests Whether the application can execute the file denoted by this abstract pathname.
Boolean canRead () Tests Whether the application can read the file denoted by this abstract pathname.
Boolean canWrite () Tests Whether the application can modify the file denoted by this abstract pathname.
Boolean exists () Tests whether the file or directory denoted by this abstract pathname Exists.
Boolean isdirectory () Tests Whether the file denoted by this abstract pathname is a directory.
Boolean isfile () Tests Whether the file denoted by this abstract pathname is a normal file.
Boolean Ishidden () Tests whether the file named by this abstract pathname is a hidden file.
Boolean isabsolute () Tests Whether this abstract pathname is Absolute.
packagebxd;Importjava.io.File;Importjava.io.IOException;/*Common methods of the file Class: 3. Judge Boolean exists () to determine whether a file/folder exists in Boolean canexecute () to determine if the file is executable, and return False if the file does not exist, Boolean IsF Ile () Boolean isdirectory to determine if the file object is files/directories and returns false if it does not exist, so be careful when Judging: yes/no/no Boolean isabsolute () to determine Whether the corresponding pathname is an absolute path (regardless of whether the File/folder exists)*/ public classFileDemo2 { public Static voidMethod_1 ()throwsioexception {file File=NewFile ("file.txt"); SOP ("file.txt exists:" +file.exists ()); SOP ("file.txt canexcute:" +File.canexecute ()); } public Static voidmethod_2 () {file file=NewFile ("file.txt"); SOP ("file.txt isfile:" +File.isfile ()); SOP ("file.txt isdirectory:" +file.isdirectory ()); } public Static voidMain (string[] Args)throwsIOException {method_2 (); } public Static voidSOP (object Object) {System.out.println (object); }}
20th Day -04-io Stream (file object Function-get)
Bi Xiangdong _java Basic Video tutorial 20th day _io Stream (1~4)