Overview of the file class and how to construct
it
Construction Method:
- File (String pathname): Gets the file object based on a path
- File (String parent,string child): Gets the file object based on a directory and a sub-file/directory
- File parent,string Child: Gets the file object based on a parent file object and a sub-file/directory
File should be called a path, path, or folder path.
file refers to the abstract representation of the path name of a document or directory form
Create Features:
- public boolean CreateNewFile (): Creates a file, and if such a file exists, it is not created
- public boolean mkdir (): Creates a folder (directory) that is no longer created if such a folder exists
- public boolean mkdir (): Create folders iteratively, multi-layered folder nesting created
Duplicate name and delete function:
- public boolean Renameto (file dest): Names the files as specified file paths (as with Linux, the name of the duplicate is actually a paste)
- public boolean Delete (): Delete files or folders (this deletion is not through the Recycle Bin)
Judging function:
- public boolean isdirectory (): Determines whether an empty directory
- public boolean isfile (): Determines whether the file is
- public Boolean exists (): Determine if there is
- public boolean CanRead (): Determines whether it is readable
- public boolean canWrite (): Determines if writable
- public boolean Ishidden (): Determines whether to hide
Get Features:
- Public String GetAbsolutePath (): Gets the absolute path
- Public String GetPath (): Get Path
- Public String getName (): Get Name
- public long Length (): Gets the length (number of bytes in the file)
- Public long LastModified (): Gets the last modified time, millisecond value
- Public string[] List (): Gets an array of names for all files or folders under the specified directory
- Public file[] ListFile (): Gets the file array of all files or folders under the specified directory
We implement a file name filter by ourselves
Packagenull08012133;ImportJava.io.File; Public classDemo01 { Public Static voidMain (string[] args) {//TODO auto-generated Method StubFile File =NewFile ("c:/"); File[] FileList=File.listfiles (); for(File fileobj:filelist) {if(Fileobj.isfile () &&fileobj.getname (). EndsWith (". Sys")) {System.out.println (Fileobj.getname ()); } } }}
Use some of the built-in methods to judge
Packagenull08012133;ImportJava.io.File;ImportJava.io.FilenameFilter; Public classDemo02 { Public Static voidMain (string[] args) {File file=NewFile ("c:/"); String[] FileNames= File.list (NewFilenameFilter () {@Override Public BooleanAccept (File dir, String name) {//TODO auto-generated Method StubFile f =NewFile (dir,name); returnF.isfile () &&name.endswith (". Sys")); } }); for(String filename:filenames) {System.out.println (fileName); } } }
Java Learning Path (11): IO Stream < foreplay >