IO operations in Java

Source: Internet
Author: User

File class

/*** Path delimiter: public static final String separator;    * Set file path: Public files (String pathName);    * Create file: public boolean creatnewfile () throws IOException;    * Determine if the file exists: public boolean exists ();    * Delete File: public boolean delete ();    * Find Parent path: public File getpareentfile ();    * Create Parent path: public boolean mkdir (); * Create multi-level path: public boolean mkdirs (); */ PackageCom.java.demo;ImportJava.io.File; Public classTestdemo { Public Static voidMain (String args[])throwsexception{File FL=NewFile ("E:" +file.separator + "Hello" +file.separator+ "demo" +file.separator+ "Java.txt");//set the full path of the file        if(!fl.getparentfile (). exists ()) {//if the parent path does not existFl.getparentfile (). Mkdirs ();//Create parent Path        }        if(Fl.exists ()) {//determine if a file existsFl.delete ();//If the file exists, delete the file;}Else{fl.createnewfile (); //do not exist then create file        }    }  }
/*** Get file Size: public long length (); * Determine if File: Public boolean isfile (); * Determine if directory: public boolean isdirectory (); * Last modified time : Public long lastmodified (); * Get file Full path: public String getpath (); */ PackageCom.java.demo;ImportJava.io.File;ImportJava.math.BigDecimal;ImportJava.text.SimpleDateFormat;Importjava.util.Date; Public classTestdemo { Public Static voidMain (String args[])throwsexception{File FL=NewFile ("E:" +file.separator + "Msdia80.dll");//set the full path of the fileSystem.out.println (Fl.getpath ());//E:\msdia80.dllSystem.out.println ("is file" +fl.isfile ()); System.out.println ("Is directory" +fl.isdirectory ()); if(Fl.exists ()) {System.out.println ("Size of File:" + (NewBigDecimal ((Double) Fl.length ()/1024/1024). Divide (NewBigDecimal (1), 2,bigdecimal.round_half_up). Doublevalue ()) + "M"); } System.out.println ("Last Modified Time" +NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss. SSS "). Format (NewDate (Fl.lastmodified ())); }  }
/*** 1. List the information in the directory: public String [] list (); * 2. Return directory information through the File object: public file[] Listfiles ();*/ PackageCom.java.demo;ImportJava.io.File;ImportJava.math.BigDecimal;ImportJava.text.SimpleDateFormat;Importjava.util.Date; Public classTestdemo { Public Static voidMain (String args[])throwsexception{File FL=NewFile ("E:" +file.separator);//set the full path of the file        if(Fl.isdirectory ()) {String result[]=fl.list ();  for(intx=0;x<result.length;x++) {System.out.println (result[x]);//$RECYCLE. BIN}} File [] Reslut=Fl.listfiles ();  for(inty=0;y<reslut.length;y++) {System.out.println (reslut[y]);//e:\ $RECYCLE. BIN        }    }  }

IO operations in Java

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.