Java File class

Source: Internet
Author: User

Package Pack;import Java.io.file;import Java.io.filenamefilter;public class Main {public static void sys (Object obj) {    System.out.println (obj);        } public static void Main (string[] args) throws Exception {//method1 (); METHOD2 ();//list directory under File name Method3 ();//List all filenames under directory} public static void Method1 () throws Exception {file F  1 = new File ("E:\\2.txt");        There is no new file created ("E:\\abc", "3.txt"), and F2 SYS (F1); Print path sys (f1.createnewfile ());        Create File//sys (F1.delete ());//delete//f1.deleteonexit ();//delete on exit, usually for temporary file sys (F1.canexecute ());//whether executable        SYS (f1.exists ());//Whether there is a file dir = new file ("E:\\abc"); Dir.mkdir ();        Create folder File Dir1 = new file ("E:\\BB\\CC");        Dir1.mkdirs ();  SYS (Dir.isdirectory ());  is the directory sys (Dir.isfile ());           is the file sys (Dir.ishidden ());//is the hidden file sys (Dir.isabsolute ());//is absolute path sys (Dir.getpath ()); Query Sys (dIr.getname ());        SYS (Dir.getabsolutepath ());        SYS (dir.lastmodified ());//Last Modified time Sys (DIR.LENGTH ());        SYS (dir.getparent ());//parent directory file Dir2 = new file ("E:\\ac"); Dir.renameto (DIR2);//modify file name} public static void Method2 () throws Exception {//list directory under file name/*file[] files = File        . Listroots (); for (File f:files) sys (f); *//*file F = new File ("e:\\");        Print e-disk file name file[] files = f.listfiles ();  for (file f1:files) sys (F1); */File F = new file ("e:\\"); Add filter, output TXT file name string[] files = f.list (new FilenameFilter () {//anonymous class, similar to Comparator public boolean accept (file di                R,string name) {if (Name.endswith (". txt")) return true;            else return false;        }        });    for (String s:files) sys (s);        } public static void Method3 () throws Exception {//List all filenames under file F = new file ("e:\\"); Show (f);        public static void Show (File f) {sys (f);        file[] files = f.listfiles ();            for (File s:files) {if (S.isdirectory ()) show (s);        else SYS (s); }    }}

Java File class

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.