Java中的Java.io.LineNumberReader类
Java I/O总结
Java中的Java.io.LineNumberReader类
一个缓冲的字符输入流,用于跟踪行号。这个类定义了方法setLineNumber(int)和getLineNumber(),分别用于设置和获取当前行号。
- 默认情况下,行编号从0开始。随着数据被读取,此编号在每行终止符处递增,并且可以通过调用setLineNumber(int)来更改。
- 但是请注意,setLineNumber(int)实际上并不改变流中的当前位置; 它只会改变getLineNumber()返回的值。
- 换行符被换行符('\ n'),回车符('\ r')或回车符后面的换行符中的任何一个结束。
构造函数:
- LineNumberReader(Reader in):使用默认的输入缓冲区大小创建一个新的行号读取器。
- LineNumberReader(Reader in,int sz):创建一个新的行号读取器,将字符读入给定大小的缓冲区。
方法 :
- int getLineNumber():获取当前行号。
语法: public int getLineNumber() 返回: 当前行号
- void mark(int readAheadLimit):标记流中的当前位置。随后对reset()的调用将尝试将流重新定位到此点,并且还会适当地重置行号。
语法: public void mark(int readAheadLimit) throws IOException 参数: readAheadLimit - 限制可读取的字符数 同时仍保留该标记。 抛出: IOException
- int read():读取单个字符。行结束符被压缩成单个换行符('\ n')字符。无论何时读取行终止符,当前行号都会递增。
语法: public int read() throws IOException 返回: 读取的字符,如果已到达流的末尾,则返回-1 抛出: IOException
- int read(char [] cbuf,int off,int len):将字符读入数组的一部分。每当读取行结束符时,当前行号都会增加。
语法: public int read(char [] cbuf, int off, int len) throws IOException 参数: cbuf - 目标缓冲区 off - 开始存储字符的偏移量 len - 要读取的最大字符数 返回: 读取的字节数,如果已到达流的末尾,则为-1 抛出: IOException
- String readLine():读取一行文本。每当读取行结束符时,当前行号都会增加。
语法: public String readLine() throws IOException 返回: 包含行内容的字符串,不包含任何行 终止字符;如果已到达流的末尾,则返回null 抛出: IOException
- void reset():将流重置为最近的标记。
语法: public void reset() throws IOException 抛出: IOException
- void setLineNumber(int lineNumber):设置当前行号。
语法: public void setLineNumber(int lineNumber) 参数: lineNumber - 指定行号的整数
- 长跳过(长整数n):跳过字符。
语法: public long skip(long n) throws IOException 参数: n - 要跳过的字符数 返回: 实际跳过的字符数 抛出: IOException 抛出:IllegalArgumentException
示例:
//Java program demonstrating LineNumberReader methods import java.io.FileReader; import java.io.IOException; import java.io.LineNumberReader; class LineNumberReaderDemo { public static void main(String[] args) throws IOException { FileReader fr = new FileReader("file.txt"); LineNumberReader lnr = new LineNumberReader(fr); char c[] = new char[20]; //illustrating setLineNumber() lnr.setLineNumber(0); //illustrating set System.out.println(lnr.getLineNumber()); //illustrating markSupported() method if(lnr.markSupported()) { System.out.println("mark() method is supported"); //illustrating mark method lnr.mark(100); } /*File Contents * This is first line this is second line This is third line */ //skipping 19 characters lnr.skip(19); //illustrating ready() method if(lnr.ready()) { //illustrating readLine() method System.out.println(lnr.readLine()); //illustrating read(char c[],int off,int len) lnr.read(c); for (int i = 0; i <20 ; i++) { System.out.print(c[i]); } //illustrating reset() method lnr.reset(); for (int i = 0; i <18 ; i++) { //illustrating read() method System.out.print((char)lnr.read()); } int ch; //illustrating read() method System.out.println(lnr.readLine()); while((ch = lnr.read())==1) System.out.print((char)ch); } //close the stream lnr.close(); } }
输出:
0 mark() method is supported this is second line This is third line This is first lineJava I/O总结
Java的相关文章
![]() |
微信返利机器人 | ![]() |
免费:淘宝,京东,拼多多优惠券 | ||
腾讯,爱奇艺,优酷的VIP视频免费解析,免费看 | ||
即刻扫描二维码,添加微信机器人! |
发表笔记