IO学霸

学习IO,有必要了解一下Decorator装饰器模式,该模式在IO设计中大量被使用:

http://blog.csdn.net/eyeooo/article/details/12870867

1.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对应DataInputStreamOutputStream
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);
		}
	}
}

.........未完还有下一篇:)





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值