------<ahref= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! -------
Dark Horse Programmer--24,file class, Properties class,
/*
Application of the file class:
Encapsulates objects for file or folder operations.
*/
Import Java.io.*;class ioliou16{publicstatic void Main (string[] args) { File F1=new file ("f:\\ amazon \ \ amazon 1th. txt"); /* Encapsulates Amazon 1th. txt as an object that can be encapsulated as an object for an existing file, or as a file that does not already exist. */File F2=new file ("f:\\ Amazon", "Amazon 1th. txt"); /* This is the same as the previous sentence, except that it is more flexible to separate the specified position and file into two parameters. */File F3=new file ("f:\\ Amazon"); File F4=new file (F3, "Amazon 1th. txt"); Soc ("f1=" +f1);//print f1=f:\ amazon \ amazon 1th. txt Soc ("f2=" +f2);//print f2=f:\ amazon \ amazon 1th. Txt Soc ( "f3=" +f3);//print is f3=f:\ Amazon Soc ("f4=" +f4);//print f4=f:\ amazon \ amazon 1th. txt file f5=new file ( "F:\\ Amazon" +file.separator+ "Amazon 1th. txt"); FIle.separator represents only the universal delimiter Soc ("f5=" +f5);//print f5=f:\ amazon \ amazon 1th. txt} public static void Soc (Object obj) {System.out.println (obj); }}
—————— Split Line ——————
/*
method invocation of the file class
*/
Import java.io.*;class ioliou17{public static void Main (string[] args) throwsioexception { Createnewfiledemo (); Deletedemo (); Mkdirsdemo (); Isabsolutedemo (); Renameto (); Lastmodifieddemo (); } public static void Createnewfiledemo () throwsioexception {file F=new file ("F:\ \mianbao.txt "); The following is a file created on the specified path, Boolean b =f.createnewfile (); Soc (b); /* CreateNewFile Returns a Boolean if the file is created on the specified path Mianbao.txt the success returns true, otherwise false Note: If a file with the same name under the specified path is not created, the return is false and the IO exception is generally thrown with the IO stream, and do not forget this. If this is the case: file F=new file ("f:\\ bread \\mianbao.txt"); Boolean b=f.createnewfile (); This is the compile display IO exception and the specified path cannot be found. (I have no bread this folder on my computer's F-disk at this time) that means the path must be real. */} public static void Deletedemo () throws IOException {File f=new Fil E ("F:\\mianbao.txt"); F.createnewfile ();//Create File Boolean b= f.delete (); Soc (b); /* The Delete method deletes the file, the method returns a Boolean, and the delete succeeds returns true, otherwise false in the file class A method deleteonexit if used in this example f.deleteonexit (); indicates that the Java program deletes the file before the end of the run */} public static void Existsdemo () throws IOException {file F=new file ("e:\\javawenjian\\ioliou17\\ioliou.17"); Boolean b=f.exists ();//Determines whether the file exists on the SOC (b) in the specified path; } public static void Mkdirdemo () throws IOException {file F=new file ("Shuei\\klkl"); Boolean b= f.mkdir (); /* MkDir method establishes a first-level directory, if the specified directory is established successfully returns true otherwise returns false below a common situation: A, file F=new file ("f://single"); Boolean b=f.mkdir ();//Return to True also can specify which disk to store two, File f=new Fil E ("Shuei//klkl"); Boolean b=f.mkdir ();//return to False can only specify to establish a first-level directory */SOC (b); } public static void Mkdirsdemo () throws IOException {file F=new file ("Spinach \ \ good spinach \ \ \ 's Better Spinach "); Boolean b= f.mkdirs (); The Mkdirs method can establish a multilevel directory Soc (b); Soc ("f.exists () =" +f.exists ()); The Exists method can also be used to determine whether a folder file F2=new file ("fruit. txt") exists under the specified path. Boolean b2=f2.mkdirs ();//The folder named "Fruit. txt" is created here! Soc (B2); So MkdiThe R and Mkdirs methods are all built to be folders! } public static void Pd () {File F=new file ("F:\\ mania \ Hot. txt"); Boolean b1= f.isfile ();//Determines whether the encapsulated object is a file Boolean b2 = F.isdirectory ();//Determines whether the encapsulated object is a folder (path) /* In this case, there is no txt file named "Hot" under the specified path, so both B1 and B2 are false */ Soc ("b1=" +b1+ "b2=" +b2); } public static void Isabsolutedemo () throws IOException {file F=new file ("Fantasy 1\\ fantasy novel. Tx T "); Boolean B=f.isabsolute (); /* Isabsolute method determines whether an absolute path, even if the file does not exist as long as the absolute path is returned true the so-called absolute path is from Which disk starts to write is the absolute path */SOC ("b=" +b); Soc ("f.getpath () =" +f.getpath ());//f.getpath () = Fantasy 1\ fantasy novel. txt Soc ("f.getparent () =" +f.getparent ());//f.getparent () = Fantasy 1//GetParent method returns the parent directory of the absolute path otherwise returns a null SOC ("f.getabsolute () =" + F.getabsolutepath ()); Printed is F.getabsolute () =e:\javawenjian\ fantasy 1\ Fantasy novel The. txt//getabsolutepath method returns an absolute path Soc ("f.getname () =" +f.getname ());//f.getname () = fantasy novel. txt} public static void Lastmodifieddemo () {File F=new file ("E:\\javawenjian\\ioliou17.java"); Long l= f.lastmodified (); The LastModified method returns the last time the file was modified by the SOC ("f.lastmodified () =" +f.lastmodified ()); public static void Renameto () {File F=new file ("f:\\ untitled 23copy.png"); File F2=new file ("d:\\ untitled 23copy.png"); F.renameto (F2); The effect is: The untitled 23copy.png in the F disk is clipped to the file F3=new file ("F:\\yuyuyu.java") on the D disk; File F4=new file ("D:\\shue.java"); F3.renameto (F4); The effect is: the Yuyuyu.java in the F drive is clipped to the D drive and renamed to Shue.java} public static void Soc (Object obj) {S Ystem.out.println (obj); }}
—————— Split Line ——————
/*
Method application in File class
File list
*/
Import java.io.*;class ioliou18{public static void Main (string[] args) {Listdemo (); Listrootsdemo (); } public static void Listdemo () {SOC ("The following is Listdemo--------"); File F=new file ("F:\\yuyuyu.txt"); String[] s= f.list (); The/* List method returns all file names and folder names under the specified path (not the specified file), and if the specified path is not a file, a null pointer exception is run */ for (String s2:s) {SOC (S2); }} public static void Listrootsdemo () {SOC ("The following is Listrootsdemo-------"); File[] F=file.listroots (); for (File f2:f) {SOC (F2); }/* Printed: The following is Listrootsdemo-------c:d:e:f:g: */} public static void Soc (Object obj) {System.out.println (obj); }}
———————— Split Line ——————
/*file Object function: File list If you only need to enumerate a certain type of file name under a specified path, use FilenameFilter, which is equivalent to a filter, which has a method, Boolean accept (file dir,string name) */import java.io.*;class ioliou19{public static void Main (String[]args) throws IOException {L Istdemo (); } public static void Listfilesdemo () {SOC ("The following is Listfilesdemo----"); File F=new file ("f:\\"); File[] fs= f.listfiles (); for (File Zz:fs) {Soc (Zz.getname () + "---" +zz.length ()); If it is a folder, the printed length is displayed as 0}} public static void Listdemo () { Just print out all of the. txt files in the F-disk file F=new ("f:\\"); String[] s =f.list (new FilenameFilter () {public Boolean accept (File dir,string name) {return Name.endswith (". txt"); } }); for (String s2:s) {SOC (S2); }} public static void Soc (Object obj) {System.out.println ( OBJ); }}
———————— Split Line ——————
Application of the/*file class: Lists all files under the specified path. This requires recursion, and when recursion is used, you should pay attention to the constraints and the number of recursion. */import java.io.*;class ioliou20{ public static void main (string[] args) { file f=new file ("Picture of f:\\ technology blog \ \"); Method (f) ; } Public static void method (File f) { Soc ("This is---" +f); File[] fs=f.listfiles (); for (int x=0;x<fs.length;x++ ) { if (fs[x].isdirectory ()) method (Fs[x]); /* Because there are directories in the directory, it is possible to invoke its own function, which is called the recursive */ Else Soc (fs[x]);//The absolute path is printed } } public static void Soc (Object obj) { System.out.println (obj);} }
———————— Split Line ——————
/* Delete the directory with content */import Java.io.*;class ioliou21{publicstatic void Main (string[] args) {Fi Le f=new File ("f:\\ exercises today \ \"); Method (f); } public static void method (File f) {file[] fs= f.listfiles (); for (int x=0;x<fs.length;x++) {if (Fs[x].isdirectory ()) {method (fs[x]); Soc (fs[x]+ "---folder--" +fs[x].delete ()); } else Soc (fs[x]+ "---" +fs[x].delete ()); } if (F.isdirectory ())//Add this judgment here to delete the outer folder of the SOC (f+ "--Folder--" +f.delete ()); } public static void Soc (Object obj) {System.out.prinTLN (obj); }}
—————— Split Line ——————
/* Create a list of Java files */import java.io.*;import java.util.*;class ioliou22{public static void Main (string[] args) throws IOException {filename f=new file ("F:\\java files Exercise"); List<string> li=new arraylist<string> (); File F2=new file ("F:\\2015.8.10.txt"); Method (F, Li); METHOD2 (LI,F2); } public static void method (File f,list<string> li) {file[] fs =f.list Files (); for (int x=0;x<fs.length;x++) {if (Fs[x].isdirectory ()) Method (Fs[x],li); else if (Fs[x].getname (). EndsWith (". Java")) Li.add (FS[X].GETABSO Lutepath ()); }} public static void Method2 (list<string> lI,file f) throws IOException {//filewriter fw=new FileWriter (F.getabsolutepath ()); FileWriter fw=new FileWriter (f); The FileWriter constructor can also accept the file class object BufferedWriter bfw=new bufferedwriter (FW); for (String S:li) {bfw.write (s); Bfw.newline (); Bfw.flush (); } bfw.close (); } public static void Soc (Object obj) {System.out.println (obj); }}
—————— Split Line ——————
The/*io stream (Introduction to the Properties Class) also features the map collection, which contains strings and no generics. The Properties collection is a subclass of the Hashtable collection object: A configuration file that can be used in the form of key-value pairs, which needs to be fixed when loading data into a container from a file: Key = value f:\\ Practice number No. 8.1. txt content: bvuivb=8heauobgvjeb=02 qcdtycwqd=45buiverhbgvi=09oirjgbitr=20hbn=00iohoii=74oirjgbitr=55 */import Java.io.*;import java.util.*;class ioliou23{public static void Main (string[] args) throws IOException {method3 (); } public static void Method () {Properties Pro=new properties (); Pro.setproperty ("Pupi", "01");//add Element Pro.setproperty ("Huj", "03"); Pro.setproperty ("HJSBF", "12"); Pro.setproperty ("HJSBF", "100");//note there are repeating elements pro.setproperty ("Add", "10"); String s= pro.getproperty ("HJSBF");//Gets the value of the key corresponding to the SOC ("s---" +s); Set jian= pro.stringpropertynames ();//Place all keys inside the set set/* Because properties have the characteristics of a map collection, a pair of key-value pairs repeats, so the new repeating element overwrites the previous one. */} public static void Method2 () throws IOException { Properties Pro=new properties (); FileInputStream fis=new FileInputStream ("f:\\8.10 practice. txt"); Pro.load (FIS); Load the data of the stream into the collection/* f:\\8.10 the data in the. txt is separated by the equal sign (*/) Pro.setproperty ("Kkkkkk", "KK"); Soc (pro);/* When the program executes to this line, open the "No. 8.1 practice. txt" file, and there is no previous line to add the key value to "KKKKKK", "KK" */ Fis.close (); /* Print Result: {bvuivb=8, oirjgbitr=55, kkkkkk=kk,heauobgvjeb=02 qcdtycwqd=45,buiverhbgvi=09, iohoii=74, H BN=00} */} public static void Method3 () throws IOException { PropertiesPro=new Properties (); FileInputStream fis=new FileInputStream ("f:\\8.10 practice. txt"); Pro.load (FIS);//load file data into container pro.setproperty ("Kkkkkk", "KK");//add element to container FileOutputStream fos= New FileOutputStream ("f:\\8.10," "exercise. txt"); Pro.store (FOS, "JK");//The key-value pair stored in the Pro container is equivalent to the update in the file, where JK is only the annotation pro.list (System.out); /* This statement prints out the contents of the container Pro, printing the format:--listing properties--bvuivb=8 OIRJG bitr=55 kkkkkk=kk heauobgvjeb=02 qcdtycwqd=45 buiverhbgvi=09 hbn=00 iohoii= * * * fis.close (); Fos.close (); } public static void Soc (Object obj) {System.out.println (obj); }}
—————— Split Line ————————
/*properties Exercise: (Configuration file creation and use) the number of times the program has been run, the number has been given the registration prompt. The map plus IO technology is the properties configuration file that enables the sharing of application data. */import java.io.*;import java.util.*;class ioliou24{public static void Main (string[] args) throws IOException {//file f=new file ("F:\\jilu.properties");//write suffix name. The properties profile file F=new files ("F : \\jilu.ini "); if (!f.exists ()) f.createnewfile (); FileReader fis=new FileReader (f); Create a read stream object, where you can read the stream with a character stream and use a byte stream to read the flow Properties Pro=new properties ();//Build Properties Object Pro . Load (FIS);//load file data into container String s= pro.getproperty ("Cishu");//Get value int i=0; if (s==null) {pro.setproperty ("Cishu", "1"); } else {i= integer.parseint (s);//Convert a string to a number i++; if (i>=6) {SOC ("times exceeded"); } pro.setproperty ("Cishu", i+ ""); } FileWriter fos=new FileWriter (f); Create a write stream object, where you can write streams with character streams and write stream Pro.store (fos, "haha"), or save the data in a file Fis.close (); Fos.close (); } public static void Soc (Object obj) {System.out.println (obj); }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Dark Horse Programmer--24,file class, Properties class,