Character input file output stream, the stream buffer, a random stream

Because the byte stream can not operate well in itself a large capacity per unit data (equal to a multiple byte character), the character stream is required.

A method using the character input, the output stream
1. The input streams:
. 1) the FileReader (String filename);
2) the FileReader (File filename);

Then Read (); method.

2. The output stream:
. 1) FileWriter (String filename);
2) FileWriter (File filename);
. 3) FileWriter (String filename, append Boolean);
. 4) FileWriter (File filename, append Boolean);
If the append is true, then bytes are written at the end of the file, the equivalent of the additional information, written at the beginning and vice versa.

Then use Write (); method.

Examples are as follows:

import java.io.*;
public class Example{
public static void main(String args[]){
File sourceFile = new File("a.txt");              //读取的文件
File targetFile = new File("b.txt");              //写入的文件
char c[] = new char[19];                          // char型数组
try{
 Writer out = new FileWriter(targetFile,true);   //指向目的地的输出
 Reader in = new FileReader(sourceFile);         //指向源的输入流
 int n= -1;
 while((n=in.read(c))! = -1){
    out.write(c,0,n);
    }
    out.flush();
    out.close();
 }
 catch(IOException e){
  System.out.println("Error "+e);
  }
 }
}
    

II. Buffered stream
buffer flow enhances the ability to read and write files. BufferedWriter BufferedReader objects and classes known as a buffer to create an input, an output stream. (Also referred to as an upper layer flow).

1. The buffer input stream
1) constructor: the BufferedReader (Reader in);

2) BufferReader stream can be read lines, by calling the readLine ();

3) We can create a BufferReader objects by passing an object to a subclass of Reader BufferedReader, such as:

Use buffered input stream:

FileReader inOne = new FileReader("Student.txt");
BufferReader inTwo = BufferedReader(inOne);
String strLine = inTwo.readLine();

The output stream using a similar buffer:

FileWriter tofile = new FileWriter("hello.txt");
BufferedWriter out = BufferedWriter(tofile);

Then can call out
write (String s, int off,
int len); string s hello.txt written, the parameter is the offset from the start off at the s, len is the number of characters written. It also can be called newLine (); return method writes a character to the file.

III. Random stream
in java, we can set up a stream, the stream can not only read files, write files also. That's random stream RandomAccessFile class created.
Either as point source stream flow RandomAccessFile class created, it can be used as a destination stream.

1. Method:
1) constructor:
a RandomAccessFile (String name, String MODE);
Parameter name used to identify a file name is given to create the source stream (and destination). R & lt taking mode parameters (Read Only) or RW (read-write), the decision to create a stream file access rights.
RandomAccessFile (File file, String mode)
; ibid.

Note: random stream to a file, the file is not refreshed.

2. Method used:
Close ();
close the file.
the getFilePointer ();
get the current write location.
; length ()
Get the length of the file.
Read ();
byte read from the file data.
the readBoolean ();
read from the file Boolean value 0 represents false, other representatives true.
the readByte ();
reads a byte from the file.
the readChar ();
reading a character from a file. (Two bytes)
the readDouble ();
reading a double precision floating point value (8 bytes) from the file
the readFloat ();
read a single-precision floating-point (four bytes) from the file
readFully (byte b []);
read into the array of bytes b.length b, completely fills the array
the readInt ();
reading an int value (4 bytes) from the file
the readLine ();
to read a line of text from the file
the readLong ();
reading a long value (8 bytes) from the file
the readShort ();
reading a value of type short (two bytes) from the file
readUnsignedByte ();
file read from an unsigned word section (1 byte)
readUnsignedShort ();
file read from an unsigned short value (2 bytes)
the readUTF ();
reading a UTF string from a file
seek (long position);
positioning read position
setLength (long newlength);
provided length of the file.
skipBytes (int n);
skip a given number of bytes in the file.
write (byte b []);
write to a file b.length bytes
writeBoolean (boolean v);
the Boolean value as a single byte value is written to a file
writeByte (int v);
write to a file byte
writeByte ( string s);
writing to a file string
writeChar (char c);
character written to file
writeChars (string s);
written as a character string data to the file
writeDouble (double v);
writing to the file a double-precision floating-point value
writeFloat (float v);
write single-precision floating point value to a file
writeInt (int v);
writing to a file int value
writeLong (long v);
Write to file a long integer
writeShort (int v);
writing a short value to the file
writeUTF (String s);
writes a file to the UTF string

Published 35 original articles · won praise 0 · Views 1288

Guess you like

Origin blog.csdn.net/c1776167012/article/details/104130770