当前位置: 技术问答>java相关
关于读取文本内容的问题
来源: 互联网 发布时间:2015-01-19
本文导语: 请问大家,如何实现这样的功能,就是要将一个文本文件里的内容逐行读出,然后打印在屏幕上。 还请大家帮忙了,谢谢! | java.io.RandomAccessFile类readLine方法 可以这么做: RandomAccessFile file=n...
请问大家,如何实现这样的功能,就是要将一个文本文件里的内容逐行读出,然后打印在屏幕上。
还请大家帮忙了,谢谢!
还请大家帮忙了,谢谢!
|
java.io.RandomAccessFile类readLine方法
可以这么做:
RandomAccessFile file=new RandomAccessFile("file.txt","rw");
String line=new String();
while ((line=file.readLine()!=null)
{
System.out.print(line);
}
以下是jdk的api文档的解释
readLine
public final String readLine()
throws IOException
Reads the next line of text from this file. This method successively reads bytes from the file, starting at the current file pointer, until it reaches a line terminator or the end of the file. Each byte is converted into a character by taking the byte's value for the lower eight bits of the character and setting the high eight bits of the character to zero. This method does not, therefore, support the full Unicode character set.
A line of text is terminated by a carriage-return character ('r'), a newline character ('n'), a carriage-return character immediately followed by a newline character, or the end of the file. Line-terminating characters are discarded and are not included as part of the string returned.
This method blocks until a newline character is read, a carriage return and the byte following it are read (to see if it is a newline), the end of the file is reached, or an exception is thrown.
Specified by:
readLine in interface DataInput
Returns:
the next line of text from this file, or null if end of file is encountered before even one byte is read.
Throws:
IOException - if an I/O error occurs.
可以这么做:
RandomAccessFile file=new RandomAccessFile("file.txt","rw");
String line=new String();
while ((line=file.readLine()!=null)
{
System.out.print(line);
}
以下是jdk的api文档的解释
readLine
public final String readLine()
throws IOException
Reads the next line of text from this file. This method successively reads bytes from the file, starting at the current file pointer, until it reaches a line terminator or the end of the file. Each byte is converted into a character by taking the byte's value for the lower eight bits of the character and setting the high eight bits of the character to zero. This method does not, therefore, support the full Unicode character set.
A line of text is terminated by a carriage-return character ('r'), a newline character ('n'), a carriage-return character immediately followed by a newline character, or the end of the file. Line-terminating characters are discarded and are not included as part of the string returned.
This method blocks until a newline character is read, a carriage return and the byte following it are read (to see if it is a newline), the end of the file is reached, or an exception is thrown.
Specified by:
readLine in interface DataInput
Returns:
the next line of text from this file, or null if end of file is encountered before even one byte is read.
Throws:
IOException - if an I/O error occurs.