Java字符流inputStream和outputStream(详解read()的返回值到底是什么和write()的byte[]参数)

字节流的作用:字节流一般用于读取或者写入二进制文件,如图片音频文件,还可以处理那些字符流无法正常处理的二进制可执行文件。一般而言只要是非文本数据就应该使用字节流来处理。

因为字符流无法保证非文本数据的完整性;(对这句话的理解以后详细解释)

1,字节输入流InputStream。


public class TestDemo{
	public static void main(String args[]) {
		File file=new File("E:"+File.separator+"testdemo1"+File.separator+"file.txt");
		byte b[]=new byte[520];
		InputStream input=null;
		try {
			input=new FileInputStream(file);
			input.read(b);
			input.close();
		}catch(Exception e) {
			e.printStackTrace();
		}
		System.out.println(new String (b));
		
	}
}

这种写法开辟的空间大小固定,浪费资源,这是不合理的。为此我们可以修改上面的程序如下·:

public class TestDemo{
	public static void main(String args[]) {
		File file=new File("E:"+File.separator+"testdemo1"+File.separator+"file.txt");
		byte b[]=new byte[(int)file.length()];
		InputStream input=null;
		try {
			input=new FileInputStream(file);
			input.read(b);
			input.close();
		}catch(Exception e) {
			e.printStackTrace();
		}
		System.out.println(new String (b));
		
	}
}

除了这种方法之外,我们还可以使用循环输出的方法从文件中将一个一个字节读出来。这种方法直接使用read()方法,不需要出入byte数组。如下:


public class TestDemo{
	public static void main(String args[]) {
		File file=new File("E:"+File.separator+"testdemo1"+File.separator+"file.txt");
		byte b[]=new byte[(int)file.length()];
		InputStream input=null;
		int temp=0;
		int len=0;
		try {
			input=new FileInputStream(file);
			while((temp=input.read())!=-1) {
				b[len]=(byte)temp;
				Thread.sleep(500);
				System.out.println(len+"---"+temp);//------------1
				len++;
			}
			input.close();
		}catch(Exception e) {
			e.printStackTrace();
		}
		System.out.println(new String (b));
	}
}

上面三种输入方法中,都会以不同形式使用到字节类型数组b,但是你们真的知道b这个数组具体是干什么的吗?为了验证b的功能,我在最后一种上面这个Demo中添加一行代码,就是注释1那行。我的E盘中testdemo1目录下的file1文件中有四个汉字“春花秋月”。现在来看输出:

0---180
1---186
2---187
3---168
4---199
5---239
6---212
7---194
春花秋月

是不是优点明白了,原来read()方法返回的是一个汉字的字节编码的十进制表示形式,比如“春”,它的字节编码是B4 BA,换算成十进制就是:180 186,是不是突然明白了,原来read()方法每次读取一个字符编码的一个字节编码,然后存储在字节数组b中,到把所有字符读取完成之后,read()就返回-1了,于是跳出while循环,此时byte b[]数组中的存储的值是byte b[]=[180,186,187,168,199,239,212,194];当输出的时候通过new String(byte b[])这个String方法的构造把字节数组转换成字符串,于是的得以输出。同理,上面的两个Demo中的b数组也是这个道理,只不过是先把字符解码,然后把这些码缓存在字节数组b中。

2,字节输出流outputStream:


public class TestDemo{
	public static void main(String args[]) {
		File file=new File("E:"+File.separator+"testdemo1"+File.separator+"file.txt");
		String str="春花秋月";
		OutputStream output=null;
		byte b[]=str.getBytes();//将汉字转化成对应的编码存放在b数组中
		try {
			//其中public FileOutput(File file,boolean applen);第二个参数设为true表示追加
			//output=new FileOutputStream(file,true);
			output=new FileOutputStream(file);
			output.write(b);
			output.close();
			System.out.println("写入成功");
		}catch(Exception e) {
			System.out.println("写入失败");
			e.printStackTrace();
		}
	}
}

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值