S2/java/10-file I/O

Source: Internet
Author: User

The Java.io package provides interfaces and classes to perform basic operations on files, including operations on file and directory attributes, operations on file reads and writes, and so on.

the file object can represent files or directories.

The syntax format for creating a File object is as follows:

File File=new file (String pathName);

Where pathName represents the path name of the file to which it is pointing.

For example,"file File=new file (" C:\\Test.txt "); created an object that points to the Test.txt text file under the C packing directory.

It is important to note that in the Windows operating system, the delimiter in the file pathname can use a forward slash "/", such as "C:/test.txt" or a backslash "\", but must be written as " \ \ ", where the first represents an escape character, such as " C:\\Test.txt ".

The file object is the only object in the Java.io package that references the disk file. The file class simply describes the properties of the file object and does not describe how the data is stored. The file class provides some important ways to manage the properties of files or directories, as shown in the following table:

common methods for File classes

To determine if a file or directory exists

td>

Method name

Description

boolean exists ()

boolean isfile ()

Determine if it is a file

boolean isdirectory ()

Determine if it is a directory

string GetPath ()

Returns the relative path name of the file represented by this object

string GetAbsolutePath ()

Returns the absolute path name of the file represented by this object

string getName ()

Returns the name of the file or directory represented by this object

boolean Delete ()

Delete the file or directory specified by this object

boolean createnewfile ()

Create empty file with name, do not create folder

long length ()

Returns the length of the file, in bytes, if the file does not exist, returns 0L

Reading a file refers to reading the data in the file into memory. Conversely, writes the data in memory to the file when the file is written. The stream to read and write files, streams, refers to a series of flowing characters, is the first-in-one way to send and receive data channels.

When writing data to a stream, this stream is called the output stream, and the output stream can send information to the outside of the program. Data can be read from an output stream.

In the java.io package, the API for many input / output streams is encapsulated . In programs, the objects of these input / output stream classes are called Stream objects. These stream objects can be used to stream in-memory data to a file, or stream objects to read the data in the file to memory in a streaming manner.

Stream objects are often constructed with data sources (such as files) associated with them. The data source is divided into the source data source and the target data source. The input stream is contacted by the source data source, and the output stream is associated with the destination data source.

(1) according to different classification methods, the flow can be divided into different types.

1. Input stream: Data can only be read from and not written to.

2. Output stream: Data can only be written to, not read from.

For example, data from memory to hard disk, usually we call the output stream. In other words, the input and output here are divided from the memory angle in which the program is running.

the output stream of Java mainly consists of OutputStream and Writer as the base class, while the input stream is mainly composed of InputStream and Reader as base class.

(2) According to the data unit of the operation, the stream can be divided into a byte stream and a character stream.

The minimum data unit for a byte stream operation is 8 bytes, while the smallest data unit of a character stream operation is a character of the range.

The distinction between a byte stream and a character stream is very simple, the byte stream is recommended for binary data (slices), and the character stream is used for text, and their usage is almost identical.

According to the flow direction, we can also continue to divide the byte stream and the character stream, to separate bytes input stream, byte output stream, character input stream, character output stream.

    • Byte input stream InputStream class

the function of byte input stream InputStream is to input the data in the file into internal memory (called memory), which provides a series of methods related to reading data, the common method is as follows:

Common ways to read data

Method name

Description

int read ()

Read a byte of data

int read (byte[] b)

Reading data into a byte array

int read (byte[] b,int off,int len)

Reads bytes up to Len length from the input stream , saved in byte array b , and the saved position starts from off

void Close ()

Close the input stream

int available

Returns the estimated number of bytes read by the input stream

The non-parametric read () method reads 1 8-bit bytes from the input stream , converting it to an integer returned between 0~255.

The two read () method with the parameter reads a number of bytes from the input stream in bulk. Read (byte[] b) or read (byte[] b,int off,int len) methods can reduce the number of physical read files or keyboards when reading data from a file or keyboard, increasing input/ Output operation efficiency.

    • Direct input stream FileInputStream class

In the practical application, we usually use the subclass FileInputStream Class of InputStream to realize the reading of the content of the text file, the common construction method has the following two.

1,fileinputstream (file file).

Where file specifies the data source for the files. Use this construction method to create a file input stream object as follows.

File File=new file ("C:\\Test.txt");

InputStream fileobject=new fileinputstream (file);

At this point, the file input stream object FileObject is associated with the source data source (c:\\test.txt file).

2,FileInputStream (String name).

Wherename specifies the file data source, which contains the path information. Use this construction method to create a file input stream object as follows.

(File) InputStream in=new FileInputStream ("C:\\Test.txt");

3. Use fileinputstream to read the text file using the following steps.

(1) introduce the relevant classes.

Import java.io.IOException;

Import Java.io.InputStream;

Import Java.io.FileInputStream;

(2) Create a file input stream object.

InputStream fileobject=new FileInputStream ("C:\\Test.txt");

(3) Use the file input stream method to read the text file data.

Fileobejct.available (); number of bytes that can be read

Fileobject.read (); reading data from a file

(4) Close the file input stream object.

Fileobject.close ()

S2/java/10-file I/O

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.