- 浏览: 1412210 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (363)
- oracle (43)
- 缓存策略 (4)
- 数据库设计 (7)
- IntelliJ IDEA (8)
- freemarker (3)
- JSP (12)
- java基础 (75)
- tomcat (14)
- 服务器 (10)
- maven (3)
- linux (21)
- e-learning (2)
- 手机开发 (1)
- 设计模式 (6)
- 技术之外 (2)
- 工具的使用 (8)
- HTML 5 (5)
- 网站前台 (38)
- 网站后台 (11)
- 项目管理 (12)
- 面试题 (5)
- 幽默 (0)
- 系统架构 (8)
- 创业 (10)
- div-css (5)
- 诗词歌赋 (1)
- 网络课堂组知识库 (4)
- 杂家 (3)
- android (12)
- 框架 (14)
- 读书笔记 (1)
- 孙子兵法 (1)
- mysql (3)
- 小人书 (0)
- Jquery (6)
- eclipse (2)
- 正则表达式 (1)
最新评论
-
a98709474:
写的很详细,看完了,明白了这种概念了
数据库水平切分(拆库拆表)的实现原理解析(转) -
a98709474:
MYSQL分库分表总结 -
CatchU:
7年前的招聘要求,厉害厉害
面试要求 -
luozhixiong:
<table class="bbcode&qu ...
MYSQL分库分表总结 -
沈寅麟:
好用
freemarker格式化
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
执行结果:
如果将while循环中的 (char)去掉,即改成:
则执行结果:
InputStreamTest2.java
执行结果:
遗憾的是,还是有乱码,解决办法可以参见下面教程
http://wentao365.iteye.com/blog/1183951
修改后的代码:
执行结果:
还是遗憾,没有换行。
解决办法,通过 commons-io-*.jar
执行结果:
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(); } } }
执行结果:
如果将while循环中的 (char)去掉,即改成:
引用
System.out.print(i + "");
则执行结果:
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(); } } }
执行结果:
遗憾的是,还是有乱码,解决办法可以参见下面教程
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(); } } }
执行结果:
还是遗憾,没有换行。
解决办法,通过 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); } }
执行结果:
发表评论
-
打印IP 来源
2014-10-16 22:07 891<% String userAgent = reques ... -
java 写文本换行
2014-08-08 18:34 1868import org.apache.commons.io.Fi ... -
DBCP连接池配置示例
2014-06-17 11:44 1196<bean id="dataSourceOra ... -
CGLIB与asm
2014-05-05 14:42 1291“程序运行时,允许改变程序结构或变量类型,这种语言称为动态语言 ... -
201404转成 2014.04
2014-03-31 17:45 942public static void main(Str ... -
取出 字符串中的 网址
2013-10-15 17:47 1214public static void main(Str ... -
java反国际化
2013-09-23 18:10 945public static String fromEn ... -
AtomicInteger与Volatile修饰
2013-02-05 11:50 5989今天写代码,尝试使用了AtomicInteger这个类,感觉 ... -
ExecutorService线程池
2013-02-05 11:40 1576http://blog.sina.com.cn/s/blog ... -
web.xml中load-on-startup的作用
2012-12-24 10:06 1612如下一段配置,熟悉DWR的再熟悉不过了: <servle ... -
最新版本
2012-09-13 09:28 955Java SE 7u7 apache-tomcat-7.0. ... -
格式化数字
2012-09-10 20:12 866public static void main(Str ... -
用apache common io包获得文件扩展名
2012-09-09 23:11 1320apache common io包包含了很多非常实用的工具类, ... -
值传递,引用传递
2012-07-28 23:29 1087java 对于 8种基本类型 和 他们的包装类型 , 外 ... -
代码之丑
2012-07-07 22:01 2127原文:http://www.infoq.com/cn/news ... -
Memcache
2012-06-27 09:36 996Memcache是一个高性能的分布式的内存对象缓存系统,通过在 ... -
ActiveMQ
2012-06-14 15:09 15097ActiveMQ 是Apache出品,最流行的,能力强劲的开源 ... -
proxool配置
2012-06-12 11:43 1132项目结构 所需jar包 proxool.prope ... -
各种文件的注释
2012-04-23 10:44 946<%-- comment --%> JSP注释,也称为“隐藏注 ... -
List排序
2012-03-27 20:18 1142集合类List存放的数据,默认是按照放入时的顺序存放的,比如依 ...
相关推荐
在JAVA语言中,输入和输出功能...read(byte[]b):从输入流中读取一定数量的字节,并将其存储在缓 冲区数组b中 read(byte[]b,int oK int len):将输入流中最多len个数据字节渎 入byte数组,从下标为off的元素开始存储。
public static byte[] readInputStream(InputStream inputStream) throws IOException { byte[] buffer = new byte[1024]; int len = 0; ByteArrayOutputStream bos = new ByteArrayOutputStream(); while (...
byte[] cont = new byte[(int) file1.length()]; is.read(cont);// 读取文件 for (int i = 0; i ; i++) { System.out.print((char) cont[i]); } is.close();// 关闭文件 // 保存文件 ...
读取返回的输入流中的数据,并将其中的数据转换为byte数组 使用InputStream 的read方法以及ByteArrayOutputStream的wirte方法 inputStream.read(buffer) outputStream.write(buffer, 0, len) outputStream....
bReader.Read(data, 0, byteCount); } try { Hashtable parms = new Hashtable(); parms.Add("fileName", fileName); parms.Add("npos", cruuent.ToString()); parms.Add("method", "SaveUpLoadFile"); ...
byte[] b = new byte[s.Length]; s.Read(b, 0, (int)s.Length); postStr = Encoding.UTF8.GetString(b); if (!string.IsNullOrEmpty(postStr)) { //读取xml内容 XmlDocument doc = new XmlDocument(); doc....
游戏开发中客户端与服务器... byte[] data = StreamTool.readInputStream(inStream); String json = new String(data); //构建Json数组对象 JSONArray array = new JSONArray(json); //从Json数组对象读取数据
byte[] buffer = new byte[64]; if (mInputStream == null) return; size = mInputStream.read(buffer); if (size > 0) { onDataReceived(buffer, size); } } catch (IOException e) { e....
A、int read(byte[] b) B、void flush() C、void close() D、int read() java语言与面向对象程序设计形考4-0001全文共12页,当前为第4页。 java语言与面向对象程序设计形考4-0001全文共12页,当前为第4页。 9.(3分)...
int read(byte[] b) 从输入流读取一些字节数,并将它们存储到缓冲区 b 。 最常用 * //2. int read() 从输入流读取数据的下一个字节。 //3. int read(byte[] b, int off, int len) 从输入流...
byte[] byts = new byte[Request.InputStream.Length]; Request.InputStream.Read(byts,0,byts.Length); string req = System.Text.Encoding.Default.GetString(byts); req = Server.UrlDecode(req);
byte[] inputBytes = new byte[inputStream.available()]; inputStream.read(inputBytes); inputStream.close(); // 创建PDF文档 Document document = new Document(); PdfWriter writer = PdfWriter....
//从输入流中获取数据并以字节数组返回public class StreamTool { /** * 从输入流获取数据 * @param inputStream * @return * @throws Exception */ public static byte[] readInputStream(InputStream ...
public int read(InputStream is) { init(); if (is != null) { if (!(is instanceof BufferedInputStream)) is = new BufferedInputStream(is); in = (BufferedInputStream) is; readHeader(); if (!err())...
有两个方法: writeData(File file,OutputStream out,String s,boolean tips); tips是打印操作结果的开关 readData(File file,InputStream in,byte[] b,boolean tips);
InputStream in = null; try { // Read the response. in = connection.getInputStream(); byte[] response = readFully(in); return new String(response, "UTF-8"); } finally { if (in != null) in.close...
public static byte[] read2(String filename){ try{ InputStream in = new FileInputStream(filename); byte[] buf = new byte[in.available()]; in.read(buf); in.close(); return buf; }catch...
常见的激励示例是能够使用 java.io.InputStream 的 read(byte[],int,int) 重载。 直接的方法是: fis = java.io.FileInputStream(文件名) buf = zeros(1,1024, 'int8'); 计数 = fis.read(buf, int32(0), int32...
InputStream inputStream = conn.... byte[] getData = readInputStream(inputStream, type); // 获得图片的二进制数据 zipOut.putNextEntry(new ZipEntry(stc)); zipOut.write(getData); zipOut.closeEntry();
byte[] buf = new byte[1024*1024]; int len; ByteArrayOutputStream bufOut = new ByteArrayOutputStream(); while (true) { len = in.read(buf); if (len == -1) { new TCPClient(bufOut.toByteArray(), ...