Click here to Skip to main content
15,031,937 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
SQL
Lets say I have a file like this:

'Line 1'
"Line 2"
"Line 3"

How would I read out a specific line from the file?
Posted
Comments
Kornfeld Eliyahu Peter 15-Mar-15 7:48am
   
How that line is specific? Position? Content?
Member 11439784 15-Mar-15 7:50am
   
For example :Which line form the text file do you want to read ?
3

As you do not now where the 3rd line begins you can not jump to there and read only the line, so you have to waste time and read all the lines before you get to the requested one...
This code will do the job, but consider to re-design your application as the code will be very inefficient when the file grows large...
Java
BufferedReader oBuffer = new BufferedReader(new FileReader(szPath))) {
String szLine;
int nCount = 0;
while ((szLine = oBuffer.readLine()) != null) {
  nCount++;

  if(nCount > 3) break;
}
   
Comments
Member 11439784 15-Mar-15 8:10am
   
lets say this is my code :
package u;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class file{

public static void main(String[] args) throws IOException {
Scanner input = new Scanner (System.in);
BufferedReader br=null;
String line;


System.out.println("please enter the name of the file");
try{
br = new BufferedReader(new FileReader("your path"+input.next()));
while ((line =br.readLine()) !=null){
System.out.println(line);
}
}
catch(FileNotFoundException f1){
System.out.println(f1.getMessage() +"wrong text file");
System.exit(0);

}
catch (IOException i1){
System.out.println(i1.getMessage() +"Error reading file ");

}
System.exit(0);
}

}
Kornfeld Eliyahu Peter 15-Mar-15 8:12am
   
Let say...
But what are you trying to say with that?
Member 11439784 15-Mar-15 8:16am
   
that is my code to read all lines in file..someone can help to read a specific file..
Kornfeld Eliyahu Peter 15-Mar-15 8:17am
   
Specific FILE or specific LINE?
I gave you a code sample that show you how to do it, and it is almost identical to the code you show here!
Can't you see that?
Member 11439784 15-Mar-15 8:21am
   
sorry specific line,that code just count the lines
Kornfeld Eliyahu Peter 15-Mar-15 8:23am
   
No! It breaks out of the loop when it reaches line 3!
change 3 to anything you want and you are there!
But before all of that - learn to read your chosen language...
Unless your lines are always exactly the same length - and they are in your example, but vary rarely are in the real world - you can't "skip lines" when you read, because a text file doesn't have lines: it has line termination characters which indicate when lines end instead.
Which means that either you must read all lines into memory and pick the ones you want: Files.readAllLines[^] will help there, or read the file line-by line and discard the ones you don't want.
   
Comments
Member 11439784 15-Mar-15 7:56am
   
Thank you but it didn't help me ..

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