3.Java file operation

Source: Internet
Author: User

    • Overview
    • 1.File class
      • Field Summary
      • Construction method
Overview
    • 1. File class
    • 2. Principles and concepts of IO flow
    • 3. System of IO Stream
    • 4. Byte stream and character stream
    • 5. Process Flow
    • 6. File copy
    • 7. File segmentation and Merging
1.File class

The file class is an abstract form of the path name of files and directories. A file object can represent a document or directory, but not exactly.
The file class object is primarily used to obtain some information about the files themselves. The aim is to: establish a connection between a Java program and a file (folder). So that the Java program operates on the file.

Field Summary

The following four fields are related to the system, primarily for dynamic writing paths, and are designed to be cross-platform.
(static string) pathseparator----path delimiter (string)
(static char) Pathseparatorchar----path delimiter (character)
(static string) separator----name delimiter (string)
(static char) Separatechar----name delimiter (character)

Construction method

Just make a connection and not check whether the file really exists

    1. File (file parent, String child)
      Relative path construction
      Creates a new File instance based on the parent abstract pathname and child pathname string.

    2. File (String pathname)
      Absolute path Construction
      Creates a new File instance by converting the given pathname string to an abstract path name.

    3. File (string parent, String child)
      Relative path construction
      Creates a new File instance based on the parent pathname string and the child pathname string.

    4. File (URI Uri)
      Absolute path Construction
      Creates a new file instance by converting the given File:uri to an abstract path name.

Package Filetest;import java.io.file;/** * For testing the file class about knowledge points * relative paths and absolute paths and absolute paths constructs a file object * @author Max * */public class Filedemo        {public static void main (string[] args) {String Parentpath = "user/xiejinhui/documents/";                String name = "style sheet. css"; /** * 1.         File (file parent, string child) * Creates a new File instance based on the parent abstract path name and children pathname string. * 2.         File (String pathname) * Creates a new file instance by converting the given pathname string to an abstract path name. * 3.         File (string parent, String child) * Creates a new File instance based on the parent pathname string and the children pathname string. * 4.         The file (Uri Uri) * Creates a new file instance by converting the given File:uri to an abstract pathname. *///relative path build, relative path relative to parent path//provide parent path first, then build own file under parent path//Just establish contact, do not check whether the file really exists files F1 = new file (paren Tpath,name);//The first way to build is to give the path F1 = new file (new file (Parentpath), name);//The second way to build is to give the object System.out.println ("-------        ----relative path-----------");        System.out.println ("File path:" +f1.getpath () + "\ n filename is called:" +f1.getname ()); AbsolutelyPath Build f1 = new File ("user/xiejinhui/documents/style sheet. css");        System.out.println ("-----------Absolute path-----------");        System.out.println ("File path:" +f1.getpath () + "\ n filename is called:" +f1.getname ());        When there is no drive letter, the workspace is the parent path F1 = new File ("style sheet. css"); System.out.println ("-----------is missing a drive letter-----------"), or//if a drive letter is missing, the default current System.out.println ("File path is:" +    F1.getabsolutepath () + "\ n filename is called:" +f1.getname ()); }}

3.Java file Operations

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.