Package Com.io.demo;import Java.io.File;/** Two common constants * path separators; File.pathseparator * Name separator under Windows: \ Non windows:/* * relative path and absolute path construct file Object **/ Public classIofile { Public Static voidMain (string[] args) {System. out. println (File.pathseparator);// ;System. out. println (File.separator);// //path representation 1, because of the escape character, use a double slashString Path ="E:\\soft\\book\\1.txt"; //path Representation 2, can do cross-platformPath="E"+file.separator+"Soft"+file.separator+" Book"+file.separator+"1.txt"; //path representation 3, recommendedPath="E:/soft/book/1.txt"; String Parentpath="e:/xp/test"; String name="2.jpg"; //relative PathFile src =NewFile (parentpath,name); SRC=NewFile (NewFile (Parentpath), name); System. out. println (Src.getname ());//2.jpgSystem. out. println (Src.getpath ());//E:\xp\test\2.jpg//Absolute Pathsrc =NewFile ("e:/xp/test/2.jpg"); System. out. println (Src.getname ());//2.jpgSystem. out. println (Src.getpath ());//E:\xp\test\2.jpg//no drive letter, built with User.dirsrc =NewFile ("Test.txt"); System. out. println (Src.getname ());//Test.txtSystem. out. println (Src.getpath ());//Test.txtSystem. out. println (Src.getabsolutepath ());//D:\MyEclipse 10\java300\test.txt } }
Several common ways to get filenames:
GetName (): Return name
GetPath (): Returns the full path if it is an absolute path, otherwise the relative path
GetAbsolutePath (): Returns the absolute path
GetParent (): Returns the previous level of the directory, if it is relative, returns null
Method of judging information:
Exists (): File exists, Boolean
Canwriter (): Whether the file is writable, Boolean
CanRead (): Whether the file is readable
Isfile (): is a file or a folder, there is no default folder
Isdirectory (): Whether it is a directory
Isabsolute (): is an absolute path
Judging length:
Length (): Number of bytes, if the folder length is 0, cannot read, only the length of the file can be read
To create and delete files:
CreateNewFile (): Create file Boolean type
Delete (): Delete file
The constants and Paths of Java--io