The file operation of the Java-io stream

Source: Internet
Author: User

The file class of Java is primarily used to manipulate the metadata of the files, with a little demonstration as follows:

The method Getalljavafile () is the use of the filter filefileter, this filter only need to implement the Accept method, to determine what kind of file returns true on the line. Pretty simple Direct Sticker code:

    /** * The file class is primarily used to manipulate the metadata of files * @param args * @throws IOException */public static void Main (string[] Ar        GS) throws IOException {File File = new file ("file.txt");        if (file = = null) {file.createnewfile ();        } System.out.println (File.getname ());        System.out.println (File.getparent ());        System.out.println (File.getpath ());        System.out.println (File.getabsolutepath ());        System.out.println (File.length ());        System.out.println (File.getusablespace ());        System.out.println (File.lastmodified ());        Boolean isdelete = false; if (Isdelete = File.delete ()) {System.out.println ("Delete succeeded!        ");        } if (File.isdirectory ()) {System.out.println ("This is a folder");        } if (File.isfile ()) {System.out.println ("This is a folder");        } if (File.exists ()) {System.out.println ("The file exists ~"); } System.out.println ("Traverse folder:" + System.getproperty ("usEr.dir "));        File RootDir = new file (System.getproperty ("User.dir"));        Getallfiles (RootDir);                FileFilter filter = new FileFilter () {@Override public boolean accept (File pathname) {                if (Pathname.getname (). EndsWith ("Java")) {return true;            } return false;        }        };        System.out.println ("=========================================================================");        Getalljavafile (RootDir, filter);    System.out.println ("=========================================================================");            } private static void Getalljavafile (File RootDir, FileFilter filter) {if (Rootdir.isdirectory ()) {            file[] Javafiles = rootdir.listfiles (filter);            for (File item:javafiles) {System.out.println (Item.getname ());            } file[] files = rootdir.listfiles ();             for (File item:files) {   if (Item.isdirectory ()) {Getalljavafile (item, filter);        }}}} else {System.out.println (Rootdir.getname ()); }} private static void Getallfiles (File rootdir) {if (Rootdir.isdirectory ()) {file[] files = ro            Otdir.listfiles ();                for (File item:files) {if (Item.isdirectory ()) {getallfiles (item);                } else {System.out.println (Item.getname ());        }}}} else {System.out.println (Rootdir.getname ()); }    }

Properties are primarily used to store configuration information that can be read from the stream, stored on disk, and is basically used:

1, GetProperties and SetProperties methods are used to obtain and set corresponding key value pairs;

2. The list method is used to view all information,

3, the store method is used to write configuration information to disk;

 public static void main (string[] args) throws IOException {Properties prop        Erties = new Properties ();        Properties.setproperty ("1", "1");        Properties.setproperty ("2", "2");        Properties.setproperty ("3", "3");        Properties.setproperty ("4", "4");        PrintStream print = System.out;        Properties.list (print);        Properties Pfromstream = new properties (); Try (FileInputStream in = new FileInputStream (System.getproperty ("User.dir") + "\\eke.test.first.common\\src\\main\\        Resources\\database.properties ")) {pfromstream.load (in);        } pfromstream.list (print);        Pfromstream.setproperty ("Test", "1000"); Try (FileOutputStream out = new FileOutputStream (System.getproperty ("User.dir") + "\\eke.test.first.common\\src\\main        \\resources\\database.properties ")) {Pfromstream.store (out," I has nothing to say~ "); } pfromstream.list (print); 

The results are as follows:

--Listing Properties--4=43=32=21=1--listing properties--port=3306password=123456jdbc.driver= com.mysql.jdbc.driverdatabase=exe_courseserver=jdbc:mysql://localhostusername=roottest=test--Listing Properties --port=3306password=123456jdbc.driver=com.mysql.jdbc.driverdatabase=exe_courseserver=jdbc:mysql:// localhostusername=roottest=1000

  

The file operation of the Java-io stream

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.