Input and output-total

Source: Internet
Author: User
Tags flushes object serialization

1.File class

The file class can use a path string of files to create a file instance, which can be either an absolute or a relative path, and by default the system always interprets the relative path based on the user's working path, which is specified by the system property "User.dir". This is usually the path where the Java virtual machine is running.
File cannot access the content itself. If you need to access the file content itself, you need to use the input/output stream.

----------------------------------------------------------------------------------------------

2. File Filter--filenamefilter interface

You can accept a filenamefilter parameter in the file's list method, which allows you to list only files that match the criteria.
The FilenameFilter interface contains an accept (File dir, String name) method that iterates through all subdirectories and subfolders of the specified File in turn, and if the method returns True, the list method lists the subdirectory or sub-file.

3.

Character Stream--reader\writer

BYTE Stream--inputstream\outputstream

Use their implementation classes when used, and the following common public method reader:intRead () Reads a single character.intReadChar[] cbuf) Reads characters to an array. Abstract intReadChar[] Cbuf,intOffintLen) Reads characters into a portion of an array. intRead (Charbuffer target) attempts to read characters into the specified character buffer.-----------------------------------------------------------------------------InputStream:Abstract intRead () Reads the nextbyteof data from the input stream.intReadbyte[] b) Reads some number of bytes from the input stream and stores them into the buffer array B.intReadbyte[] B,intOffintLen) Reads up to Len bytes of data from the input stream into an array of bytes.------------------------------------------------------------------Writerwriter Append (Charc) appends the specified character to Thiswriter. Writer Append (charsequence csq) appends the specified character sequence to Thiswriter. Writer Append (charsequence csq,intStartintend) Appends a subsequence of the specified character sequence to Thiswriter.Abstract voidClose () closes the stream, flushing it first.Abstract voidFlush () flushes the stream.voidWriteChar[] cbuf) Writes an array of characters. Abstract voidWriteChar[] Cbuf,intOffintLen) Writes a portion of an array of characters. voidWriteintc) writes a single character. voidwrite (String str) writes a String.voidWrite (String str,intOffintLen) Writes a portion of a string.------------------------------------------------------------------OutputStreamvoidClose () closes ThisOutput stream and releases any system resources associated with Thisstream.voidFlush () Flushes Thisoutput stream and forces any buffered the output bytes to is written out.voidWritebyte[] b) writes B.length bytes from the specifiedbyteArray to Thisoutput stream.voidWritebyte[] B,intOffintLen) writes Len bytes from the specifiedbyteArray starting at offset off to Thisoutput stream.Abstract voidWriteintb) writes the specifiedbyteTo ThisOutput stream.
View Code

-------------------------------------------------------------------------------------------------------------

4. Input/output stream system

Http://www.cnblogs.com/moonpool/p/5488463.html

Read and write text files
Read files with FileInputStream
Write files with FileOutputStream
Read a text file with BufferedReader
Write a text file with BufferedWriter
Read and write binary files
DataOutputStream
DataInputStream

--------------------------------------------------------------------------------------------------------------- -------

5. Convert Stream--inputstreamreader OutputStreamWriter

The conversion stream is used to convert the byte stream into a stream of characters, where inputstreamreader converts byte input streams into character input flows, OutputStreamWriter converts the byte output stream into a character output flow

--------------------------------------------------------------------------------------------------------------- ---------------------------------

6. Push Reflux--pushbackinputstream Pushbackreader

The following three methods are available:
void Unread (byte[]/char[] buf): Pushes the contents of a byte/character array back into the push buffer, allowing repeated reads of the content just read.
void Unread (byte[]/char[] B, int off, int len): Starts from off in a byte/character array, and the length of Len Byte/character is pushed back into the push buffer, allowing repeated reads of the content just read.
void unread (int b): pushes a byte/character into the push-back buffer, allowing repeated reads of the content just read.

--------------------------------------------------------------------------------------------------------------- ---------------------------

