- 浏览: 434564 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
wjy20150716:
我遇到的问题,删除哪个都不行,最后实在没办法了,就用了Tomc ...
Unable to read TLD “META-INF/c.tld” from JAR file的 -
lijun123:
对新手来说不错
Oracle新建用户,授权,建表空间 -
netkongjian:
欢迎加入程序员网址导航[deyi]
[Z]浅析中国五大网址导航!!! -
iijjll:
<div class="quote_title ...
Unable to read TLD “META-INF/c.tld” from JAR file的 -
iijjll:
oh,yeah! get it。问题解决了!
Unable to read TLD “META-INF/c.tld” from JAR file的
相信BufferedReader应该是大家所熟悉的一个操作类,但是其中的mark,reset方法,不知大家是否有过关注,
近日工作中碰到问题,不解,所以就Google并记录下来,给自己个记录,也希望与大家分享。
关于BufferedReader:
extends Reader
Read text from a character - input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.
The buffer size may be specified, or the default size may be used. The default is large enough for most purposes.
In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream. It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders. For example,
BufferedReader in
= new BufferedReader( new FileReader( " foo.in " ));
will buffer the input from the specified file. Without buffering, each invocation of read() or readLine() could cause bytes to be read from the file, converted into characters, and then returned, which can be very inefficient.
Programs that use DataInputStreams for textual input can be localized by replacing each DataInputStream with an appropriate BufferedReader.
Since:
JDK1. 1
See Also:
FileReader, InputStreamReader
关于它的mark,reset方法:
public void mark( int readAheadLimit)
throws IOException
Mark the present position in the stream. Subsequent calls to reset() will attempt to reposition the stream to this point.
Overrides:
mark in class Reader
Parameters:
readAheadLimit - Limit on the number of characters that may be read while still preserving the mark. After reading this many characters, attempting to reset the stream may fail. A limit value larger than the size of the input buffer will cause a new buffer to be allocated whose size is no smaller than limit. Therefore large values should be used with care.
Throws:
IllegalArgumentException - If readAheadLimit is < 0
IOException - If an I / O error occurs
public void reset()
throws IOException
Reset the stream to the most recent mark.
Overrides:
reset in class Reader
Throws:
IOException - If the stream has never been marked, or if the mark has been invalidated
在项目中有如下代码:
{
String string = "" ;
try
{
br.mark( 9 );
int charVal = br.read();
while (charVal != ' < ' && ! isFileEnd(br))
{
if (charVal == ' \r ' ){
currentLineNo ++ ;
}
string += ( char )charVal;
br.mark( 9 );
charVal = br.read();
}
br.reset();
if (isFileEnd(br) && charVal > 0 )
{
string += ( char )charVal;
}
return (string);
}
catch (IOException ioe)
{
Message.show(Message.error, ioe.getMessage());
return ( null );
}
}
其功能是:在html文件解析中,读取当前BufferedReader至第一个tag。
其实,BufferedReader的功能是有很多用处的,比如统计文件行数,在html中读取发现tag后再将文件指针返回指向tag前面的位置;
1.在上面的code中,我查阅后(原作者已离职)的理解是:此处就是要在当前处mark一下,读取下一个char后,判断是否'<'
,重复画线处,直到发现'<',然后返回tag前的文本;则此处的9(就是这个9害我思索许久),不一定是9,可以是8,7...2,后面只读一次就又mark了; 1不行(后面讨论)。
2.在文件读取中,使用mark方法时,要注意,要设置mark参数int readAheadLimit=file.length + 1,否则就会爆出异常java.io.IOException: Mark invalid.
原因在于:
jdk中声明:
readAheadLimit - Limit on the number of characters that may be read while still preserving the mark. After reading this many characters
,
attempting to reset the stream may fail. A limit value larger than the
size of the input buffer will cause a new buffer to be allocated whose
size is no smaller than limit. Therefore large values should be used
with care.
英文声明可能有些confused,来看中文的:
readAheadLimit - 在仍保留该标记的情况下,对可读取字符数量的限制。在读取达到或超过此限制的字符
后,尝试重置流可能会失败。限制值大于输入缓冲区的大小将导致分配一个新缓冲区,其大小不小于该限制值。因此应该小心使用较大的值。 //就是建议使用大于最大值的值
给大家一段代码可以参考运行:
import java.io.CharArrayReader;
import java.io.IOException;
public class BufferedReaderDemo {
public static void main(String[] args) throws IOException {
String s = " Message.show(Message.error, ioe.getMessage()).一 " ;
char buf[] = new char [s.length()];
s.getChars( 0 , s.length(), buf, 0 );
CharArrayReader in = new CharArrayReader(buf);
BufferedReader f = new BufferedReader(in);
String d = "" ;
int c;
System.out.println(s.length() );
f.mark(s.length() + 1 );
while ((c = f.read()) != - 1 ) {
d += ( char )c;
}
f.reset();
System.out.println(d);
}
}
发表评论
-
《Social Network》中扎克伯格所做的美女照片对比选美的实现原理
2010-11-03 10:10 1382看了《Social Network》,在开场不久后,扎克伯格在 ... -
【转】谈谈Unicode编码,简要解释UCS、UTF、BMP、BOM等名词
2010-05-12 16:31 1316这是一篇程序员写给程序员的趣味读物。所谓趣味是指可以比较轻松地 ... -
分类读入、解析XML
2010-05-12 16:28 2322在最近的一个项目,经 ... -
JAVA可使用双括号进行初始化
2010-03-31 11:24 0形如 private static final Set< ... -
HTTPS的实现及其原理
2010-03-26 13:35 10472在以前的一个项目中,需要使用到SSL得加密链接访问,所以对HT ... -
【转】【share】设计模式快速参考
2010-03-26 12:01 1009从网上找到的一个设计模式快速参考,感觉做的非常的好,分 ... -
去除UTF-8文件的BOM的方法
2010-03-01 14:49 2082使用UltraEdit,点击“文件-另存为”,格式选为“UTF ... -
[Z]字符编码笔记:ASCII,Unicode和UTF-8
2009-12-30 10:00 945我 们知道,在计算机内部,所有的信息最终都表示为一个二进 ... -
MAVEN使用
2009-11-13 17:38 727maven的定义、安装、设置就不说了,粘贴一大篇从官网或各处搜 ... -
J2EE/J2SE/J2ME的概念区别
2009-11-05 10:02 2340忽然觉得对J2EE/J2SE/J2ME的概念有些模糊,记忆过的 ... -
RTSP协议简介
2009-10-28 16:21 3086Real Time Streaming Proto ... -
接口 BlockingQueue<E>
2009-04-23 15:01 1591java.util.concurrent 接口 ... -
java中byte转换int时为何与0xff进行与运算
2009-04-20 13:07 3063在剖析该问题前请看如 ... -
Dead store
2008-09-12 17:43 2072In Computer programming , if we ... -
JSP/Servlet的重定向技术综述
2008-08-13 09:20 1267由于response是jsp页面中的隐含对象,故在jsp页 ... -
ServletConfig,ServletContext讲解
2008-08-01 17:35 2794在编写servlet过程中,需要用到 ServletConfi ... -
Transient 关键字用法
2008-05-22 14:45 80461、 java的serialization提供了一个非常 ... -
java的几种对象(PO,VO,DAO,BO,POJO)解释
2008-05-07 11:24 1043一、PO:persistant object 持久对象, ... -
Java Servlet基本方法介绍
2007-03-09 11:49 1015CSDN - 文档 ... -
java 面试中的一道编写一个截取字符串的函数
2007-03-09 16:01 2289package com.heping.xx; //7、编程:编 ...
相关推荐
详解BufferedReader详解BufferedReader详解BufferedReader详解BufferedReader详解BufferedReader详解BufferedReader详解BufferedReader详解BufferedReader详解BufferedReader详解BufferedReader详解BufferedReader
BufferedReader 开发的结构
文件内容的读取 BufferedReader
BufferedReader 是缓冲字符输入流。它继承于Reader。 BufferedReader 的作用是为其他字符输入流添加一些缓冲功能。
BufferedReader的用法---马克-to-win java视频缓存输入流的介绍
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int f = Integer.parseInt(br.readLine()); double c=5*(f-32); c=c/9; System.out.println("Changing it to Celsius is "+c); } }
没有堵塞,客户端和服务端简单的信息传递,利用了BufferedReader读。
NULL 博文链接:https://lisanlai.iteye.com/blog/673253
文件读写 BufferedReader BufferedWriter 去除代码后面空格 简单 readLine newLine flush
NULL 博文链接:https://chaoyi.iteye.com/blog/2084140
Fibonacci int PrintWriter BufferedWriter FileWriter Integer.parseInt BufferedReader InputStreamReader System.in readLine
NULL 博文链接:https://wushipan-easy.iteye.com/blog/1831047
import java.io.BufferedReader
public class Method { private int intinput() throws IOException,NumberFormatException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String str=br.readLine(); int i=Integer...
jdk api-BufferedReader基础、应用、实战
BufferedReader 是缓冲字符输入流。它继承于Reader。接下来通过本文给大家介绍BufferedReader的相关知识,需要的朋友参考下吧
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));//获取返回信息数据流 StringBuffer sb = new StringBuffer(""); //自定义缓冲,用于存放返回信息数据 String line = ...
BufferedReader与BufferedWriter分别继承于Reader和Writer类,分别为字符的读取和写入添加缓冲功能,这里我们就来看一下Java中BufferedReader与BufferedWriter类的使用示例:
javaIO流实现永久化查询添加学生信息-java-BufferedReader
主要介绍了InputStreamReader和BufferedReader用法及实例讲解的相关资料,需要的朋友可以参考下