您的位置:首页 > 其它

IO_02

2013-10-03 10:18 225 查看
IO流(InputStream,OutputStream)抽象类

Java中的IO流是实现输入/输出的基础 EOF == End Of File ==-1;

输入基本方法:

int b = in.read()读取一个byte无符号填充到int低八位,-1是EOF

in.read(byte[] buf)读取数据填充到buf中 in.read(byte[] buf, int start, int size) in.skip(long n) in.close()

输出基本方法:

out.write(int b)
写出一个byte到流b的低八位写出

out.write(byte[] buf) 将缓冲区buf都写入到流 out.write(byte[] buf,int start,int size) out.flush()清理缓冲,out.close();

IO工具类 IOUtils.java

printHex()方法,读取文件并且安装HEX输出,每10byte为一行,没输出一行,则换行

复制文件,从输入流读取写出输出流

public static void copy(String src, String dest) throws IOException{
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);
byte[] buf = new byte[1024*512];
int count;
while((count=in.read(buf))!=-1){//EOF
out.write(buf, 0, count);
}
in.close();
out.close();
}


FileInputStream和FileOutputStream

DataOutputStream和DataInputStream是对"流"功能的扩展,提供了基本数据类型的序列化与反序列化

BufferedInputStream和BufferedOuputStream为IO操作提供了缓冲区,一般打开文件进行读写时都加上缓冲流,提高IO性能

仅使用FOS的write方法,相当于一滴水一滴水的转移;使用DOS的write方法,相当于一瓢一瓢的转移;使用BOS的write方法相当于从DOS一瓢一瓢放入桶BOS中,再从桶BOS中倒入另一个缸,性能提高

Java的文本(char)是16位无符号整数,是字符的unicode编码,文件时byte by byte ...的数据序列

文本文件是字符序列安装某种编码方案序列化为byte的存储结果

字符流(Reader Writer)

字符的处理:一次处理一个字符

字符的底层仍是基本的字节流

字符流的基本实现:InputStreamReader-->byte--char 编码解析;OutputStreamWriter -->char--byte 编码处理

字符流的过滤器

是字符读写的功能扩展,方便文本的读写操作

BufferedReader:readLine()一次读一行 ;PrintWriter:println() 一次打印一行

读一个文本文件

String file = "demo.txt";
BufferedReader br = new BufferedReader(new FileReader(file));
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));


写出一个文本文件 类似
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: