Click here to watch in Youtube :
Click the below Image to Enlarge
Java Tutorial : Java IO (LineNumberReader) |
Java Tutorial : Java IO (LineNumberReader) |
Java Tutorial : Java IO (LineNumberReader) |
Welcome
Hello
Bye
LineNumberReaderDemo.javaimport java.io.FileReader;
import java.io.LineNumberReader;
public class LineNumberReaderDemo
{
public static void main(String[] args) throws Exception
{
LineNumberReader lineNumberReader = null;
FileReader fileReader = null;
try
{
fileReader = new FileReader("myfile.txt");
lineNumberReader = new LineNumberReader(fileReader);
String line;
while ((line = lineNumberReader.readLine()) != null)
{
int lineNumber = lineNumberReader.getLineNumber();
System.out.println(lineNumber + " = " + line);
}
}
finally
{
if (lineNumberReader != null)
{
/*
* When you are finished reading characters
* from the LineNumberReader you should
* remember to close it. Closing a
* LineNumberReader will also close the
* Reader instance from which the
* LineNumberReader is reading.
*/
lineNumberReader.close();
}
}
}
}
Output1 = Welcome
2 = Hello
3 = Bye
Refer:https://docs.oracle.com/javase/8/docs/api/index.html?java/io/LineNumberReader.html
No comments:
Post a Comment