java 输入/输出流小记 (2)

[b]字符输入流[/b]

[b]BufferedReader[/b]
从 Reader 中读取多个字符到一个缓冲区,然后返回整个缓冲区内容。使小量读写更加
有效。

[b]CharArrayReader[/b]
从一个 Char 数组中顺序读入多个字符

[b]FileReader[/b]
从文件中顺序读入多个字符,是 InputStreamReader 的子类,能从一个自动创建的
FileInputStream 中读取数据。

[b]FilterReader[/b]
字符输入流过滤器类的超类

[b]InputStreamReader[/b]
从一个字节输入流读入字符,使用默认的编码方式或指定的编码方式将字节转化成字符

[b]LineNumberReader[/b]
读取多行文本,并记录已经读取多少内容。

[b]PipedReader[/b]
读取它所连接的 PipedOutputStream 中的多个字符。在多线程程序中使用。

[b]PushbackReader[/b]
将固定大小的“回推缓冲区”添加到 Reader 中,从而这些内容可以是 “读不出”的
,这在某些解析程序中很有用。

[b]Reader[/b]
所有字符输入流的超类

[b]StringReader[/b]
从一个字符串中顺序读入字符

-------------------------------------------------------------
[b]字节输出流[/b]

[b]BufferedOutputStream[/b]
将字节按缓冲区输出,当缓冲区满时才将其中字节写往 OutputStream

[b]ByteArrayOutputStream[/b]
往字节数组中写入字节

[b]CheckedOutputStream[/b]
这个 java.util.zip 包中的类,计算写入 OutputStream 中数据的校验合

[b]DataOutputStream[/b]
以 java 基本类型的二进制形式向 OutputStream 写入

[b]DeflaterOutputStream[/b]
GZIPOutputStream 和 ZipOutputStream 的超类

[b]FileOutputStream[/b]
向文件中顺序写入字节

[b]FilterOutputStream[/b]
字节输出流过滤器类的超类

[b]GZIPOutputStream[/b]
这个 java.util.zip 包中的类,使用 GZIP 格式压缩写入其中的内容
然后输出

[b]ObjectOutputStream[/b]
向一个 OutputStream 写入二进制形式的 java 对象和基本类型,这个类用来序列化
对象。

[b]PipedOutputStream[/b]
将字节写入与他连接的 PipedInputStream 中,在多线程程序中使用

[b]PrintStream[/b]
写入文本形式的 java 对象和基本类型,不推荐使用 PrintStream ,使用 PrintWriter 代替

[b]ZipOutputStream[/b]
这个 java.util.zip 包中的类压缩 ZIP 文件中的数据

------------------------------------------------------------
[b]字符输出流[/b]

[b]BufferedWriter[/b]
为提高效率将字符按缓冲区输出,仅当缓冲区满时才将字符写往输出流。

[b]CharArrayWriter[/b]
向字符数组中写入字符

[b]FileWriter[/b]
向文件中顺序写入字符,它是 OutputStreamWriter 的一个子类,能自动创建
一个FileOutputStream.

[b]FilterWriter[/b]
字符输出流过率器类的超类

[b]OutputStreamWriter[/b]
将字符写入字节输出流,使用默认编码方式或指定编码方式将字符转化成字节。

[b]PipedWriter[/b]
将字符写入他连接的 PipedReader 中,在多线程程序中用到

[b]StringWriter[/b]
将字符顺序写入一个内部创建的 StringBuffer 中

[b]PrintWriter[/b]
在Writer 中写入文本形式的 Java 对象和基本类型

[b]Writer[/b]
字符输出流的超类

----------------------
文件 copy 的 例子 代码

[code]
/**
* io 例子,文件copy 的一个类,加入了相关检验
*/
package cn.lokvin.examples.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/**
* @author lokvin
*
*/
public class FileCopy {

/**
* @param args
*/
public static void main(String[] args) {
if(args.length != 2)
System.err.println("Usage: java FileCopy <source> <destination>");
else {
try {
copy(args[0], args[1]);
}catch(IOException ex) {
System.err.println(ex.getMessage());
}
}

}

/**
* 文件复制的静态方法,复制前进行检查
* @param from_name
* @param to_name
*/
public static void copy(String from_name, String to_name) throws IOException {

File fromFile = new File(from_name);
File toFile = new File(to_name);

//确定源文件存在,可读
if(!fromFile.exists()) {
abort("no such source file: " + from_name);
}

if(!fromFile.canRead()) {
abort("source file is unreadble: " + from_name);
}

//如果目标是目录,用源文件名命名目标
if(toFile.isDirectory()) {
toFile = new File(toFile, fromFile.getName());

}

if(toFile.exists()) {
if(!toFile.canRead()) {
abort("destination file is unreadble: " + to_name);
}

// 询问是否覆盖
System.out.println("Overwrite existing file " + toFile.getName() +
" ? (Y/N):");
System.out.flush();

//获得用户响应
BufferedReader in = new BufferedReader
(new InputStreamReader(System.in));

String response = in.readLine();

if(!response.equalsIgnoreCase("y")) {
abort("existing file was not overwritten. ");
}
}

else {
String parent = toFile.getParent();
if(parent == null) {//如果不存在,使用当前目录
parent = System.getProperty("user.dir");
}

File dir = new File(parent);
if(!dir.exists()) {
abort("destination directory doesn't exist: "+ parent);

}
if(dir.isFile()) {
abort("destination is not a directory: " + parent);
}
if(!dir.canRead()) {
abort("destination dir is unwirteable: " + parent);

}
}

//到此说明一切正常

FileInputStream fromStream = null;
FileOutputStream toStream = null;

try{
fromStream = new FileInputStream(fromFile);
toStream = new FileOutputStream(toFile);

byte[] buffer = new byte[4096];
int bytes_read;

while((bytes_read = fromStream.read(buffer)) != -1 ){
toStream.write(buffer, 0, bytes_read);

}


}

finally {

if(fromStream != null) try{fromStream.close(); } catch(IOException ex) {}
if(toStream != null) try {toStream.close();} catch(IOException ex) {}

}

}


private static void abort(String msg) throws IOException{
throw new IOException("FileCopy: " + msg);
}

}

[/code]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值