Click here to Skip to main content
15,906,335 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi , I have a file which i am reading line by line.for e.g
Java

Java
package com.java2novice.files;
 
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
 
public class ReadLinesFromFile {
 
    public static void main(String a[]){
        BufferedReader br = null;
        String strLine = "";
        try {
            br = new BufferedReader( new FileReader("fileName"));
            while( (strLine = br.readLine()) != null){
                System.out.println(strLine);
            }
        } catch (FileNotFoundException e) {
            System.err.println("Unable to find the file: fileName");
        } catch (IOException e) {
            System.err.println("Unable to read the file: fileName");
        }
    }
}


I want to extract a single line from the file , if the line contains "Lets C++".
i.e if a line contains "Lets C++", store the entire line in a string.
Can anyone help me in getting this.

Thanks in advance
Posted
Comments
Sergey Alexandrovich Kryukov 21-Jan-16 11:06am    
How about reading all the file into the array of lines or digest it all in some highly usable data structure? Is the file too big for that?

One little problem is: the lines have different lengths, so you don't know the location of each line in advance, before you read the whole file. So, another solution would be reading the whole file preliminarily, to digest it into the information on how to locate each line. This data structure could be further used to read any of those lines on demand.

—SA

 
Share this answer
 
What you need is change below

C#
while( (strLine = br.readLine()) != null){
               System.out.println(strLine);
           }


TO

C#
while( (strLine = br.readLine()) != null){
              if( strLine.toLowerCase().contains("Lets C++".toLowerCase())
             {
                  System.out.println(strLine);
             }
          }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900