例子
以下示例显示java.io.LineNumberInputStream.available()方法的用法。
package com.jc2182;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.LineNumberInputStream;
public class LineNumberInputStreamDemo {
public static void main(String[] args) throws IOException {
LineNumberInputStream lnis = null;
FileInputStream fis = null;
int i,j;
char c;
try {
// create new input streams
fis = new FileInputStream("C:/test.txt");
lnis = new LineNumberInputStream(fis);
while((i = lnis.read())!=-1) {
// converts int to char
c = (char)i;
// prints char
System.out.println("Character read: "+c);
j = lnis.available();
System.out.println("Available bytes: "+j);
}
} catch(Exception e) {
// if any error occurs
e.printStackTrace();
} finally {
// closes the stream and releases any system resources
if(fis!=null)
fis.close();
if(lnis!=null)
lnis.close();
}
}
}
假设我们有一个文本文件c:/test.txt ,其内容如下。该文件将用作示例程序的输入-
让我们编译并运行以上程序,这将产生以下结果-
Character read: A
Available bytes: 2
Character read: B
Available bytes: 1
Character read: C
Available bytes: 1
Character read: D
Available bytes: 0
Character read: E
Available bytes: 0