IO_02

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)));


写出一个文本文件 类似

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值