Java IO Learning Note: File class

Source: Internet
Author: User

in the entire IO package, the only class that represents the file itself is the files class. You can use the file class to make 1 common operations such as creating or deleting files. You want to use the file class. The first thing to do is to observe the method of constructing the file class, which is commonly constructed as follows: When the public file (String pathname) instantiates the file class, the path must be set .
No. Method or constant Type Describe
1 public static final String PathSeparator Constant Represents the delimiter for the path (Windows is ";" )
2 public static final String separator Constant Represents the delimiter for the path (Windows is "\")
3 Public File (String pathname) Structure Creating a File class object, passing in the full path
4 public Boolean CreateNewFile () throws IOException Ordinary Create a new file
5 public Boolean Delete () Ordinary deleting files
6 public Boolean exists () Ordinary Determine if a file exists
7 public boolean isdirectory () Ordinary Determines whether a given path is in a directory
8 public long Length () Ordinary Returns the size of a file
9 Public string[] List () Ordinary Lists the entire contents of the specified directory, just the name
10 Public file[] Listfiles () Ordinary List all contents of the specified directory, there will be paths
11 Public Boolean mkdir () Ordinary Create a Directory
12 Public Boolean Renameto (File dest) Ordinary Renaming an existing file
Instance Action one: Create a new file Create a Test.txt file on the D drive
Import Java.io.file;import Java.io.ioexception;public class Test1 {public    static void Main (string[] args) {        File F=new file ("D:" +file.separator+ "test.txt");//For increased portability, we recommend using File.separator        try {            f.createnewfile ();        } catch (IOException e) {            e.printstacktrace ();}}    }
instance Operation two: delete a specified file delete the Test.txt file just created
Import Java.io.file;public class Test2 {public    static void Main (string[] args) {        file F=new file ("D:" +file.sep arator+ "Test.txt");        if (f.exists ()) {//To determine that the file does not exist, do not delete the            f.delete () if it does not exist;}}}    

  

   instance Operation three: synthetic create, delete file operation given a path, if this file exists, then delete, if not present, create
Import Java.io.file;import Java.io.ioexception;public class Test3 {public    static void Main (string[] args) {        File F=new file ("D:" +file.separator+ "test.txt");        if (f.exists ()) {            f.delete ();        } else{            try {                f.createnewfile ();            } catch (IOException e) {                e.printstacktrace ();    }}}}
   instance Operation four: Create a folder use the MkDir () method to create a folder
Import Java.io.file;public class Test4 {public    static void Main (string[] args) {        file F=new file ("D:" +file.sep arator+ "test");        F.mkdir ();//Create Folder    }}
   instance Action five: List all files for a specified directory if you now have a directory, you can list the contents of the directory directly. However, there are two methods listed in the File class:
    • Returned as an array of strings: public string[] List ()
    • Returned as a File array: public file[] Listfiles ()
Action One: List all content using list ()
Import Java.io.file;public class Test5 {public    static void Main (string[] args) {        file F=new file ("D:" +file.separ Ator);        String[] Str=f.list ();        for (String s:str) {            System.out.println (s);}}    }
   all names are listed above, including the name of the folder and the name of the file. action Two: Use Listfiles () to list
Import Java.io.file;public class Test6 {public    static void Main (string[] args) {        file F=new file ("D:" +file.separ Ator);        File[] Files=f.listfiles ();        for (file file:files) {            System.out.println (file);}}    }
   the full path is listed above. Instance Operation VI: Determine whether a given path is a directory given path, determine whether it is a directory
Import Java.io.file;public class Test7 {public    static void Main (string[] args) {        file F=new file ("D:" +file.sep Arator);        if (F.isdirectory ()) {            System.out.println (F.getpath () + "is Directory");        } else{            System.out.println (F.getpath () + "not Directory");}}}    

Java IO Learning Note: 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.