InputStream中read()与read(byte[] b)
原文:InputStream中read()与read(byte[] b)
read()与read(byte[] b)这两个方法在抽象类InputStream中前者是作为抽象方法存在的,后者不是,JDK API中是这样描述两者的:
1:read() :
从输入流中读取数据的下一个字节,返回0到255范围内的int字节值。如果因为已经到达流末尾而没有可用的字节,则返回-1。在输入数据可用、检测到流末尾或者抛出异常前,此方法一直阻塞。
2:read(byte[] b) :
从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。以整数形式返回实际读取的字节数。在输入数据可用、检测到文件末尾或者抛出异常前,此方法一直阻塞。如果 b 的长度为 0,则不读取任何字节并返回 0;否则,尝试读取至少一个字节。如果因为流位于文件末尾而没有可用的字节,则返回值 -1;否则,至少读取一个字节并将其存储在 b 中。将读取的第一个字节存储在元素 b[0] 中,下一个存储在 b[1] 中,依次类推。读取的字节数最多等于 b 的长度。设 k 为实际读取的字节数;这些字节将存储在 b[0] 到 b[k-1]
的元素中,不影响 b[k] 到 b[b.length-1] 的元素。
由帮助文档中的解释可知,read()方法每次只能读取一个字节,所以也只能读取由ASCII码范围内的一些字符。这些字符主要用于显示现代英语和其他西欧语言。而对于汉字等unicode中的字符则不能正常读取。只能以乱码的形式显示。
对于read()方法的上述缺点,在read(byte[] b)中则得到了解决,就拿汉字来举例,一个汉字占有两个字节,则可以把参数数组b定义为大小为2的数组即可正常读取汉字了。当然b也可以定义为更大,比如如果b=new byte[4]的话,则每次可以读取两个汉字字符了,但是需要注意的是,如果此处定义b 的大小为3或7等奇数,则对于全是汉字的一篇文档则不能全部正常读写了。
下面用实例来演示一下二者的用法:
实例说明:类InputStreamTest1.java 来演示read()方法的使用。类InputStreamTest2.java来演示read(byte[] b)的使用。两个类的主要任务都是通过文件输入流FileInputStream来读取文本文档xuzhimo.txt中的内容,并且输出到控制台上显示。
先看一下xuzhimo.txt文档的内容