7. REDIRECT standard input/output system.in System.out

Standard input/Output methods:
static void Seterr (PrintStream err): Redirects the "standard" error output stream.
static void SetIn (InputStream in): Reassign the "standard" input stream.
static void SetOut (PrintStream out): Redirects the "standard" output stream.

------------------------------------------------------------------------------------------------

8.Java virtual machine reads data from other processes

The Exec method of the runtime object can be used to run other programs on the platform, which produces a process object that represents a child process that is initiated by the Java program.

The Process class provides the following three methods for the program to communicate with other child processes.

InputStream Geterrorstream (): Gets the error stream of the child process

InputStream getInputStream (): Gets the input stream of the child process

Oututstream Getoutputstream (): Gets the output stream of the child process

-----------------------------------------------------------------------------------------------

9.RandomAccessFile class

It provides a number of ways to access the contents of a file, which can either read the contents of a file or output data to a file. Unlike normal input/input streams, Randomaccessfile supports a "random access" approach that allows programs to jump directly to and read data anywhere in a file.

The Randomaccessfile object also contains a record pointer to identify the location of the current read and write, and when the program creates a new Randomaccessfile object, the file record pointer is located at the file header (that is, 0), and when n bytes are read/written, The file record pointer moves backwards by n bytes. In addition, the Randomaccessfile can move the record pointer freely, either forward or backward. Randomaccessfile contains the following two methods to manipulate file record pointers:
----Long Getfilepointer (): Returns the current position of the file record pointer.
----void Seek (Long POS): Positions the file record pointer to the POS location.
The Randomaccessfile class has two constructors, in fact, the two constructors are basically the same, it really is the format of the specified file is different: one uses the string parameter to specify the file name, and one that uses the filename parameter to specify itself. In addition to this, you need to specify a mode parameter when creating the Randomaccessfile object, which specifies the access mode of the Randomaccessfile.

-------------randomaccessfile (file file, String mode) creates a random access File stream to the read from, and optionally to Write to, the file specified by the file argument.
-------------randomaccessfile (string name, string mode) creates a random access file stream to the read from, and optionally t O Write to, a file with the specified name.

The parameter has the following four values:
----"R": Opens the specified file as read-only. If you attempt to execute a write method on the Randomaccessfile, the IOException will be thrown.
----"RW": Opens the specified file in read and write mode. If the file does not already exist, try to create the file.
----"RWS": Opens the specified file in read and write mode. In contrast to the "RW" mode, each update to the file's contents or metadata is also required to be written to the underlying storage device synchronously.
----"RWD": Opens the specified file in read and write mode. For "RW", each update to the file content is also required to be written to the underlying storage device synchronously.

Randomaccessfile still cannot insert content into the specified location of the file, and if the file record pointer is moved directly to a position in the middle to start the output, the content of the new output overwrites the contents of the file.
If you need to insert content into a specified location, the program needs to read the contents behind the insertion point into the buffer, and then append the contents of the buffer to the file after writing the data that needs to be inserted.

--------------------------------------------------------------------------------------------------------------- --------

10. Object serialization--serializable Interface

The serialization mechanism allows you to convert a serialized Java object into a sequence of bytes that can be saved on disk or transmitted over a network for later restoration to the original object. The serialization mechanism allows an object to exist independently from the running of the program.
The serialization of Objects (Serialize) refers to the writing of a Java object to the IO stream, which corresponds to the object's deserialization (deserialize), which is the restoration of the Java object from the IO stream.
If you need to have an object that can support the serialization mechanism, you must make its class serializable (serializable), in order for a class to be serializable, the class must implement one of the following two interfaces:
----Serializable
----externalizable

Here are two steps to serialize an object.
(1) Create a objectoutputstream, this output stream is a processing stream, so it must be built on the basis of other node flows.
(2) Call the WriteObject method of the ObjectOutputStream object to output the serializable object.

Deserialization steps

