常用Java IO Classes 关系图
|
||||
Byte
|
InputStream/ OutputStream
|
Node
Byte Stream
|
FileInputstream/ FileOutputStream
|
|
PipeInputStream/
PipeOutputStream
|
|
|||
…
|
|
|||
Processing Byte Stream
|
FilterInputStream/
FilterOutputStream
|
BufferInputStream/
BufferOutputStream
|
||
DataInputStream/
DataOutputStream
|
||||
PrintStream
|
|
|||
…
|
|
|||
Byte 与 Char 通过 InputstreamReader 和 OutputStreamWriter 来转换
|
||||
Char
|
Reader/Writer
|
Node
Char Stream
|
FileReader/FileWriter
|
|
PipeReader/PipeWriter
|
|
|||
…
|
|
|||
Processing Char Stream
|
|
BufferReader/
BufferWriter
|
||
PrintWriter
|
|
|||
…
|
|
(一)Stream的分类:
abstract int
(可对应Char)
|
read()
Reads the next byte of data from the input stream |
int
|
read(byte[] b)
Reads some number of bytes from the input stream and stores them into the buffer array b. |
void
|
close()
Closes this input stream and releases any system resources associated with the stream. (Stream 用完之后要注意关闭!) |
abstract void
|
write(int b)
Writes the specified byte to this output stream. |
void
|
write(byte[] b)
Writes b.length bytes from the specified byte array to this output stream. |
void
|
close()
Closes this output stream and releases any system resources associated with this stream. |
void
|
flush()
Flushes this output stream and forces any buffered output bytes to be written out. (不必等buffer满了再写出,强行把所有的东西都写出来) |
boolean
|
|
byte
|
readByte()
|
char
|
readChar()
|
double
|
|
float
|
|
int
|
readInt()
|
void
|
writeBoolean(boolean v)
Writes a boolean to the underlying output stream as a 1-byte value. |
void
|
writeByte(int v)
Writes out a byte to the underlying output stream as a 1-byte value. |
void
|
writeDouble(double v)
Converts the double argument to a long using the doubleToLongBits method in class Double, and then writes that long value to the underlying output stream as an 8-byte quantity, high byte first. |
void
|
writeFloat(float v)
Converts the float argument to an int using the floatToIntBits method in class Float, and then writes that int value to the underlying output stream as a 4-byte quantity, high byte first. |
void
|
writeInt(int v)
Writes an int to the underlying output stream as four bytes, high byte first. |
Constructor Summary
|
|
Constructor Summary
|
|
FileWriter(String fileName, boolean append)
Constructs a FileWriter object given a file name with a boolean indicating whether or not to append the data written. |
Constructor Summary
|
|
PrintWriter(File file)
Creates a new PrintWriter, without automatic line flushing, with the specified file. |
|
PrintWriter(OutputStream out)
Creates a new PrintWriter, without automatic line flushing, from an existing OutputStream. |
|
|
void
|
println(boolean x)
Prints a boolean value and then terminates the line. |
void
|
println(char x)
Prints a character and then terminates the line. |
void
|
println(double x)
Prints a double-precision floating-point number and then terminates the line. |
void
|
println(float x)
Prints a floating-point number and then terminates the line. |
void
|
println(long x)
Prints a long integer and then terminates the line. |
void
|
|
void
|
int
|
read()
Reads a single character. |
readLine()
Reads a line of text. |
|
void
|
close()
Closes the stream and releases any system resources associated with it. |
void
|
write(char[] cbuf, int off, int len)
Writes a portion of an array of characters. |
void
|
|
void
|
close()
Closes the stream, flushing it first. |
void
|
flush()
Flushes the stream. |
Constructor Summary
|
|
Constructor Summary
|
|
OutputStreamWriter(OutputStream out)
Creates an OutputStreamWriter that uses the default character encoding. |
(三)IO 编程的一般流程:
import java.io.*;
/*
1. Creat node stream;
2. Upgrade node stream to processing stream if necessary
3. Use the methods in the stream object to read or write
4. Close the stream and release the resource
--------------------------------------------------------
1. Create InputStream/Reader
2. Upgrade to Buffered
3. Use readLine()
While((str=in.readln())!=null)
4. close()
--------------------------------------------------------
1. Create OutputStream/Writer
2. Upgrade to PrintWriter
3. Use println()
4. close()
*/
public class IOProcessSample{
public static void main(String[] args) {
//Create a file based on the first command-line argument to the program
File file= new File(args[0]);
//Create buffered reader from the standard input
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Press ctr-d or ctr-z to end");
String str;
try{
//Create a print write to write on a file
//PrintWriter is required to handled the IO exception
PrintWriter out= new PrintWriter(file);
//Read from the standard input and write to the file
while((str=in.readLine())!=null){
out.println(str);
}
//close the stream and release the resource
in.close();
out.close();
}
catch(FileNotFoundException e){
System.err.println("File not found in part 1 : "+file);
}
catch (IOException e){
e.printStackTrace();
}
finally{
System.out.println("-----------Part1 is ended-----------------------");
}
//////////////////////////////////////////////////////////////////////////////
try{
//Create a buffer reader from a file
in=new BufferedReader(new FileReader(file));
//Read the file and print the content on the screen.
while((str=in.readLine())!=null){
System.out.println(str);
}
//close the stream and release the resource
in.close();
}
catch (FileNotFoundException e){
System.err.println("File not found in part 2: "+file);
}
catch (IOException e){
e.printStackTrace();
}
finally{
System.out.println("----------------------The End -------------------------");
}
}
}