Java IO-----The use of the file class for Java (13)

Source: Internet
Author: User


The use of the file class in the JAVAI/O section of the Java---(note)

In Java, I/O (input/output) is a tedious thing, because I do not see the obvious effect, but the input/output is a necessary part of all programs-using the input mechanism, allowing the program to read external data (including data from disk, CD and other storage devices), user input data, using the output mechanism, Allows the program to record the running state and output the program data to a storage device such as disk, CD-ROM, etc.

Java I/O through the java.io package of classes and interfaces to support, under the Java.io package mainly includes input, output two IO streams, each input, output stream can be divided into two categories of byte stream and character stream. where the byte stream handles the input and output operations in bytes, and the character stream processes the input and output operations with characters.

The IO stream in Java uses an adorner design pattern that divides the IO stream into the underlying node stream and the upper processing stream, where the node stream is used to correlate directly to the underlying physical storage nodes-there may be some differences in the way that different physical nodes acquire the node streams. However, the program can wrap different physical node streams into a unified processing stream, allowing the program to use uniform input. Output code to read the resources of different physical storage nodes.

The following is mainly through a class to record this part of the knowledge point, the specific code is as follows:

Package Com.gc.test;import java.io.file;import java.util.date;/** * * @author Android General * *//* * File class is a java.io packet that represents platform-independent text and directories, that is, if you want to manipulate files and directories in your program, you can do so by using the file * class. It is worth pointing out that both files and directories are manipulated using file, file can create new, delete, rename files and directories, and file * Cannot access the contents of the files themselves. If you need to access the file content itself, you need to use the input/output stream. * Once the file object has been created, it can be accessed by invoking the method of the file object, which provides many ways to manipulate files and directories.  */public class Filestudy {private static file file;/** * Incoming files or directories in the specified directory, get details of the object * @param URI of the passed-in file path or file directory */public static void Createsysout (String uri) {file=new file (URI), if (File.isfile ())//Determines whether the file object corresponds to files, not directory {// Returns the file name represented by this file object System.out.println ("file name:" +file.getname ());//Returns the path name of this file object System.out.println ("File path:" + File.getpath ());//Returns the file object that corresponds to the absolute path of this file object System.out.println ("The name of the filename for the absolute path to which it corresponds:" +file.getabsolutefile ( ). GetName ());//returns the absolute pathname corresponding to this file object. System.out.println (File.getabsolutepath ());//Returns the parent directory name of the directory (the last level subdirectory) of this file object. System.out.println (File.getparent ()) ,//Rename the file or directory corresponding to this file object, return True if rename succeeds, otherwise return false//system.out.println (File.renameto ("TestNewName.txt") );//rename, the file will not have the corresponding files. Determine whether the file or directory corresponding to the files object exists System.out.println ("The file exists:" +file.exists ());//Determines whether the file and directory corresponding to the files object is writable System.out.println (" Whether the file is writable: "+file.canwrite ()");//Determines whether the file and directory corresponding to the files object is readable System.out.println ("The file is readable:" +file.canread ());// Determines whether the file object corresponds to a directory, not a file System.out.println ("whether the object is a directory:" +file.isdirectory ()), or whether the files or directories that correspond to the document object are absolute paths. This method eliminates the differences between different platforms and can directly determine if the file object is an absolute path. On systems such as UNIX/LINUX/BSD, if the path name starts with a slash (/), it indicates that the file object corresponds to an absolute path, and on systems such as windows, it is an absolute path if the path begins//is a drive letter. System.out.println ("is absolute path:" +file.isabsolute ()); System.out.println ("Last Modified time for file:" +new Date (File.lastmodified ()). ToString ()); System.out.println ("Length of File content:" +file.length ());}}}


Ps: Most of the text is excerpted from the crazy Java handout, which only records the knowledge points of the File class section.

Reprint please specify the source of this article: http://blog.csdn.net/android_jiangjun/article/details/41683915

Java IO-----The use of the file class for Java (13)

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.