Java Java类
Java.io.LineNumberInputStream 类只是输入流的扩展,提供了一个额外的工具来保存当前行号的记录。
行是以 : '\r' 结尾的字节序列,即回车字符或字符:'\n',或回车字符后面的字符。
宣言 :
public class LineNumberInputStream
extends Reader
构造函数:
LineNumberInputStream(InputStream in) :
Constructs a newline no. stream that reads
it's input from the specified Input Stream.
方法:
- read() : Java.io.LineNumberInputStream.read()从输入流中读取数据的下一个字节。它返回表示 '0 – 255' 范围内的字节的 int 值。它返回“-1”以指示输入流的结束。
句法 :
public int read()
Parameters :
-------
Return :
int value representing the bytes in the range of '0 - 255'.
return -1, indicating end of Input Stream.
Exception:
IOException : in case I/O error occurs
执行 :
Java
// Java program illustrating the working of read() method
import java.io.*;
public class NewClass
{
public static void main(String[] args) throws IOException
{
// LineNumberInputStream & FileInputStream initially null
LineNumberInputStream geekline = null;
FileInputStream geekinput = null;
try{
char c;
int a;
// New InputStream : 'ABC' is created
geekinput = new FileInputStream("ABC.txt");
geekline = new LineNumberInputStream(geekinput);
// read() method returning Bytes of Input Stream as integer
// '-1' indicating to read till end Of Input Stream
while((a = geekline.read()) != -1)
{
// Since read() method returns Integer value
// So, we convert each integer value to char
c = (char)a;
System.out.print(c);
}
}
catch(Exception e)
{
// In case of error
e.printStackTrace();
System.out.println("ERROR Occurs ");
}
finally
{
// Closing the streams, Once the End of Input Stream is reached
if(geekinput != null)
geekinput.close();
if(geekline != null)
geekline.close();
}
}
}
Java
// Java program illustrating the working of getLineNumber() method
import java.io.*;
public class NewClass
{
public static void main(String[] args) throws IOException
{
// LineNumberInputStream & FileInputStream initially null
LineNumberInputStream geekline = null;
FileInputStream geekinput = null;
try
{
char c;
int a, b;
// New InputStream : 'ABC' is created
geekinput = new FileInputStream("ABC.txt");
geekline = new LineNumberInputStream(geekinput);
while((a = geekline.read()) != -1)
{
// So, we convert each integer value to char
c = (char)a;
// Use of getLineNumber() : to print line no.
a = geekline.getLineNumber();
System.out.println(" At line : " + a);
System.out.print(c);
}
a = geekline.getLineNumber();
System.out.println(" at line: " + a);
}
catch(Exception e)
{
// In case of error
e.printStackTrace();
System.out.println("ERROR Occurs ");
}
finally
{
// Closing the streams, Once the End of Input Stream is reached
if(geekinput != null)
geekinput.close();
if(geekline != null)
geekline.close();
}
}
}
Java
// Java program illustrating the working of available() method
import java.io.*;
public class NewClass
{
public static void main(String[] args) throws IOException
{
// LineNumberInputStream & FileInputStream initially null
LineNumberInputStream geekline = null;
FileInputStream geekinput = null;
try{
char c;
int a, b;
// New InputStream : 'ABC' is created
geekinput = new FileInputStream("ABC.txt");
geekline = new LineNumberInputStream(geekinput);
while((a = geekline.read()) != -1)
{
// So, we convert each integer value to char
c = (char)a;
// Use of available method : return no. of bytes that can be read
a = geekline.available();
System.out.println(c + " Bytes available : " + a);
}
}
catch(Exception e)
{
// In case of error
e.printStackTrace();
System.out.println("ERROR Occurs ");
}
finally
{
// Closing the streams, Once the End of Input Stream is reached
if(geekinput != null)
geekinput.close();
if(geekline != null)
geekline.close();
}
}
}
Java
// Java program illustrating the working of setLineNumber() method
import java.io.*;
public class NewClass
{
public static void main(String[] args) throws IOException
{
// LineNumberInputStream & FileInputStream initially null
LineNumberInputStream geekline = null;
FileInputStream geekinput = null;
try{
char c;
int a, b = 0;
// New InputStream : 'ABC' is created
geekinput = new FileInputStream("ABC.txt");
geekline = new LineNumberInputStream(geekinput);
while((a = geekline.read()) != -1)
{
// So, we convert each integer value to char
c = (char)a;
// Use of setLineNumber() : to set the line no.
geekline.setLineNumber(100 + b);
// getLineNumber() : returning the current line no.
a = geekline.getLineNumber();
System.out.println(c + " Line No. Set : " + a);
b++;
}
}
catch(Exception e)
{
// In case of error
e.printStackTrace();
System.out.println("ERROR Occurs ");
}
finally
{
// Closing the streams, Once the End of Input Stream is reached
if(geekinput != null)
geekinput.close();
if(geekline != null)
geekline.close();
}
}
}
Java
// Java program illustrating the working of setLineNumber() method
import java.io.*;
public class NewClass
{
public static void main(String[] args) throws IOException
{
// LineNumberInputStream & FileInputStream initially null
LineNumberInputStream geekline = null;
FileInputStream geekinput = null;
try{
char c;
int a, b = 0;
// New InputStream : 'ABC' is created
geekinput = new FileInputStream("ABC.txt");
geekline = new LineNumberInputStream(geekinput);
while((a = geekline.read()) != -1)
{
// So, we convert each integer value to char
c = (char)a;
// skip() : to skip and discard 'arg' bytes
// Here skip() will skip and discard 3 bytes.
geekline.skip(3);
System.out.println(c);
}
}
catch(Exception e)
{
// In case of error
e.printStackTrace();
System.out.println("ERROR Occurs ");
}
finally{
// Closing the streams, Once the End of Input Stream is reached
if(geekinput != null)
geekinput.close();
if(geekline != null)
geekline.close();
}
}
}
Java
// Java program illustrating the working of read() method
import java.io.*;
public class NewClass
{
public static void main(String[] args) throws IOException
{
// LineNumberInputStream & FileInputStream initially null
LineNumberInputStream geekline = null;
FileInputStream geekinput = null;
try{
char c;
int a;
// New InputStream : 'ABC' is created
geekinput = new FileInputStream("ABC.txt");
geekline = new LineNumberInputStream(geekinput);
// read() method returning Bytes of Input Stream as integer
// '-1' indicating to read till end Of Input Stream
while((a=geekline.read())!=-1)
{
// Since read() method returns Integer value
// So, we convert each integer value to char
c = (char)a;
System.out.print(c);
}
}
catch(Exception e)
{
// In case of error
e.printStackTrace();
System.out.println("ERROR Occurs ");
}
finally
{
// Closing the streams, Once the End of Input Stream is reached
if(geekinput != null)
geekinput.close();
if(geekline != null)
geekline.close();
}
}
}
Java
// Java program illustrating the working of LineNumberInputStream method
// mark() and reset()
import java.io.*;
public class NewClass
{
public static void main(String[] args) throws Exception
{
LineNumberInputStream geekline = null;
FileInputStream geek = null;
try{
geek = new FileInputStream("GEEKS.txt");
geekline = new LineNumberInputStream(geek);
// read() method : reading and printing Characters one by one
System.out.println("Char : " + (char)geekline.read());
System.out.println("Char : " + (char)geekline.read());
System.out.println("Char : " + (char)geekline.read());
// mark() : read limiting the 'geek' input stream
geekline.mark(0);
// skip() : it results in reading of 'e' in G'e'eeks
geekline.skip(1);
System.out.println("skip() method comes to play");
System.out.println("mark() method comes to play");
System.out.println("Char : " + (char)geekline.read());
System.out.println("Char : " + (char)geekline.read());
System.out.println("Char : " + (char)geekline.read());
boolean check = geekline.markSupported();
if(geekline.markSupported())
{
// reset() method : repositioning the stream to marked positions.
geekline.reset();
System.out.println("reset() invoked");
System.out.println("Char : " + (char)geekline.read());
System.out.println("Char : " + (char)geekline.read());
}
else
{
System.out.println("reset() method not supported.");
}
System.out.println("geekline.markSupported() supported reset() : "
+ check);
}
catch(Exception except)
{
// in case of I/O error
except.printStackTrace();
}
finally
{
// releasing the resources back to the GarbageCollector when closes
if(geek != null)
geek.close();
if(geekline != null)
geekline.close();
}
}
}
笔记 :
以下Java代码不会在此处运行,因为我们无法访问在线 IDE 上的任何文件。
因此,将程序复制到您的系统并在那里运行它。
程序中使用的ABC.txt文件包含:
Hello
Geeks.
Explaining
read() method
输出 :
Hello
Geeks.
Explaining
read() method
- getLineNumber() : Java.io.LineNumberInputStream.getLineNumber()返回当前行的编号。
句法 :
public int getLineNumber()
Parameters :
-------
Return :
no. of current line
执行 :
Java
// Java program illustrating the working of getLineNumber() method
import java.io.*;
public class NewClass
{
public static void main(String[] args) throws IOException
{
// LineNumberInputStream & FileInputStream initially null
LineNumberInputStream geekline = null;
FileInputStream geekinput = null;
try
{
char c;
int a, b;
// New InputStream : 'ABC' is created
geekinput = new FileInputStream("ABC.txt");
geekline = new LineNumberInputStream(geekinput);
while((a = geekline.read()) != -1)
{
// So, we convert each integer value to char
c = (char)a;
// Use of getLineNumber() : to print line no.
a = geekline.getLineNumber();
System.out.println(" At line : " + a);
System.out.print(c);
}
a = geekline.getLineNumber();
System.out.println(" at line: " + a);
}
catch(Exception e)
{
// In case of error
e.printStackTrace();
System.out.println("ERROR Occurs ");
}
finally
{
// Closing the streams, Once the End of Input Stream is reached
if(geekinput != null)
geekinput.close();
if(geekline != null)
geekline.close();
}
}
}
笔记 :
以下Java代码不会在此处运行,因为我们无法访问在线 IDE 上的任何文件。
因此,将程序复制到您的系统并在那里运行它。
程序中使用的ABC.txt文件包含:
no. of
lines
输出 :
At line : 0
n At line : 0
o At line : 0
. At line : 0
At line : 0
o At line : 0
f At line : 1
At line : 1
l At line : 1
i At line : 1
n At line : 1
e At line : 1
s at line: 1
- available() : Java.io.LineNumberInputStream.available()返回可以从输入流中读取而不阻塞的字节数。
句法 :
public int available()
Parameters :
-------
Return :
returns the no. of bytes that an be read from the Input Stream.
Exception:
IOException : in case I/O error occurs
执行 :
Java
// Java program illustrating the working of available() method
import java.io.*;
public class NewClass
{
public static void main(String[] args) throws IOException
{
// LineNumberInputStream & FileInputStream initially null
LineNumberInputStream geekline = null;
FileInputStream geekinput = null;
try{
char c;
int a, b;
// New InputStream : 'ABC' is created
geekinput = new FileInputStream("ABC.txt");
geekline = new LineNumberInputStream(geekinput);
while((a = geekline.read()) != -1)
{
// So, we convert each integer value to char
c = (char)a;
// Use of available method : return no. of bytes that can be read
a = geekline.available();
System.out.println(c + " Bytes available : " + a);
}
}
catch(Exception e)
{
// In case of error
e.printStackTrace();
System.out.println("ERROR Occurs ");
}
finally
{
// Closing the streams, Once the End of Input Stream is reached
if(geekinput != null)
geekinput.close();
if(geekline != null)
geekline.close();
}
}
}
笔记 :
以下Java代码不会在此处运行,因为我们无法访问在线 IDE 上的任何文件。
因此,将程序复制到您的系统并在那里运行它。
程序中使用的ABC.txt文件包含:
available
输出 :
a Bytes available : 4
v Bytes available : 3
a Bytes available : 3
i Bytes available : 2
l Bytes available : 2
a Bytes available : 1
b Bytes available : 1
l Bytes available : 0
e Bytes available : 0
- setLineNumber() : Java.io.LineNumberInputStream.setLineNumber(int arg)将行号分配给我们想要的参数。
句法 :
public void setLineNumber(int arg)
Parameters :
arg : line number to assign
Return :
void
Exception:
-----
执行 :
Java
// Java program illustrating the working of setLineNumber() method
import java.io.*;
public class NewClass
{
public static void main(String[] args) throws IOException
{
// LineNumberInputStream & FileInputStream initially null
LineNumberInputStream geekline = null;
FileInputStream geekinput = null;
try{
char c;
int a, b = 0;
// New InputStream : 'ABC' is created
geekinput = new FileInputStream("ABC.txt");
geekline = new LineNumberInputStream(geekinput);
while((a = geekline.read()) != -1)
{
// So, we convert each integer value to char
c = (char)a;
// Use of setLineNumber() : to set the line no.
geekline.setLineNumber(100 + b);
// getLineNumber() : returning the current line no.
a = geekline.getLineNumber();
System.out.println(c + " Line No. Set : " + a);
b++;
}
}
catch(Exception e)
{
// In case of error
e.printStackTrace();
System.out.println("ERROR Occurs ");
}
finally
{
// Closing the streams, Once the End of Input Stream is reached
if(geekinput != null)
geekinput.close();
if(geekline != null)
geekline.close();
}
}
}
笔记 :
以下Java代码不会在此处运行,因为我们无法访问在线 IDE 上的任何文件。
因此,将程序复制到您的系统并在那里运行它。
程序中使用的ABC.txt文件包含:
LineNumber
输出 :
L Line No. Set : 100
i Line No. Set : 101
n Line No. Set : 102
e Line No. Set : 103
N Line No. Set : 104
u Line No. Set : 105
m Line No. Set : 106
b Line No. Set : 107
e Line No. Set : 108
r Line No. Set : 109
- skip() : Java.io.LineNumberInputStream.skip(long arg)从输入流数据中跳过并丢弃“arg”字节。 LineNumberInputStream 的 skip 方法创建一个字节数组,然后重复读取它,直到读取 n 个字节或到达流的末尾。
句法 :
public long skip(long arg)
Parameters :
arg : no. of bytes of Input Stream data to skip.
Return :
no. of bytes to be skipped
Exception:
IOException : in case I/O error occurs
执行 :
Java
// Java program illustrating the working of setLineNumber() method
import java.io.*;
public class NewClass
{
public static void main(String[] args) throws IOException
{
// LineNumberInputStream & FileInputStream initially null
LineNumberInputStream geekline = null;
FileInputStream geekinput = null;
try{
char c;
int a, b = 0;
// New InputStream : 'ABC' is created
geekinput = new FileInputStream("ABC.txt");
geekline = new LineNumberInputStream(geekinput);
while((a = geekline.read()) != -1)
{
// So, we convert each integer value to char
c = (char)a;
// skip() : to skip and discard 'arg' bytes
// Here skip() will skip and discard 3 bytes.
geekline.skip(3);
System.out.println(c);
}
}
catch(Exception e)
{
// In case of error
e.printStackTrace();
System.out.println("ERROR Occurs ");
}
finally{
// Closing the streams, Once the End of Input Stream is reached
if(geekinput != null)
geekinput.close();
if(geekline != null)
geekline.close();
}
}
}
笔记 :
以下Java代码不会在此处运行,因为我们无法访问在线 IDE 上的任何文件。
因此,将程序复制到您的系统并在那里运行它。
程序中使用的ABC.txt文件包含:
Program
Explaining
Skip() method
输出: ”
P
r
E
a
n
k
)
t
- read() : Java.io.LineNumberInputStream.read(byte[] buffer, int offset, int maxlen)从输入流中读取最多 'maxlen' 个字节到字节中。
句法 :
public int read(byte[] buffer, int offset, int maxlen)
Parameters :
buffer : buffer whose data to read
offset : starting offset of the data
maxlen : max. no. of bytes to read
Return :
total no. of bytes, else return -1 if End of Input Stream is identified
Exception:
IOException : in case I/O error occurs
执行 :
Java
// Java program illustrating the working of read() method
import java.io.*;
public class NewClass
{
public static void main(String[] args) throws IOException
{
// LineNumberInputStream & FileInputStream initially null
LineNumberInputStream geekline = null;
FileInputStream geekinput = null;
try{
char c;
int a;
// New InputStream : 'ABC' is created
geekinput = new FileInputStream("ABC.txt");
geekline = new LineNumberInputStream(geekinput);
// read() method returning Bytes of Input Stream as integer
// '-1' indicating to read till end Of Input Stream
while((a=geekline.read())!=-1)
{
// Since read() method returns Integer value
// So, we convert each integer value to char
c = (char)a;
System.out.print(c);
}
}
catch(Exception e)
{
// In case of error
e.printStackTrace();
System.out.println("ERROR Occurs ");
}
finally
{
// Closing the streams, Once the End of Input Stream is reached
if(geekinput != null)
geekinput.close();
if(geekline != null)
geekline.close();
}
}
}
笔记 :
以下Java代码不会在此处运行,因为我们无法访问在线 IDE 上的任何文件。
因此,将程序复制到您的系统并在那里运行它。
程序中使用的ABC.txt文件包含:
Read() method
该方法所做的是 offset = r 和 maxlen = 5... 所以 — 即 3 个偏移量,然后是 5 个字节,即 Read(,然后又是偏移量,所以 —
输出 :
The number of char read: 5
---Read(--
- mark() : Java.io.LineNumberInputStream.mark(int arg)标记输入流的当前位置。它设置readlimit,即在标记位置无效之前可以读取的最大字节数。
句法 :
public void mark(int arg)
Parameters :
arg : integer specifying the read limit of the input Stream
Return :
void
- reset() : Java.io.LineNumberInputStream.reset()由 mark() 方法调用。它将输入流重新定位到标记的位置。
句法 :
public void reset()
Parameters :
----
Return :
void
Exception :
-> IOException : If I/O error occurs.
Java程序解释 LineNumberInputStream 类方法:reset() 和 mark()
Java
// Java program illustrating the working of LineNumberInputStream method
// mark() and reset()
import java.io.*;
public class NewClass
{
public static void main(String[] args) throws Exception
{
LineNumberInputStream geekline = null;
FileInputStream geek = null;
try{
geek = new FileInputStream("GEEKS.txt");
geekline = new LineNumberInputStream(geek);
// read() method : reading and printing Characters one by one
System.out.println("Char : " + (char)geekline.read());
System.out.println("Char : " + (char)geekline.read());
System.out.println("Char : " + (char)geekline.read());
// mark() : read limiting the 'geek' input stream
geekline.mark(0);
// skip() : it results in reading of 'e' in G'e'eeks
geekline.skip(1);
System.out.println("skip() method comes to play");
System.out.println("mark() method comes to play");
System.out.println("Char : " + (char)geekline.read());
System.out.println("Char : " + (char)geekline.read());
System.out.println("Char : " + (char)geekline.read());
boolean check = geekline.markSupported();
if(geekline.markSupported())
{
// reset() method : repositioning the stream to marked positions.
geekline.reset();
System.out.println("reset() invoked");
System.out.println("Char : " + (char)geekline.read());
System.out.println("Char : " + (char)geekline.read());
}
else
{
System.out.println("reset() method not supported.");
}
System.out.println("geekline.markSupported() supported reset() : "
+ check);
}
catch(Exception except)
{
// in case of I/O error
except.printStackTrace();
}
finally
{
// releasing the resources back to the GarbageCollector when closes
if(geek != null)
geek.close();
if(geekline != null)
geekline.close();
}
}
}
笔记 :
此代码不会在在线 IDE 上运行,因为此处不存在此类文件。
您可以在您的系统上运行此代码以检查工作情况。
代码中使用的ABC.txt文件有
HelloGeeks
输出 :
Char : H
Char : e
Char : l
skip() method comes to play
mark() method comes to play
Char : o
Char : G
Char : e
reset() method not supported.
geekline.markSupported() supported reset() : false