Java Learning Note IO file class

Source: Internet
Author: User
Tags file separator string format

File Class Summary

File class overview

Java.io.File class

An abstract representation of the file and directory path names.

The files and folders (directories) in the computer are encapsulated into a file object, and the files and folders can be manipulated by the method in the file object .

is a system-independent class in which any operating system can manipulate files and folders using methods in this class

3 words related to the file class:

File: files

Directory: folders, directories

Path: Paths

File Four static variables for a class
The static String PathSeparator the system-related path delimiter;

The static char Pathseparatorchar the system-related path separator;

The path separator is ":" In the UNIX system and " ;" in the Window System.

The static String separator The system-related default name delimiter.

STAITC Char separator The system-related default name delimiter.

The file separator is "/" in the UNIX system and is "\" in the Window System.

How to construct the file class:

File (String pathname); creates a new file instance by converting the given path string to an abstract path name .

String Pathname: Name of the path

Can be a path to a file, or a path to a folder

    Can be a relative path or an absolute path

    Can be a path that exists, or it can be nonexistent. The constructor method simply converts the path of a string format to an object, regardless of whether the path exists

  File (string parent, string child); creates a new file object based on the parent path name string and the child path name string , dividing the path into two parts, the parent path and the sub-path

    • Benefits:
    • More flexible to use, sub-paths and parent paths can pass different paths, forming a file object
    • Note: The path is case insensitive

  File (file parent, String child); Creates a new file object based on the parent abstract pathname and child pathname string

    • Benefits:
    • The file type used by the parent path can be called by a method in the file class to manipulate the path

File class creation and deletion capabilities

Boolean createnewfile () to create the file

    • The path and file name of the created file, given in the constructor method
    • Return Value: Boolean
    • True: Creation succeeded
    • False: File already exists, no longer created
    • Note: The file path given in the construction method must exist, otherwise an exception will be thrown

  Boolean mkdir () to create a single-level folder

  Boolean mkdirs () to create a multilevel folder

    • Only folders can be created and files cannot be created
    • return value:
      True: Creation succeeded

        False: folder already exists

    • Note: The file path given in the construction method must exist, otherwise an exception will be thrown

  A Boolean delete () deletes the file or directory represented by this abstract path name

    • return value:
    • True: Delete succeeded
    • False: File/ folder does not exist
    • Note: This method removes the Recycle Bin and deletes it directly on the hard drive.

the method of judging the File class:

Boolean isfile (); is that a file?

Boolean isdirectory (); determine if the encapsulated path is not a folder

Boolean exists (); determines whether the path encapsulated by the file construction method exists

how to get the File class:

File getabsolutefile (); Returns the absolute pathname form of this abstract path name

String GetAbsolutePath (); Returns the absolute pathname string for an abstract path name

String getName (); Gets the name of the last part of the path

String GetPath (); Gets the path in the constructor method, converting the path to a string

String getParent (); returns a string object

File getparentfile (); Returns the file object . Gets the parent path that returns the parent path at the end of the file

Long length (); Returns the number of bytes of the file path represented in the path, the folder has no size

The ability of the file class to traverse the directory

    • 1.string[] list (); Gets the name of each file/folder in the directory and saves it in a string array
    • 2.file[] listfiles () Gets the file object for each of the files/folders in the directory , and saves it in the file array
    • Attention:
    • The directory passed in the construction method must exist, or a null pointer exception will be thrown
    • A directory must be passed in the construction method, not a file, only a directory can be traversed, or a null pointer exception will be thrown
    • This method can get hidden files or hidden folders
    • Static file[] listroots (); List all the following directories
Recursive

Recursion:

Method itself to invoke itself;

Function: When invoking a method, the body of the method does not change, but each time the parameter of the method is called, it can be used recursively

Recursion is divided into two types: direct recursion and indirect recursion.

Direct recursion: The method itself calls itself

Indirect recursion: Can be a call B,B then call a

Attention:

      1. Recursion must be conditional to ensure that recursion can stop, or a stack memory overflow occurs
      2. There is a qualification in recursion, but the number of recursion cannot be too much, otherwise the stack memory overflow will occur.
      3. Construction method, no recursion, compile error
    1.   

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