InputStreamTest1.java
- /**
- * User: liuwentao
- * Time: 12-1-25 上午10:11
- */
- public class InputStreamTest1 {
- public static void main(String[] args){
- String path = "D:\\project\\opensouce\\opensouce_demo\\base_java\\classes\\demo\\java\\inputstream\\";
- File file = new File(path + "xuzhimo.txt");
- InputStream inputStream = null;
- int i=0;
- try {
- inputStream = new FileInputStream(file);
- while ((i = inputStream.read())!=-1){
- System.out.print((char)i + "");
- }
- }catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
/**
* User: liuwentao
* Time: 12-1-25 上午10:11
*/
public class InputStreamTest1 {
public static void main(String[] args){
String path = "D:\\project\\opensouce\\opensouce_demo\\base_java\\classes\\demo\\java\\inputstream\\";
File file = new File(path + "xuzhimo.txt");
InputStream inputStream = null;
int i=0;
try {
inputStream = new FileInputStream(file);
while ((i = inputStream.read())!=-1){
System.out.print((char)i + "");
}
}catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
执行结果:

如果将while循环中的 (char)去掉,即改成:
则执行结果:

InputStreamTest2.java
- /**
- * User: liuwentao
- * Time: 12-1-25 上午10:11
- */
- public class InputStreamTest2 {
- public static void main(String[] args){
- String path = "D:\\project\\opensouce\\opensouce_demo\\base_java\\src\\demo\\java\\inputstream\\";
- File file = new File(path + "xuzhimo.txt");
- InputStream inputStream = null;
- int i=0;
- try {
- inputStream = new FileInputStream(file);
- byte[] bytes = new byte[16];
- while ((i = inputStream.read(bytes))!=-1){
- String str = new String(bytes);
- System.out.print(str);
- }
- }catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
/**
* User: liuwentao
* Time: 12-1-25 上午10:11
*/
public class InputStreamTest2 {
public static void main(String[] args){
String path = "D:\\project\\opensouce\\opensouce_demo\\base_java\\src\\demo\\java\\inputstream\\";
File file = new File(path + "xuzhimo.txt");
InputStream inputStream = null;
int i=0;
try {
inputStream = new FileInputStream(file);
byte[] bytes = new byte[16];
while ((i = inputStream.read(bytes))!=-1){
String str = new String(bytes);
System.out.print(str);
}
}catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
执行结果:

遗憾的是,还是有乱码,解决办法可以参见下面教程
http://wentao365.iteye.com/blog/1183951
修改后的代码:
- /**
- * User: liuwentao
- * Time: 12-1-25 上午10:11
- */
- public class InputStreamTest3 {
- public static void main(String[] args) {
- String path = "D:\\project\\opensouce\\opensouce_demo\\base_java\\src\\demo\\java\\inputstream\\";
- File file = new File(path + "xuzhimo.txt");
- InputStream inputStream = null;
- String line;
- StringBuffer stringBuffer = new StringBuffer();
- try {
- //InputStream :1)抽象类,2)面向字节形式的I/O操作(8 位字节流) 。
- inputStream = new FileInputStream(file);
- //Reader :1)抽象类,2)面向字符的 I/O操作(16 位的Unicode字符) 。
- Reader reader = new InputStreamReader(inputStream, "UTF-8");
- //增加缓冲功能
- BufferedReader bufferedReader = new BufferedReader(reader);
- while ((line = bufferedReader.readLine()) != null) {
- stringBuffer.append(line);
- }
- if (bufferedReader != null) {
- bufferedReader.close();
- }
- String content = stringBuffer.toString();
- System.out.print(content);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
/**
* User: liuwentao
* Time: 12-1-25 上午10:11
*/
public class InputStreamTest3 {
public static void main(String[] args) {
String path = "D:\\project\\opensouce\\opensouce_demo\\base_java\\src\\demo\\java\\inputstream\\";
File file = new File(path + "xuzhimo.txt");
InputStream inputStream = null;
String line;
StringBuffer stringBuffer = new StringBuffer();
try {
//InputStream :1)抽象类,2)面向字节形式的I/O操作(8 位字节流) 。
inputStream = new FileInputStream(file);
//Reader :1)抽象类,2)面向字符的 I/O操作(16 位的Unicode字符) 。
Reader reader = new InputStreamReader(inputStream, "UTF-8");
//增加缓冲功能
BufferedReader bufferedReader = new BufferedReader(reader);
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
}
if (bufferedReader != null) {
bufferedReader.close();
}
String content = stringBuffer.toString();
System.out.print(content);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
执行结果:

还是遗憾,没有换行。
解决办法,通过 commons-io-*.jar
- /**
- * User: liuwentao
- * Time: 12-1-25 上午10:11
- */
- public class InputStreamTest4 {
- public static void main(String[] args) {
- String path = "D:\\project\\opensouce\\opensouce_demo\\base_java\\src\\demo\\java\\inputstream\\";
- File file = new File(path + "xuzhimo.txt");
- String content = null;
- try {
- content = FileUtils.readFileToString(file, "utf-8");
- } catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("content:" + content);
- }
- }
/**
* User: liuwentao
* Time: 12-1-25 上午10:11
*/
public class InputStreamTest4 {
public static void main(String[] args) {
String path = "D:\\project\\opensouce\\opensouce_demo\\base_java\\src\\demo\\java\\inputstream\\";
File file = new File(path + "xuzhimo.txt"); String content = null;
try {
content = FileUtils.readFileToString(file, "utf-8");
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("content:" + content);
}
}
执行结果:

InputStream中read()与read(byte[] b)的更多相关文章
- Java学习之InputStream中read()与read(byte[] b)
Java学习之InputStream中read()与read(byte[] b) 这两个方法在抽象类InputStream中都是作为抽象方法存在的, JDK API中是这样描述两者的: read() ...
- InputStream中read()与read(byte[] b)(转)
read()与read(byte[] b)这两个方法在抽象类InputStream中前者是作为抽象方法存在的,后者不是,JDK API中是这样描述两者的: 1:read() : 从输入流中读取数据的下 ...
- 读取InputStream 中的内容
读取InputStream 中的内容 ]) { , len); //把读取到的内容写到输出流中 } //<4> 把字节数组转换为字符串 String content = baos.to ...
- OpenCV中IplImage图像格式与BYTE图像数据的转换
最近在将Karlsruhe Institute of Technology的Andreas Geiger发表在ACCV2010上的Efficent Large-Scale Stereo Matchin ...
- (一)一个工作任务引起的乱战——c#中结构体与byte[]间相互转换
一个工作任务涉及到c#与c++系统间的udp通信,处理了蛮长时间没有完成任务,但是期间接触到不少小知识点.本人是初接触c#,c++语言没有接触过.可能写的东西都很小儿科,暂且记录下来当工作日记把. 先 ...
- Android中如何将Bitmap byte裸数据转换成Bitmap图片int数据
Android中如何将Bitmap byte裸数据转换成Bitmap图片int数据 2014-06-11 10:45:14 阅读375次 我们在JNI中处理得到的BMP图片Raw数据,我们应该如何 ...
- InputStream中通过mark和reset方法重复利用缓存
通过缓存InputStream可重复利用一个InputStream,但是要缓存一整个InputStream内存压力可能是比较大的.如果第一次读取InputStream是用来判断文件流类型,文件编码等用 ...
- java中的int与byte的转化
java中的int与byte的转化 1.基础准备 1.1.原码 就是二进制码,最高位为符号位,0表示正数,1表示负数,剩余部分表示真值 1.2.反码 在原码的基础上,正数反码就是他本身,负数除符号位之 ...
- WPF中的Bitmap与byte
原文:WPF中的Bitmap与byte public MainWindow() { InitializeComponent(); byte[] b = GetPictureData(@"F: ...
随机推荐
- asp.net根据模版生成Word小记
最近遇到一个问题,客户提了一个新的需求,客户想要将显示在网页上的数据导出成Word进行套打,由于之前没有接触过这一块的内容,自己写的系统也没有使用这种功能,现在重头学习. 具体思路: 1.先制作Wor ...
- 远程唤醒、WOL、Magic_Packet
背景:很多人熟悉远程桌面并经常地利用它所带来的方便,但是前提是服务器(远程电脑)必须是处于开机状态.对于机房里有专人管理的服务器,这点不成问题,但如果是放在家里的电脑,要让它7*24地开机似乎就不好办 ...
- springdata+redis配置详解
springdata设计初衷是位简化数据类型和数据的持久化存储,它并不局限是关系型数据库还是nosql数据库,都提供了简化的数据库连接,让数据获取变得更加的简单.所有这些的实现有统一的api提供. 本 ...
- JavaSE学习总结第04天_Java基础语法3
04.01 选择结构switch语句的格式及其解释 switch语句的格式: switch(表达式) { case 值1:语句体1;break; case 值2:语句体2;break; ...
- eclipse(MyEclipse)插件之aptana安装
1.在MyEclipse安装目录下创建文件夹, aptana2.在aptana文件夹下创建文件夹eclipse3.将aptana_update_024747.zip中的文件解压缩到aptana\ecl ...
- OCP-1Z0-053-V13.02-712新题
Why does the number of blocks for the table remain the sale after the shrink operation? A.Because ...
- Yii2 国际化的问题 zh-CN
代码增加位置: 在项目文件目录的config->main.php 的 return 内增加以下内容,并在项目目录中新建messages 内设置 en-US 和zh-CN 文件夹.zh-CN文件夹 ...
- CentOS6.5安装Python2.7和Pip
注:文中所写的安装过程均在CentOS6.5 x86下通过测试,安装的Python版本为2.7.12,Pip版本为8.1.2 主要参考博文:http://bicofino.io/2014/01/16/ ...
- C++堆和栈的比较(7个区别)
基础知识: 堆 栈是一种简单的数据结构,是一种只允许在其一端进行插入或删除的线性表.允许插入或删除操作的一端称为栈顶,另一端称为栈底,对堆栈的插入和删除操作被称 为入栈和出栈.有一组CPU指令可以实现 ...
- IT第八天 - 类的应用、debug、项目开发模式优化
IT第八天 上午 类的应用 1.对象在实例化时是非常耗费系统资源的,因此要尽量减少new字段的使用 2.类的初始值是null,在使用未实例化的类时,很容易导致报错:NullExceptionPoint ...