Java Learning's File class understanding

Source: Internet
Author: User

The file class is the only object in the IO package that represents the disk file itself. The file class defines a platform-independent way to manipulate files by invoking methods in the file class to create, delete, rename, and so on. The object of the file class is primarily used to obtain information about the files themselves, such as the directory where the file resides, the length of the file, and the file read and write permissions. Data streams can write data to a file, which is also the most commonly used data medium for data flow.

1. Creation and deletion of files

You can use the file class to create a Document object, which is constructed as a method:

(1) File (String pathname)

The construction method creates a new file instance by converting the given path moniker string to an abstract path.

New File (String pathname)

Pathname: Refers to the path name (including the file name)

File File = new file ("D:/test.txt") or file file =new file ("D:\\test.txt")

(2) File (string parent, String child)

The construction method creates a new file instance based on the defined parent path and the subpath string, which contains the file name.

(3) file (file F, String Child)

The construction method creates a new file instance based on the parent abstract pathname and the child pathname string.

To create a file object using the Files class: File File = new file ("D:/myword", "word.txt");

If there is no file in the D:/myword directory with the name Word, the file class object can be created by calling the CreateNewFile () method, and if the Word.txt file exists, the file object's Delete () method to delete it.

Import Java.io.File;
Import java.io.IOException;

public class Filetest {
public static void Main (string[] args) {
File File = new file ("D:\\test", "test6.txt");
if (file.exists ()) {
File.delete ();
System.out.println ("The file has been deleted! ");
}else {
try {
File.createnewfile ();
System.out.println ("The file has been created! ");
} catch (IOException e) {

E.printstacktrace ();
}
}
}
}

2. Get file information

The file class provides a lot of information for getting the files themselves, common methods:

Common methods of File class
Method Describe
String GetName () Gets the name of the file
Boolean CanRead () Determine if the file is readable
Boolean canWrite () Determine if the file can be written
Boolean exits () Determine if the file length exists
int Length () Gets the length of the file (in bytes)
String GetAbsolutePath () Gets the absolute path of the file
String getParent () Gets the parent path of the file
Boolean isfile () Determines whether the file represented by this abstract path name is a normal file
Boolean isdirectory () Determines whether this abstract path name represents a directory
Boolean Ishidden Determine if a file is a hidden file
Long LastModified () Get file Last modified time
Boolean CanExecute () Tests whether the application can execute the file represented by this abstract path name.
Boolean CreateNewFile () When and only if a file with that name does not already exist, Atomically creates a new empty file that is named after the abstract path name.
Boolean Delete () Deletes the file or directory represented by this abstract path name.
file[] listfiles () Returns an abstract path to the an array group that represents the files in the directory represented by the abstract path name.
string[] list () Returns an array of strings that name the files and directories in the directory represented by this abstract path name.
Boolean mkdirs () Creates a directory named after this abstract path name, including any parent directories that are required but not present. Can create multi-tier packages
Boolean mkdir () Creates a directory named after this abstract path name. Only one layer of packages can be created
Boolean Renameto (File dest) Renames the file represented by this abstract path name.
Boolean setreadonly () Marks a file or directory named by this abstract pathname so that only read operations are allowed.
Boolean setwritable (Boolean writable) A convenient way to set the owner's write permission to this abstract path name.

Java Learning's File class understanding

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.