Common methods for IO stream----File class

Source: Internet
Author: User

Package Com.qf.day18_3;import Java.io.file;import Java.io.filenamefilter;import java.io.ioexception;import java.util.date;/** * File class * * @author WGY * */public class Demo1 {public static void main (string[] args) throws Excepti on {//separator ();//Fileconstructor ();//fileope ();//rename ();d irectoryope ();//rename2 ();//listfiles ();} Delimiter public static void separator () {//Path delimiter System.out.println (file.pathseparator); System.out.println (File.pathseparatorchar);//Name Separator System.out.println (file.separator); System.out.println (File.separatorchar);} File construction method public static void Fileconstructor () {//Create file Object File MyFile = new file ("D:\\image\\haha.jpg"); File MyFile2 = new file ("D:\\image", "4.jpg"); File parent = new file ("D:\\image"); File MyFile3 = new file (parent, "4.jpg"); File MyFile4 = new file (parent, "haha.jpg"); File MyFile5 = new file ("D:\\xxx\\yyy\\a.jpg"); System.out.println (MyFile); System.out.println (MyFile2); System.out.println (myFile5);} /** * File Operation create delete judge get rename */public static void filEope () throws Exception {//Create file object absolute path relative path File File = new file ("D:\\a.txt");//1 Create (if file does not exist, create returns True if present, do not create, return false ) Boolean B = File.createnewfile (); System.out.println ("Create file" + b);//2 delete//2.1 Directly Delete Boolean b2=file.delete ();//System.out.println ("delete file" +b2);//2.2JVM Delete ( No direct deletion when the JVM exits)//File.deleteonexit ();//Thread.Sleep (5000);//3 Judging System.out.println ("CanExecute ()" + File.canexecute ());//Whether SYSTEM.OUT.PRINTLN can be executed ("CanRead ()" + file.canread ());//Whether it is readable System.out.println ("CanWrite ()" + File.canwrite ());//Can write System.out.println ("exists ()" + file.exists ());//whether there is System.out.println ("Isabsolute ()" + File.isabsolute ());//is the absolute path System.out.println ("Isfile ()" + file.isfile ()), or whether it is a file System.out.println ("Ishidden () "+ File.ishidden ());//Whether it is hidden//4 Get System.out.println (File.getabsolutepath ()); Gets the absolute path name//String System.out.println (File.getabsolutefile ()); Gets the absolute file name return type FILESYSTEM.OUT.PRINTLN (File.getcanonicalpath ());//Gets the canonical absolute path name System.out.println (File.getname ()); Get file name System.out.println(File.getpath ()); Gets the path System.out.println (new Date (file.lastmodified ())). toLocaleString ());//File Last Modified date System.out.println ( File.length ()); File Length}/** * file renamed */public static void rename () {file File=new file ("D:\\a.txt");  File Newfile=new file ("D:\\mywork\\b.txt"); File.renameto (newFile);//Modify Name}/** * Folder operation create delete judgment get rename */public static void Directoryope () {//Create Filefile dir=new File ("d:\\xxx\\yyy\\zzz");//1 create//boolean B=dir.mkdir ();//Create First-level folder Boolean b2= Dir.mkdirs ();//Create Multilevel folder System.out.println (B2);//2 Delete//2.1 Direct Delete (only one level can be deleted, only the empty directory)//boolean b3=dir.delete ();// SYSTEM.OUT.PRINTLN (b3);//2.2JVM delete//dir.deleteonexit ();//3 judge System.out.println ("exists ()" +dir.exists ()); Whether there is a System.out.println ("Isabsolute ()" +dir.isabsolute ());//is an absolute path System.out.println ("Isdirectory ()" + Dir.isdirectory ());//Is the directory System.out.println ("Ishidden ()" +dir.ishidden ()), or whether it is a hidden//4 get System.out.println (" GetAbsolutePath "+dir.getabsolutepath ()); Gets the absolute path System.out.println ("GetName ()" +dir.getname ()); System.out.println ("GetPath ()" +dir.GetPath ());//system.out.println ("LastModified" +dir.lastmodified ());//}/** * folder renamed */public static void Rename2 () { File Dir=new file ("d:\\xxx\\yyy\\zzz"); File Newdir=new file ("D:\\xxx\\yyy\\hahah");d Ir.renameto (newdir);} /** * Lists the files and sub-files under the folder */public static void Listfiles () {//1 Lists all file Dir=new file ("D:\\image");//string[] Files=dir.list ( );//for (string string:files) {//system.out.println (string);//}//2 filter File string[] files2=dir.list (New FilenameFilter () {public Boolean accept (File dir, String name) {//TODO auto-generated method Stubif (Name.endswith (". png")) {return true;} return false;}}); for (string string:files2) {System.out.println (string);}}}

file recursively deletes all content in a folder


Import Java.io.file;public class Demo4 {public static void main (string[] args) {//TODO auto-generated method Stubdeldir (n EW File ("d:\\xxx"));} public static void Deldir (file dir) {file[] files = dir.listfiles (), if (Files!=null) {for (file file:files) {if (File.isdir Ectory ()) {deldir (file);} else {System.out.println (file.tostring () +file.delete ());//delete file}}system.out.println (dir.tostring () +dir.delete ()) ;//Delete directory}}}



File class recursively traverse folder

Level is used to record the current recursive hierarchy public static void Listchilds (File F, int. level) {    //Generate layered spaces    stringbuffer sb = new Stringbuf Fer ("|--");    for (int i = 0; I < level; i++) {        sb.insert (0, "|  ");    }    file[] Childs = F.listfiles ();    Recursive exit    int length = Childs = = null? 0:childs.length;    for (int i = 0; i < length; i++) {        System.out.println (sb.tostring () + childs[i].getname ());        if (Childs[i].isdirectory ()) {            listchilds (Childs[i], level + 1);}}}    


Common methods for IO stream----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.