Introduction to Java file operations (File class)

Source: Internet
Author: User

Introduction to Java file operations (File class)

 

The user interface and operating system use the system-related path name string to name files and directories. This class presents an abstract, system-independent view of the hierarchical path. The abstract path name has two components:
An optional system-related prefix string, such as a drive letter, "/" indicates the root directory in UNIX, and "//" indicates the Microsoft Windows UNC path name, and a sequence of zero or more string names.
In addition to the last one, each name in the abstract path name represents a directory. The last name can represent both a directory and a file. An empty abstract path name has no prefix or name sequence.

The conversion between the path name string and the abstract path name is related to the system. When an abstract path name is converted to a path name string, each name and the next name are separated by a single default delimiter. The default name separator is defined by the system attribute file. separator. It can also be obtained from the common static fields separator and separatorchar of this class. When converting a path name string to an abstract path name, you can use the default name separator or any other name separator supported by the basic system to separate the names.

Whether it is an abstract or string path name, it can be an absolute or relative path name. The absolute path name is the complete path name. You can locate the file you represent without any other information. On the contrary, the relative path name must be interpreted using information from other path names. By default, classes in the Java. Io package always analyze relative path names based on the current user directory. This directory is specified by the system property user. dir, which is usually the call Directory of the Java Virtual Machine.

The prefix concept is used to process the root directory of the UNIX platform, as well as the drive letter, root directory, and UNC path name on the Microsoft Windows platform, as shown below:
1. for UNIX platforms, the prefix of absolute path names is always "/". The relative path name has no prefix. Indicates that the absolute path name of the root directory is prefixed with "/" and there is no name sequence.
2. for Microsoft Windows, the prefix of the path name containing the drive letter is composed of the drive name and a ":". If the path name is an absolute path name, it may be followed "//". The prefix of the UNC path name is "//". The host name and the shared name are the first two names in the name sequence. The relative path name of the drive is not specified and has no prefix.

The file class instance is unchangeable. That is to say, once created, the abstract path represented by the file object will never change.

Constructor:
File (File parent, string child)
Creates a new file instance based on the abstract path name and child path name string of the parent.
File (string pathname)
Create a new file instance by converting a given path name string to an abstract path name.
File (string parent, string child)
Create a new file instance based on the parent path name string and the Child path name string.
File (URI)
Create a new file instance by converting the given file: URI into an abstract path name.

For more information, seeThis API document.

The following is an example:
(Please create the writefile.txt file first. After the program runs, the backup directory will be generated under the same Directory, which contains the copy file and the package will be placed in biz.1cn. Stream)

Package biz.1cn. stream; import Java. io. file; import Java. io. fileinputstream; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. text. simpledateformat; import Java. util. date;/*** @ author chenrz (Simon) * @ date 2006-6-30 * <p> * Java file operations-update files (www.1cn. biz) * </P> */public class updatefile {public static void main (string [] ARGs) throws ioexception {// file name to be copied string filename = "writef Ile.txt "; // subdirectory name string childdir =" backup "; new updatefile (). update (filename, childdir);} public void Update (string filename, string childdir) throws ioexception {file F1, F2, child; // create the file object F1 = new file (filename); child = new file (childdir); If (f1.exists () {If (! Child. exists () child. mkdir (); // create the file object F2 = new file (child, filename) in the subdirectory childdir; If (! F2.exists () | f2.exists () & (f1.lastmodified ()> f2.lastmodified () Copy (F1, F2); getinfo (F1); getinfo (child );} else system. out. println (f1.getname () + "file not found! ");}/*** Copy the file F1 to F2 ** @ Param F1 * Source File * @ Param F2 * target file * @ throws ioexception */Public void copy (File F1, file F2) throws ioexception {// create a file input/output stream object fileinputstream is = new fileinputstream (F1); fileoutputstream OS = new fileoutputstream (F2 ); // set the number of bytes to be read, int count, n = 512; byte buffer [] = new byte [N]; // read the input stream COUNT = is. read (buffer, 0, n); While (count! =-1) {OS. write (buffer, 0, count); Count = is. read (buffer, 0, n);} // close the input/output stream is. close (); OS. close ();}/*** get the file or folder information: path, modification time ** @ Param file * @ throws ioexception */public static void getinfo (File file) throws ioexception {// initialization time format simpledateformat SDF = new simpledateformat ("yyyy-mm-dd hh: mm: SS"); If (file. isfile () // returns the absolute path, file length, and last modification time of the abstract path name system. out. println ("<File>/t" + file. getabsolutepath () + "/t" + SDF. format (new date (file. lastmodified (); else {system. out. println ("/t" + file. getabsolutepath (); file [] files = file. listfiles (); For (INT I = 0; I <files. length; I ++) getinfo (files [I]) ;}}
Package biz.1cn. stream; import Java. io. file; import Java. io. fileinputstream; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. text. simpledateformat; import Java. util. date;/*** @ author chenrz (Simon) * @ date 2006-6-30 * <p> * Java file operations-update files (www.1cn. biz) * </P> */public class updatefile {public static void main (string [] ARGs) throws ioexception {// file name to be copied string filename = "writef Ile.txt "; // subdirectory name string childdir =" backup "; new updatefile (). update (filename, childdir);} public void Update (string filename, string childdir) throws ioexception {file F1, F2, child; // create the file object F1 = new file (filename); child = new file (childdir); If (f1.exists () {If (! Child. exists () child. mkdir (); // create the file object F2 = new file (child, filename) in the subdirectory childdir; If (! F2.exists () | f2.exists () & (f1.lastmodified ()> f2.lastmodified () Copy (F1, F2); getinfo (F1); getinfo (child );} else system. out. println (f1.getname () + "file not found! ");}/*** Copy the file F1 to F2 ** @ Param F1 * Source File * @ Param F2 * target file * @ throws ioexception */Public void copy (File F1, file F2) throws ioexception {// create a file input/output stream object fileinputstream is = new fileinputstream (F1); fileoutputstream OS = new fileoutputstream (F2 ); // set the number of bytes to be read, int count, n = 512; byte buffer [] = new byte [N]; // read the input stream COUNT = is. read (buffer, 0, n); While (count! =-1) {OS. write (buffer, 0, count); Count = is. read (buffer, 0, n);} // close the input/output stream is. close (); OS. close ();}/*** get the file or folder information: path, modification time ** @ Param file * @ throws ioexception */public static void getinfo (File file) throws ioexception {// initialization time format simpledateformat SDF = new simpledateformat ("yyyy-mm-dd hh: mm: SS"); If (file. isfile () // returns the absolute path, file length, and last modification time of the abstract path name system. out. println ("<File>/t" + file. getabsolutepath () + "/t" + SDF. format (new date (file. lastmodified (); else {system. out. println ("/t" + file. getabsolutepath (); file [] files = file. listfiles (); For (INT I = 0; I <files. length; I ++) getinfo (files [I]) ;}}

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.