学习IO,有必要了解一下Decorator装饰器模式,该模式在IO设计中大量被使用:
http://blog.youkuaiyun.com/eyeooo/article/details/128708671.InputStream
类 | 说明 | 构造器参数 |
ByteArrayInputStream | 允许将内存的缓冲区当做InputStream | 缓冲区,字节从中取出 |
StringBufferInputStream(Deprecated) | 将String转InputStream | 字符串,底层用StringBuffer实现 |
FileInputStream | 读取文件信息 | 字符串表示的文件名/文件/FileDescriptor |
PipedInputStream | 数据写入相关PipedOutputStream, 实现管道化 | PipedOutputStream(多线程数据源) |
SequenceInputStream | 两个或多个InputStream转换成一个 | 两个InputStream,或者一个容纳InputStream对象容器Enumeration |
FilterInputStream | 抽象类,装饰器接口 | 3 |
2.OutputStream
类 | 说明 | 构造器参数 |
ByteArrayOutputStream | 内存中创建缓冲区,所有送往流的数据都在此缓冲区 | 缓冲区初始大小 |
FileOutputStream | 数据写入文件 | 字符串表示的文件名/文件/FileDescriptor |
PipedOutputStream | 数据写入相关PipedInputStream, 实现管道化 | PipedInputStream(多线程数据目的地) |
FilterOutputStream | 抽象类,装饰器接口 | 4 |
3.FilterInputStream
DataInputStream | 对应DataOutputStream,在流读取基本类型 | InputStream |
BufferedInputStream | 防止每次读取都进行实际写操作。代表使用缓冲区。 | InputStream,可选指定缓冲大小 |
LineNumerInputStream | 跟踪输入流行号,getLineNumber()/setLineNumber() | InputStream |
PushbackInputStream | 能取出一个字节的缓冲区,可将读到最后一个字符回退。 | InputStream,编译器扫描器。 |
4.FilterOutputStream
DataOutputStream | 对应DataInputStream | OutputStream |
PrintStream | 格式化输出 | OutputStream |
BufferedOutputStream | 防止每次发送数据都要实际写操作,flush()清空缓冲区 | OutputStream,可选设定缓冲大小 |
5.Reader与Writer
1)字符
2)适配器:InputStreamReader, OutputStreamWriter
3)就是为了在所有IO操作中支持Unicode,并且快!
6.对应
InputStream - Reader (InputStreamReader),
OutputStream - Writer (OutputStreamWriter),
FileInputStream - FileReader,
FileOutputStream - FileWriter,
StringBufferInputStream(Deprecated) - StringReader,
(null) - StringWriter,
ByteArrayInputStream - CharArrayReader,
ByteArrayOutputStream - CharArrayWriter,
PipedInputStream - PipedReader,
PipedOutputStream - PipedWriter,
FilterInputStream - FilterReader,
FilterOutputStream - FilterWriter,
BufferedInputStream - BufferedReader,
BufferedOutputStream - BufferWriter,
LIneNumberInputStream(Deprecated) - LineNumberReader,
PushbackInputStream - PushbackReader.
7. RandomAccessFile
直接从Object派生,适用于已知大小文件,可以用seek()定位位置读写文件记录等。
8.常用简单实例
import java.io.*;
//From flie
public class BufferedInputFile {
// Throw exceptions to console:
public static String
read(String filename) throws IOException {
// Reading input by lines:
BufferedReader in = new BufferedReader(
new FileReader(filename));
String s;
StringBuilder sb = new StringBuilder();
while((s = in.readLine())!= null)
sb.append(s + "\n");
in.close();
return sb.toString();
}
public static void main(String[] args)
throws IOException {
System.out.print(read("BufferedInputFile.java"));
}
}
//From Memory
import java.io.*;
public class MemoryInput {
public static void main(String[] args)
throws IOException {
StringReader in = new StringReader(
BufferedInputFile.read("MemoryInput.java"));
int c;
while((c = in.read()) != -1)
System.out.print((char)c);
}
}
//: io/BasicFileOutput.java
import java.io.*;
public class BasicFileOutput {
static String file = "BasicFileOutput.out";
public static void main(String[] args)
throws IOException {
BufferedReader in = new BufferedReader(
new StringReader(
BufferedInputFile.read("BasicFileOutput.java")));
PrintWriter out = new PrintWriter(
new BufferedWriter(new FileWriter(file)));
int lineCount = 1;
String s;
while((s = in.readLine()) != null )
out.println(lineCount++ + ": " + s);
out.close();
// Show the stored file:
System.out.println(BufferedInputFile.read(file));
}
}
//: io/FileOutputShortcut.java
import java.io.*;
public class FileOutputShortcut {
static String file = "FileOutputShortcut.out";
public static void main(String[] args)
throws IOException {
BufferedReader in = new BufferedReader(
new StringReader(
BufferedInputFile.read("FileOutputShortcut.java")));
// Here's the shortcut:
PrintWriter out = new PrintWriter(file);
int lineCount = 1;
String s;
while((s = in.readLine()) != null )
out.println(lineCount++ + ": " + s);
out.close();
// Show the stored file:
System.out.println(BufferedInputFile.read(file));
}
}
9.标准IO
System.in, System.out, System.err
//: io/Echo.java
// How to read from standard input.
// {RunByHand}
import java.io.*;
public class Echo {
public static void main(String[] args)
throws IOException {
BufferedReader stdin = new BufferedReader(
new InputStreamReader(System.in));
String s;
while((s = stdin.readLine()) != null && s.length()!= 0)
System.out.println(s);
// An empty line or Ctrl-Z terminates the program
}
} ///:~
10. 进程控制
import java.io.*;
public class OSExecuteDemo {
public static void main(String[] args) {
OSExecute.command("ipconfig");
}
}
class OSExecute{
public static void command(String command) {
boolean err = false;
try {
Process process = new ProcessBuilder(command.split(" ")).start();
BufferedReader result = new BufferedReader(new InputStreamReader(process.getInputStream()));
String s;
while((s=result.readLine())!=null){
System.out.println(s);
}
BufferedReader errors = new BufferedReader(new InputStreamReader(process.getErrorStream()));
while((s=errors.readLine())!=null){
System.out.println(s);
err=true;
}
} catch (IOException e) {
if(!command.startsWith("CMD /C")){
command("CMD /C "+command);
}else{
throw new RuntimeException(e);
}
}
if(err){
System.err.println("ERROE CMD: "+command);
}
}
}
.........未完还有下一篇:)