Detailed Java file and directory management and input and output related operations _java

Source: Internet
Author: User

File and directory Management in Java

A directory is a special mechanism for managing files, and keeping the same files in the same directory can simplify file management and improve productivity. The Java language defines a file class in the Java.io package specifically for managing disk files and directories.

Each file class object represents a disk file or directory, and its object properties contain information about the file or directory. By invoking the various methods provided by the file class, you can create, delete, duplicate name files, determine file read and write permissions, and exist, set up and inquire about the latest modification times of files. Different operating systems have different file system organizations, and by using the File class object, Java programs can process files and directories in a unified, platform-independent manner.
To create an object of the File class

To create a file class object, you need to give its corresponding filename or directory name, and the file class is constructed as shown in the table.

When you use the construction method of the File class, you need to be aware of the following points:
(1) The path parameter can be an absolute path, a relative path, or a directory on a disk.
(2) Because different operating systems use different directory delimiters, you can use a static variable system.dirsep of the system class to implement paths that are common to different operating systems. Such as:

  "D:" +system.dirsep+ "Myjava" +system.dirsep+ "file"


Get Properties and actions

With the file object, you can get property information for files and related directories and manage and manipulate them. Table 10-10 lists the methods and descriptions that are commonly used.

The "example" determines that the absolute path entered is representative of a file or a directory. If the file outputs the absolute path of this file and determines the file attributes (whether read or write or hidden) of the file, and if the directory outputs all the files in the directory (excluding hidden files) (view source code).

The results of the run are as shown in the figure:

Basic concepts of Java input and output (IO) and streams

Input output (I/O) is an operation that the program interacts with an external device or other computer. Almost all programs have input and output operations, such as reading data from a keyboard, reading data from a local or network file, or writing data. Through input and output operations can receive information from the outside, or to pass information to the outside world. Java takes these input and output operations with a stream to represent them through a unified interface, making the program design simpler.
The concept of Java streaming

Stream is the flow of data between parts in a computer's input-output operation. According to the data transmission direction, the flow can be divided into input stream and output stream. The data in the stream sequence in the Java language can be raw binary data, or data that conforms to a particular format after a certain encoding.

1. Input output stream
In Java, different types of input and output sources are abstracted as streams, where the input and output data is called the data stream. A data flow is a channel through which Java programs send and receive data, including input streams and output streams. Typically, an input stream is used in an application to read the data, and the output stream writes the data. The characteristic of streaming input and output is that the data is obtained and sent along the sequence of data sequences. In contrast to a program, an output stream writes data to a storage medium or data channel, and an input stream reads data from a storage medium or a data channel, with the following characteristics in general about the flow:
FIFO, the first data written to the output stream is first read by the input stream.
Sequential access, one after another to the stream to write a string of bytes, read out will also read the sequence of bytes in writing, can not randomly access the middle of the data.
Read-only or write-only, each stream can only be an input stream or an output stream, can not have two functions at the same time, in a data transmission channel, if both write data, but also read data, to provide two streams respectively.

2. Buffer stream
In order to improve the efficiency of data transmission, the concept of buffer stream (buffered stream) is introduced, that is, a buffer is provided for a stream, and a buffer is a piece of memory dedicated to transferring data.

When writing data to a buffer stream, the system sends the data to the buffer instead of sending it directly to the external device. The buffer automatically records data, and when the buffer is full, the system sends all the data to the appropriate external device. When reading data from a buffer stream, the system actually reads the data from the buffer, and when the buffer is empty, the system automatically reads the data from the relevant external device and reads as much data as possible to fill the buffer. The purpose of using data flow to process input and output is to make the input and output operation of the program independent of the related device. Because the program does not need to pay attention to the details of the implementation of the specific equipment (specific details by the system), so for a variety of input, as long as the flow of processing can be, do not need to modify the source program, thereby enhancing the portability of the program.
I/O Flow class overview

For ease of flow processing, the Java language provides a java.io package in which each class represents a specific input or output stream. In order to use these streaming classes, it is necessary to introduce this package in programming. Java provides two types of input-output streams: a byte-oriented stream, the processing of data in bytes, and a character-oriented stream for character data processing. byte flow (byte stream) reads and writes a 8-bit binary number at a time, also known as a binary byte throttle or a bit stream. The character stream reads and writes a 16-bit binary number at a time, and it is processed as a character instead of a bits. It is to be noted that the Java language character encoding uses a 16-bit Unicode code to satisfy the internationalization of the character, whereas a normal text file uses a 8-bit Ascⅱ code.

The hierarchy of classes in Java.io is shown in the figure.

For a number of frequent device interactions, the Java language system has ordered 3 streaming objects that can be used directly, respectively:

    • System.in (standard input), usually representing keyboard input.
    • System.out (standard output): usually written to the monitor.
    • System.err (standard error output): Usually written to the monitor.

The steps used in the Java language to use a byte stream and a character stream are basically the same, taking an input flow as an example, first creating a Stream object related to the data source, then using the method of the Stream object to enter data from the stream, and then the close () method closes the stream.

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.