Java's File class

Source: Internet
Author: User

1.File class overview

If we want to implement the IO operation, we must know the representation of the file on the hard disk. And Java provides a file class for us to use.

File: An abstract representation of the path name of a document or directory (folder).


How to construct a 2.File class

File (string pathname) Gets the file object file (string parent,string child) based on a path to a file object file (file parent, based on a directory and a sub-directory). String child) Gets a file object based on the parent file object and a sub-file/directory
Package Cn;import Java.io.file;public class Filedemo {public static void main (string[] args) {//file (String pathname): According to a Path to get a file object//package e:/demo/a.txt into an object file File = new file ("E:/demo/a.txt");//file (String parent,string Child) : Get the file object file File2 = new file ("E:/demo", "a.txt") according to a directory and a sub-file/directory;//file (file Parent,string child): Based on a parent file object and a sub-file/ The directory gets the file object file File3 = new file ("E:/demo"); File File4 = new file (file3, "a.txt");}}


Member Methods for 3.File

Creating features

Package Cn;import Java.io.file;import java.io.ioexception;/** * File class creation function * public boolean createnewfile () Create files * Public A Boolean mkdir () creates a folder, and if such a folder exists, creates only one level of folder * Public boolean mkdirs () to create a folder, you can create a multi-tier folder */public class Filedemo {public S tatic void Main (string[] args) throws Exception {//Create a folder under the E-disk directory Demofile file = new file ("E:/demo"); System.out.println ("Create folder:" +file.mkdir ());//Create a file in the E-disk directory demo A.txtfile file1 = new File ("E:/demo/a.txt"); System.out.println (File1.createnewfile ());//Create a a.txt file under the E-disk directory test/aaa files file2 = new file ("E:/text/aaa"); System.out.println (File2.mkdirs ());}}

Remove Features

Package Cn;import java.io.file;/** * File class Delete function * public boolean delete () delete file or folder * Note: * If you create a file or folder forgot to write the drive path, then the default The target path.  * Java deletion does not go through the Recycle Bin * To delete a folder, the folder cannot include files or folders * */public class FileDemo2 {public static void main (string[] args) {File file = new File ("E:/demo/a.txt"); System.out.println (File.delete ()); File File2 = new file ("E:/text/aaa"); System.out.println (File2.delete ());}}























This article is from the "11831428" blog, please be sure to keep this source http://11841428.blog.51cto.com/11831428/1864659

Java's 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.