One.
Two constant separators in file
PackageFile;ImportJava.io.File;/** * Two constants * 1, Path delimiter; * 2, name separator \ (Windows)/(Linux, etc.) */@SuppressWarnings("All") Public class Demo01 { Public Static void Main(string[] args) {System.out.println (file.pathseparator); System.out.println (File.separator);//Path representationString Path ="E:\\xp\\test\\2.jpg";//Not recommended //For use when dynamically generatedPath ="E:"+file.pathseparator+"Win7"+file.separator+"Test"+file.separator+"2.jpg"; Path ="E:/win7/test/2.jpg";//Recommended Use}}
Operation Result:
;\
Two.
relative path and absolute path
PackageFile;ImportJava.io.File;/** * Relative path and absolute path constructs a file object * 1. Relative path * file (String parent,string child) ==>file ("E:/xp/test", "2.jpg"); * File (Fi Le parent,string Child) ==>file (new File ("E:/xp/test"), "2.jpg"); * 2. Absolute path * src = new File ("e:/xp/test/2.jpg"); * * GetName () returns the name * GetPath () If it is absolute, returns the absolute path, or, if it is relative, returns the relative path. * GetAbsolutePath () returns the absolute path */ Public class Demo02 { Public Static void Main(string[] args) {String Parentpath ="E:/xp/test"; String name ="2.jpg";//relative pathFile src =NewFile (Parentpath,name);//src = new file (new file (Parentpath), name); //OutputSystem.out.println (Src.getname ()); System.out.println (Src.getpath ()); System.out.println ("-------------");//absolute pathsrc =NewFile ("E:/xp/test/2.jpg"); System.out.println (Src.getname ()); System.out.println (Src.getpath ()); System.out.println ("-------------");//No drive letter, one User.dir buildSrc=NewFile ("Test.txt");//src= New File ("."); /current path, in this way you can find the current workspace path. System.out.println (Src.getname ());//Return nameSystem.out.println (Src.getpath ());//If absolute, returns the absolute path, or, if it is relative, returns the relative path. System.out.println (Src.getabsolutepath ()); }}
Operation Result:
2.jpgE:\xp\test\2.jpg-------------2.jpgE:\xp\test\2.jpg-------------test.txttest.txtG:\program\javase\IOFile\test.txt
Three. Common methods of File
Package File;import Java.io.file;import java.io.IOException;/** * Common methods * 1, file name * GetName () filename, path name * GetPath () path name * Getabsolutefile () the file object corresponding to the absolute path * getab Solutepath () absolute path name * GetParent () returns the parent directory * * 2, Judgment information * EXISTS () * CanWrite () * CanRead () * isfile () * isdirectory () * Isabsolute () eliminates platform differences, such as the beginning of the drive letter, other with/start * 3, Length (bytes) cannot read the length of the folder * * 4, create, delete files * CreateNewFile creates a new file, creates it if it does not exist, and returns False if it exists. * Temp File * *@SuppressWarnings ("All") Public classDemo03 { Public Static void Main(string[] args) {test01 (); System. out. println ("========"); Test02 (); System. out. println ("========");Try{test03 (); }Catch(IOException e) {E.printstacktrace (); System. out. println ("file operation failed"); } }///1, get file name Public Static void test01() {//file src = new File ("e:/xp/test/2.jpg");File src =NewFile ("Test/2.txt"); System. out. println (Src.getname ());//Name //If it is an absolute path, return the full path, otherwise the relative pathSystem. out. println (Src.getpath ());//Return absolute pathSystem. out. println (Src.getabsolutepath ());//returns to the previous level directory, which may return NULL if it is a relative path, such as New File ("2.txt");System. out. println (Src.getparent ()); }///2, Judge information reading related information, not read content, but location, size, Occupy space, create time and other properties Public Static void test02() {//string Path = "2.txt";String Path ="G:/love.txt"; File src =NewFile (path); System. out. println ("Does the file exist:"+src.exists ());//Whether it is readable or writableSystem. out. println ("Whether the file can be written:"+src.canwrite ());//isfile (), isdirectory () if(Src.isfile ()) {System. out. println ("File"); }Else if(Src.isdirectory ()) {System. out. println ("Folder"); }Else{System. out. println ("file does not exist"); } System. out. println ("is an absolute path:"+src.isabsolute ()); System. out. println ("Length is:"+src.length ());//This length is the number of bytes}//3, create, delete files Public Static void test03() throws IOException {//Create file //con system keyword, cannot be created successfully. String Path ="F:/100.txt"; File src =NewFile (path);if(!src.exists ()) {Boolean flag = Src.createnewfile (); System. out. println (flag?"created successfully":"Create failed"); }//delete filesBoolean flag = Src.delete (); System. out. println (flag?"Delete succeeded":"Delete Failed");//static createtempfile (prefix 3 bytes long, suffix default. temp) Default temporary space //static createtempfile (prefix 3 bytes long, suffix default. Temp, directory)File temp = File.createtempfile ("tes",". Temp",NewFile ("g:/"));Try{Thread.Sleep ( -); Temp.deleteonexit ();//exit is delete}Catch(Interruptedexception e) {E.printstacktrace (); } }}
Operation Result:
2.txttest\2.txtG:\program\javase\IOFile\test\2.txttest========文件是否存在:true文件是否可写:true文件是否为绝对路径:true长度为:32========创建成功删除成功
Four.
Create a directory
PackageFile;ImportJava.io.File;ImportJava.io.FilenameFilter;/** * Create directory * MKDIR () Creating a directory must ensure that the parent directory exists and if it does not exist, the creation fails. * Mkdirs () Create a directory if the parent directory chain does not exist, create it together. * string[] List () file | directory name string form * file[] listfiles () file | Directory name file Object form * file[] Listfiles (filenamefilter filter) filter */@SuppressWarnings("All") Public class Demo04 { Public Static void Main(string[] args) {test01 (); Test02 (); } Public Static void test01() {String path ="g:/try/123/123/325"; File src =NewFile (path);//src.mkdir ();Src.mkdirs (); System.out.println ("created successfully. "); } Public Static void test02() {//string Path = "g:/try/123";String Path ="G:/work"; File src =NewFile (path);//Folder if(Src.isdirectory ())//exists and is directory{System.out.println ("====== subdirectory | sub-filename" Expression of string "======"); string[] Subnames = Src.list (); for(String temp:subnames) {System.out.println (temp); } System.out.println ("= = Sub-Directory | sub-document" File Object "====="); file[] Subfiles = Src.listfiles (); for(File temp:subfiles) {System.out.println (Temp.getabsolutepath ()); } System.out.println ("====== sub-file, Java object ======");//Command design modeSubfiles = Src.listfiles (NewFilenameFilter () {/** * dir stands for src */ @Override Public Boolean Accept(File dir, String name) {//system.out.println (Dir.getname ()); //system.out.println (Dir.getabsolutepath ()); //must be a file and the suffix is. Java return NewFile (Dir,name). Isfile () &&name.endswith (". rar"); } }); for(File temp:subfiles) {System.out.println (Temp.getabsolutepath ()); } } }}
Operation Result:
创建成功。======子目录|子文件名======123324dsafds.javasdafd.java新建文件夹 (3)新建文件夹 (4)====子目录|子文件File对象=====G:\try\123\123G:\try\123\324G:\try\123\dsafds.javaG:\try\123\sdafd.javaG:\try\123\新建文件夹 (3)G:\try\123\新建文件夹 (4)======子文件,Java对象======G:\try\123\dsafds.javaG:\try\123\sdafd.java
Five.
the name of the output descendant-level directory or file
PackageFile;ImportJava.io.File;ImportJava.util.Arrays;/** * output descendant level directory | The name of the file * 1, ListFile () * 2, recursive * * static listroots () root path * @author liguodong */ Public class Demo05 { Public Static void Main(string[] args) {String Path ="G:/try/123"; File parent =NewFile (path); Printname (parent); System.out.println ("----------"); File[] roots = File.listroots (); System.out.println (arrays.tostring (roots)); } Public Static void Printname(File src) {if(NULL==src| |! Src.exists ()) {return; } System.out.println (Src.getabsolutepath ());if(Src.isdirectory ()) { for(File sub:src.listFiles ()) {Printname (sub); } } }}
Operation Result:
G:\try\123G:\try\123\123G:\try\123\123\325G:\try\123\432G:\try\123\432\1.txtG:\try\123\432\2.txtG:\try\123\432\3.txtG:\try\123\4344----------[C:\, D:\, E:\, F:\, G:\, H:\, K:\]
Java IO file operation