InputStream中read()与read(byte[] b) 标签: bytestringjavafilenullpath 2012-08-21 20:38 15233人阅读 评论(1) 收藏
2017-08-14 13:57
465 查看
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
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();
}
}
}
[java] view
plain copy
/**
* 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)去掉,即改成:
引用
System.out.print(i + "");
则执行结果:
InputStreamTest2.java
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();
}
}
}
[java] view
plain copy
/**
* 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
修改后的代码:
Java代码
/**
* 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();
}
}
}
[java] view
plain copy
/**
* 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
Java代码
/**
* 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);
}
}
[java] view
plain copy
/**
* 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);
}
}
执行结果:
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
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();
}
}
}
[java] view
plain copy
/**
* 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)去掉,即改成:
引用
System.out.print(i + "");
则执行结果:
InputStreamTest2.java
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();
}
}
}
[java] view
plain copy
/**
* 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
修改后的代码:
Java代码
/**
* 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();
}
}
}
[java] view
plain copy
/**
* 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
Java代码
/**
* 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);
}
}
[java] view
plain copy
/**
* 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);
}
}
执行结果:
相关文章推荐
- Volecity模板引擎学习笔记<一> 标签: 模板引擎volecity 2016-03-20 17:29 1229人阅读 评论(0) 收藏 举报 分类: Java开发(36) web前端(13
- Java中byte与16进制字符串的互相转换 分类: Android JAVA 2015-05-26 19:07 25人阅读 评论(0) 收藏
- bash: ./java: cannot execute binary file 问题 分类: ubuntu 2013-05-06 18:05 788人阅读 评论(0) 收藏
- java.io.FileInputStream.read()的返回值类型为什么是int而不是byte
- Android自定义控件系列二:自定义开关按钮(一) 标签: Android自定义控件Java开关按钮自定义组件 2014-11-03 10:55 2735人阅读 评论(6) 收藏 举报 分类: J
- 数据库阿里连接池 druid配置详解 标签: druidspringjavaxml配置阿里池 2016-06-16 00:34 57532人阅读 评论(11) 收藏 举报 版权声明:本文为博主原创文
- Hadoop2.6.0运行mapreduce之Uber模式验证 标签: hadoopmapreduce源码uberjava 2016-05-05 14:55 19815人阅读 评论(2) 收藏 举报
- java微信开发API解析(三)-高级功能的前奏----获取以及保存接口调用凭证 标签: 微信java办微信开发微信文档解析 2016-04-06 20:50 1101人阅读 评论(0) 收藏 举报
- Java中byte与16进制字符串的互换原理 分类: Java 2015-08-04 16:19 1人阅读 评论(0) 收藏
- hdfs.DFSClient: DataStreamer Exception: org.apache.hadoop.ipc.RemoteException: java.io.IOException: 分类: hadoop 2015-03-21 10:56 57人阅读 评论(0) 收藏
- 解决linux下java读取串口之权限问题 No permission to create lock file. 分类: LINUX 2010-07-13 20:51 2006人阅读 评论(0) 收藏
- Java当中InputStream中read()、read(byte[] b)、read(byte[] b,int off,int len)
- Java操作串口 分类: Java 2015-07-30 23:18 11人阅读 评论(0) 收藏
- 冒泡排序... 分类: java 2009-11-02 14:09 224人阅读 评论(0) 收藏
- java.lang.nullpointerexcep… 分类: Android开发 2014-05-30 10:55 51人阅读 评论(0) 收藏
- 使用xml及java代码混合的方式来设置图形界面 分类: H1_ANDROID 2013-10-26 17:33 1859人阅读 评论(0) 收藏
- Log4j 2使用教程 分类: B1_JAVA 2014-07-01 12:26 314人阅读 评论(0) 收藏
- Gradle命令和配置 标签: AndroidGradle命令配置 2016-03-11 09:32 17174人阅读 评论(5) 收藏 举报 分类: 开发工具(9) 版权声明:本文为博主原创文
- Java解析HTML之HTMLParser使用与详解 分类: C_OHTERS 2014-05-19 21:46 2309人阅读 评论(0) 收藏
- spring quartz:Instantiation of bean failed; nested exception is java.lang.IncompatibleClassChangeErr 分类: Java spring 2015-01-07 19:00 438人阅读 评论(0) 收藏