Results 1 to 3 of 3
Thread: Read a specific line or a .txt
- 10-18-2011, 12:43 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 56
- Rep Power
- 0
Read a specific line or a .txt
I know how to read all lines:
[CODE]
import java.io.*;
class ReadFile
{
public static void main(String args[])
{
try{
FileInputStream fstream = new FileInputStream("C:\\Users\\Willy\\Desktop\\Auto Generated\\Test.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
System.out.println (strLine);
}
in.close();
}catch (Exception e){//Catch exception if any
}
}
} [CODE]
But how could I read...a specific line
Like line 3;
or line 1;
- 10-18-2011, 12:55 AM #2
Member
- Join Date
- Oct 2011
- Posts
- 83
- Rep Power
- 0
Re: Read a specific line or a .txt
You have to just read up to that line (i.e. read all the lines before it). I'm 99% sure there is a way to "seek" to a specific offset in a file (though I don't know what that way is off the top of my head; I'd have to look it up), but that would only work to read a specific line if you knew the exact length of all the lines that preceded it.
- 10-18-2011, 02:04 AM #3
Re: Read a specific line or a .txt
It depends upon the situation.
If you simply want to read a specific line N once and nothing else then read n - 1 lines (throw them away) then read and keep the next line.
If you want to read multiple lines in random order then it would be better if you read the entire file into a List then you can access any line you want by using the line number as an index into the List.
Similar Threads
-
Open a URL and read it line by line (Works in Eclipse but not from Command Line)
By rosco544 in forum NetworkingReplies: 16Last Post: 09-17-2011, 02:41 AM -
Is there a way to read a specific line in a txt file (without iterating through)?
By TheNadz in forum New To JavaReplies: 3Last Post: 06-26-2011, 11:00 AM -
How to read file line by line with fixed number of characters
By trkece in forum New To JavaReplies: 1Last Post: 02-13-2011, 03:09 PM -
read a specific line in an input file
By sara12345 in forum Advanced JavaReplies: 7Last Post: 01-03-2010, 10:40 PM -
Need to read an .ini and .abook file line by line (both files contain texts)
By ollyworks in forum Java AppletsReplies: 4Last Post: 09-10-2009, 10:18 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks