Java file class-related knowledge

Source: Internet
Author: User

Java 's File

Directory:

1. How to create files or directories of file ... .

2. Three forms of the constructor that creates the File object ...

3. Knowledge of the separator .... ... ..........

4.file Simple Api introduction ........ ................. ..

1. When you say file, you have to talk about the three ways to create a file and directory.

<1>createnewfile () must have a path already created in order to create a file. Return value: Returns TRUE if the specified file does not exist and is successfully created, or false If the specified file already exists

<2>mkdir () creates a folder only in a directory that already exists.  
<3>mkdirs () can create a folder in a directory that does not exist. such as:a\\b, You can create a multilevel directory.

2. There are three types of constructors for creating File objects

A.file (String directorypath)

B.file (string directorypath, string filename)

C.file (File dirobj, String filename)

   in a There's nothing to say but a path, (1) if file createnewfile () ;(2) Span style= "font-family: the song Body;" If you call mkdir () mkdirs ()

The constructor in B is the first argument, the same as the first one, but the last parameter,(1) when called with the file object CreateNewFile () , the second parameter represents the name of the file. (2) if the second parameter "JAVAIO\\KC" is written in the form of a call to call mkdir () or mkdirs () You will create two subfolders,Javaio folders and the KC folder on the same set in the top level directory .

3. related knowledge about separators

Path delimiter: UnderWindows is " \" under Unix|linux is "/"

Considering the portability of the program, it is recommended that you use "/" when creating a file, as it is used under Windows Platform When you are testing java file creation "/" is also possible, Java seems to have been dealt with in the background.

There is another use:

Replace them with File.separator, and this will never be a portability issue.

Here are some examples to deepen your understanding:

File F = new file ("D://BOOK//1"); √

File F = new file ("D:/BOOK/1"); √

File F = new file ("d:\\book\\1"); √

File F = new file ("D:\book\1"); Χ

Note: \ is an escape character, so \ \ is equivalent to \.

Summary: We recommend that you use "/" or "File.separator".

Introduction to the 4.file simple Api

Here are two pieces of code to write:

1 //Create a file object for a directory2 3File file1=NewFile ("E:/javatest/java");4 5 if(!file1.exists ()) {6 7File1.mkdirs ();//Create a directory8 9}Else {Ten  OneSYSTEM.OUT.PRINTLN ("created directory already exists"); A  - }  -  the   -  -   -  + //Create a file -  +File file2=NewFile ("E:/javatest/java", "Java.txt"); A  at if(!file2.exists ()) { -  - Try { -  - file2.createnewfile (); -  in}Catch(IOException e) { -  to //TODO auto-generated Catch block +  - e.printstacktrace (); the  * } $ Panax Notoginseng}Else { -  theSYSTEM.OUT.PRINTLN ("created file already exists"); +  A } the  + //is the path true, whether the file is False -  $System.out.println ("is path" +file1.isdirectory ()); $  -System.out.println ("is file" +file1.isfile ()); -  the //is the path false, if true for the file - WuyiSystem.out.println ("is path" +file2.isdirectory ()); the  -System.out.println ("is file" +file2.isfile ()); Wu  -       //Common APIs About  $ //A. Direct Output Object -  -System.out.println (FILE1);//The result of the output is E:\javatest\java, which is equivalent to the file1.tostring () method -  ASystem.out.println (file2);//The result of the output is E:\javatest\java\java.txt +  the //B. Output absolute Path -  $System.out.println (File1.getabsolutepath ());//E:\javatest\java the  theSystem.out.println (File2.getabsolutepath ());//E:\javatest\java\java.txt the  the //C. Get the name of the file -  inSystem.out.println (File1.getname ());//directory Java with output as the last level the  theSystem.out.println (File2.getname ());//The result of the output is the name of the file Java.txt About  the //D. Get Parent Directory the  theSystem.out.println (File1.getparent ());//E:\javatest +  -System.out.println (File2.getparent ());//E:\javatest\java the Bayi //Effects and get the same effect as the parent directory the  theSystem.out.println (File1.getparentfile (). GetAbsolutePath ());

Java file class-related knowledge

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.