Java. Io. File class learning notes

Source: Internet
Author: User

The file class is the packaging class of the file name and its directory path. The file class provides an abstraction for processing the complex file and path name problems that most platforms depend on in an independent way.

The file class contains many methods for obtaining file attributes and deleting and renaming files. However, the file class does not contain methods for reading and writing file content.

 

Model: When we take a file as a house, the object of the file class represents the exterior characteristics of the House, which can change the exterior color of the House, but cannot operate on the interior of the house.

.

 

You can use a file name as a parameter to create an object, or use a path as a parameter to create an object.

 

 

 

Four constants of the file class: to adapt to the running of different platforms, we usually do not have to write "/", ":" and other file separators and path separators in the program. The file class in Java provides four constants for us.

Field Abstract

static String

pathSeparator

System-related path separator, which is expressed as a string for convenience.

static char

pathSeparatorChar

System-related path separator.

static String

separator

Default name separator related to the system. For convenience, it is represented as a string.

static char

separatorChar

Default name separator related to the system.

 

Pathseparator is the path separator, ";" on the window, and ":" On Unix ":"

Separator is the name separator, "/" on the window, and "/" on Unix. In fact, "/" can also be used in Windows programs or "//"

 

What are the differences between pathseparator and pathseparatorchar, and separator and separatorchar? In fact, we can see from the above that one is string type and one is char type. separator is a string that represents separatorchar. The same pathseparator is a string that represents pathsepartorchar, there is little difference in Application

 

Constructor Summary

File

(File

 parent, String

 child)

According
Parent abstract path name and child path name string to create a newFile

Instance.

File

(String

 pathname)

Creates a newFile

Instance.

File

(String

 parent, String

 child)

According
Create a new parent path name string and child path name stringFile

Instance.

File

(URI

 uri)

ByFile:

Uri is converted to an abstract path to create a newFile

Instance.

 

Note: Do not directly use the absolute directory name and file name in the program. If "C: // book // test. dat, which can be run in windows but cannot be run in other systems. Use the following string to replace C: // book // test. dat"

New file ("."). GetCanonicalPath () + "book" + file. Separator + "test. dat)

File (Java 2 platform SE 6)

 

 

 

Related Article

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.