How can i read a file from nth line onwards in java

How can i read a file from nth line onwards in java

Hi,

I need to read a file content from its nth line onwards till end. How can i implement it in java.

Regards, Chinnu

View Answers

January 4, 2011 at 5:55 PM

Hi Friend,

Try the following code:

import java.io.*;
import java.util.*;

public class ReadNthLineToEnd {
          public static void main(String[] args) {
              StringBuffer buffer=new StringBuffer();
                String line = "";
                int lineNo;
                Scanner input=new Scanner(System.in);
                System.out.print("Enter Line Number: ");
                int no=input.nextInt();
                try {
                    LineNumberReader ln = new LineNumberReader(new FileReader("C:/hello.txt"));
                        int count = 0;
                        while (ln.readLine() != null){
                        count++;   
                        }
                        ln.close();
                        FileReader fr = new FileReader("C:/hello.txt");

                        BufferedReader br = new BufferedReader(fr);
                        for (lineNo = 1; lineNo <= count; lineNo++) {
                            if(lineNo==no){
                                for (lineNo = no; lineNo <= count; lineNo++) {
                                       buffer.append(br.readLine());
                                       buffer.append("\n");
                                }
                            }
                            else
                                        br.readLine();
                        }
                } catch (IOException e) {
                        e.printStackTrace();
                }
                System.out.println(buffer.toString());
        }
}

Thanks

Ads









Related Tutorials/Questions & Answers:
How can i read a file from nth line onwards in java
How can i read a file from nth line onwards in java  Hi, I need to read a file content from its nth line onwards till end. How can i implement it in java. Regards, Chinnu
Read file from the Nth line
Read file from the Nth line Java provides several classes and methods to manipulate file. A file reading, is a common operation. Usually a file is to be read from top to bottom but here we are going  to read a file content from
Advertisements
How to read big file line by line in java?
Learn how to write a program in java for reading big text file line by line In this tutorial I will explain you how you can read big file line by line... to read the big file in your java program. For example you have to process some
How do I read a variable from a file in shell script
How do I read a variable from a file in shell script  Hi, In my... to read some of the variables from other file and then use this in spark submit command. How do I read a variable from a file in shell script and then use
Java Read File Line by Line - Java Tutorial
Excel file Using Java Read Specific Line from file Using Java... Java Read File Line by Line - Example code of reading the text file... to write java program to read file line by line. We will use the DataInputStream
How to read a large text file line by line in java?
How to read a large text file line by line in java?  I have been... you kind advice and let's know how to read a large text file line by line in java... memory assigned is also a limit. So, we have decided to read the text file line
how to read file line by line using filereader in java
how to read file line by line using filereader in java  Hi, how to read file line by line using filereader in java? Thanks   Hi...))) { String line; while ((line = br.readLine()) != null
How to Read a file line by line using BufferedReader?
How to Read a file line by line using BufferedReader?  Hello Java... problem is to find the best way to read the file in Java. I just searched the google... at a time very efficiently. View the detailed example and code at Java Read File Line
Read from file java
Read from file java  How to Read from file java? What is the best method for a text file having a size of 10GB. Since i have to process the file one line at a time so tell me the very best method. Thank you
How to Read file line by line in Java program
by line in Java. But there are various ways that can help read a larger file...() method is invocated, they can convert the bytes read from the file...()?. The output of read file line by line in Java is printed on console
java - read file line by line in Java
java - read file line by line in Java  Hi, how one can read a text... file in Java can be read line by line with the help of BufferedReader class... the details at How to read file line by line in Java?   What is the benefits
How do I read a large file quickly in Java?
How do I read a large file quickly in Java?  Hi, I my project I have... the tutorial: Java Read File Line by Line. The Scanner class can also be used... in 1-2 GB in size. What is the best way to read the file efficiently? How do I read
Java read file line by line
Java read file line by line In this section, you will learn how to read a file... are going to read a file line by line. For reading text from a file it's better... class is used to read text from a file line by line using it's readLine method
How can i extract email ids from xls/xlsx file in java?
How can i extract email ids from xls/xlsx file in java?  that's the code..... i tried to extract email id;s from xls file........... but it doesn;t... org.apache.poi.poifs.filesystem.POIFSFileSystem; public class excelread123 { File
Java read lines from file
of reading file line by line in Java. Can any one share me the code for reading...Java read lines from file  Any code example related to Java read lines from file? In my project there is requirement of reading the file line by line
Java Read File Line By Line, Video Tutorial of Java Read File Line By Line
can write code to read a text file in Java line by line". Steps... a program in Java programming language to read a text file line by line. The "Java Read File Line by line" is most searched topics by the Java
Java - How to read a string line per line
(System.getProperty("line.separator")); Read more at: Java Read File Line by Line Java Read File Line By Line, Video Tutorial Line by Line reading from...Java - How to read a string line per line  Java - How to read
How To Read Integer From Command Line In Java
How To Read Integer From Command Line In Java In this section we will discuss about how an integer can be read through the command line. In Java all... value from the command line. In this example I have created a class named
Java read file line by line - Java Tutorial
; This program reads the bytes from file and display it to the user. Download...:\nisha>java FileRead this is a file C...C:\nisha>javac ReadFile.java C:\nisha>java
How to read a file line by line?
Using Java Read Specific Line from file Using Java... to read the file in Java. Here are few tutorials which you can find helpful... How To Read File In Java with BufferedReader Read
How to read file in java
How to read file in java Java provides IO package to perform reading and writing operations with a file. In this section you will learn how to read a text file line by line  in java. FileInputStream- This class reads bytes from
How To Read String From Command Line In Java
How To Read String From Command Line In Java In this section we will discuss about how string can be read through the command line. In Java command line... while loop to read any number of input from the command line. In while loop I
nio read file line by line
nio read file line by line  Is there any way to read file line by line using the Java nio package? How to use the nio classes to read file line... the BufferedReader class. Check the tutorial Java Read File Line by Line - Java
Want to learn java - How i can learn Java from scratch?
Want to learn java - How i can learn Java from scratch?  Hi, Want to learn java - How i can learn Java from scratch? Give me some guide. Thanks   Want to learn java - How i can learn Java from scratch
How can i download these java related materials from rose india
How can i download these java related materials from rose india  How can i download these java related materials from rose india   Hello... tutorial do you want to download from the site? Please specify it. Thanks
How to read and display data from a .properties file from a jsp page
How to read and display data from a .properties file from a jsp page  I have a .properties file. I have to create a jsp page such that it reads the data from this .properties file and display it in table format. Ex:by using
How to Read a File in Java
How to Read a File in Java? In this section we are going to know, How to read... but reading a character line by line from a file. Now we are going to read a file... can be very inefficient. Example Here is the program to read a file in Java
How To Read File In Java
How To Read File In Java In this section we will discuss about about how data of a file can be read in Java. A file can contain data as bytes, characters, binary data etc. To read a file in Java we can use following of the classes
How do I install a .deb file via the command line?
How do I install a .deb file via the command line?  How do I install a .deb file via the command line?   Hi, You can install it with following command: sudo dpkg -i DEB_PACKAGE Example: sudo dpkg -i packagename.deb
how can i display a pdf file in a jtextarea
how can i display a pdf file in a jtextarea  I need to display a pdf file in a jtextfield or in a jtextarea.Atlest i need to displat it in a jframe.I have a button and while clicking on it ,i need to choose the pdf file and need
Java I/O From the Command Line
Java I/O From the Command Line In this section we will learn about the I/O from the command line in Java. Several times you can see that the programs runs after taking the input from the command line. Java also supports for the command

Ads