(1) Create a objectinputstream, this input stream is a processing stream, so it must be built on the basis of other node flows.
(2) The ReadObject object of the ObjectInputStream object is called to read the object in the stream, which returns a Java object of the type object and, if the program knows the type of the Java object, it can be cast to its true type.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Custom Serialization--transient Keywords

With the transient keyword in front of the property, you can specify that the property value is not ignored by Java serialization.
Classes that require special handling during serialization and deserialization should provide the following special signature methods, which are used to implement custom serialization:
private void WriteObject (Java.io.ObjectOutputStream out) throws IOException
private void ReadObject (Java.io.ObjectInputStream in) throws IOException, ClassNotFoundException;
private void Readobjectnodata () throws objectstreamexception;

---------------------------------------------------------------------

11.NIO

Buffer buffers

In terms of the internal structure, buffer is like an array, which can hold more than one type of data. Buffer is an abstract class whose most common subclass is Bytebuffer, which can perform get/set operations on the underlying byte array, in addition to Bytebuffer, Other basic data types (except for the Boolean) have corresponding buffer classes: Bytebuffer, Charbuffer, Shortbuffer, Intbuffer, Longbuffer, Floatbuffer, DoubleBuffer
Capacity (capacity): The capacity of the buffer (capacity) represents the maximum data capacity of this buffer, that is, how much data can be stored. The buffer's capacity cannot be negative and cannot be changed after it is created.
Limit: The first buffer position index that should not be read out or written. In other words, the data after limit cannot be read or written.
Position (position): Used to indicate the next index of buffer position that can be read out or written (similar to the record pointer in the IO stream). When you use buffer to read data from the channel, the value of position is exactly equal to how much data has been read. When a buffer object is newly created, its position is 0, and if 2 data is read from the channel into the buffer, postion is 2, pointing to the third (first position index 0) position in buffer.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Channel channels
The channel is similar to a traditional stream object, but unlike the traditional stream, the channel has two main differences:
The channel can directly map part or all of the specified file into buffer.
The program cannot directly access data in the channel, including reading and writing, and the channel can only interact with buffer. That is, if you want to get data from the channel, you must first remove some data from the channel with buffer, and then let the program remove the data from buffer, and if you want to write the data in the program to the channel, let the program put the data in buffer first. The program then writes the input in buffer into the channel.
Channel is an interface, located under the Java.nio.channels package, the system provides the interface Datagramchannel, FileChannel, Pipe.sinkchannel, Pipe.sourcechannel, Selectablechannel, Serversocketchannel, Socketchannel and other implementation classes, this section mainly introduces the use of FileChannel, According to these channel names, it is not difficult to find that the channel in the new Io is broken down by function, such as Pipe.sinkchannel, The Pipe.sourcechannel is used to support channel channels for communication between threads, while Serversocketchannel, Socketchannel is the channel used to support TCP network traffic.

Selector

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Charset

In general, converting a sequence of strings of plaintext into a sequence of bytes understood by a computer (a binary file that ordinary people cannot understand) becomes encoded, converting a sequence of bytes into a plaintext string that the average person can read is called decoding.
The following three methods are also available in CharSet:
Charbuffer decode (Bytebuffer BB): A convenient way to convert a sequence of bytes in Bytebuffer into a sequence of characters.
Bytebuffer encode (Charbuffer CB): A convenient way to convert a sequence of characters from Charbuffer into a sequence of bytes.
Bytebuffer encode (String str): A convenient way to convert a sequence of characters in a String into a sequence of bytes.

Filelock

-----------------------------------------------------------------------------

12.nio.2

Path\paths\files

Path-Interface, which represents a platform-independent directory. Provides a number of ways to manipulate the directory.
Paths-Tool class. All methods are static.
Files-the tool class for manipulating files. Provides a number of ways to manipulate files. This class contains a number of methods that may differ from our usual expectations.

Filevisitor

Watchservice

Java.nio.file.attribute--fileattributeview interface

Input and output-total

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.