IO stream – text \ read / write binary data

Text IO

1、 Brief description

The outputstreamwriter class uses the selected encoding method to convert the Unicode character stream into a byte stream, and the inputstreamreader class flows the input containing bytes into a reader that can produce Unicode characters.

Example:

(1) Inputstreamreader in = new inputstreamreader (system. In) allows an input reader to read input from the console and convert it to Unicode

(2)InputStreamReader in = new InputStreamReader(new FileInputStream(“test.txt”), “ISO08859_5”); Example (1) the input stream reader will use the default character encoding format of the host system, and this example shows that the encoding format is specified through the controller

(3)FileWriter out = new FileWriter(“test.txt”); File writer

II

1. Write text output

Printwriter: this class has methods to print strings and numbers in text format

PrintWriter out = new PrintWriter(“test.txt”); Equivalent to:... = new printwriter (New filewriter ("test. TXT");

String name = “test”;

double var = 10000;

out. print(name);

out. println(var);

The above example will write test 10000 to the writer out, and then these characters will be converted into bytes and finally written to test Txt

You can set whether the buffer is automatically emptied by constructing the method printwriter (writer out, Boolean autoflush). When autoflush is set to true, the buffer will be emptied as long as println is called. Example:

PrintWriter out = new PrintWriter(new FIleWriter(“test.txt”),true);

API:java. io. PrintWriter

(1)PrintWriter(Writer out)

(2)PrintWriter(Writer out,Boolean autoFlush)

Create a new printwriter

Parameter: out: a writer for character output

Autoflush: automatically empty

(3)PrintWriter(OutputStream out)

(4)PrintWriter(OutputStream out,boolean autoflush)

Create a new printwriter from an existing OutputStream by creating the required mediation outputstreamwriter.

(5)PrintWriter(String filename)

(6)PrintWriter(File file)

Create a new printwriter that writes to a given file by creating the necessary mediation filewriter.

(7) Void printf (string format, object... Args) prints the given value in the manner specified by the format string

(8) Boolean checkerror() returns true if a format or output error is generated. Once the stream encounters an error, it is infected, and all calls to checkerror will return true.

2. Read in text input

You can use scanner. Before 1.5, the only way to process text is to use BufferedReader. This class has the method readLine, which can read in a line of text and return null when there is no input, for example:

BufferedReader in = new BufferedReader(new FileReader(“test.txt”));

String line;

While((line = in.readLine()) != null){

Pass..

}

Note: BufferedReader has no method for reading in numbers

Reading and writing binary data

I

1. Datainputstream implements the datainput interface. To read binary data from a file, you need to combine datainputstream with byte source. For example, FileInputStream:

DataInputStream in = new DataInputStream(new FileInputStream(“test.txt”));

DataOutputStream:

DataOutputStream = new DataOutputStream(new FileOutputStream(“test.txt”));

2、API: DataInput:

readBoolean()、readByte()、readChar()、readDouble()、readFloat()、readInt()、…….

(1) Void readfull (byte [] b) reads bytes into array B and blocks them until all bytes are read.

(2) Void readfull (byte [] B, int off, int len) reads bytes into array B and blocks them until all bytes are read.

Parameter: B buffer for data reading

Off the offset of the start position of the data

Len maximum number of bytes read in

(3) String readutf(): reads a string composed of characters in "revised UTF-8" format.

DataOutput:

Void writeDouble(double d )……

(1) Void writechars (string s): write out all characters in the string.

(2) Void writeutf (string s): write out a string composed of characters in "revised UTF-8" format.

3、:

Random access files:

RandomAccessFile class can find or write data anywhere in the file. It can open a random access file for reading only or for reading and writing at the same time

RandomAccessFile in = new RandomAccessFile(“test.txt”,“r”);

RandomAccessFile inOut = new RandomAccessFile(“test.txt”,“rw”); When an existing file is opened as RandomAccessFile, the file will not be deleted

API:java. io. RandomAccessFile

(1)RandomAccessFile(String file,String mode)

RandomAccessFile(File file,String mode)

Parameter: file file to open

Mode "R" indicates read-only mode; "RW" indicates read / write mode; "RWS" refers to the read / write mode that synchronizes the disk operations of data and metadata during each update; "RWD" refers to the read / write mode in which only the write operations of data to the disk are synchronized during each update

(2) Long getfilepointer(): returns the current position of the file pointer

(3) Void seek (long POS): set the file pointer from the beginning of the file to POS bytes.

(4) Long length() returns the length of the file measured in bytes.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>