Solved

how to find the specific line of a file

Posted on 2011-03-17
5
209 Views
Last Modified: 2012-06-21
Hi,
In Java, I know the path to the file.

Now, I want to find the specific line of this file and assign it to a variable.

The line that I wanna find looks like this:

# SOMEDATEandTIME (SOMETEXT)     SOMEDIGITNUMBER    <a href='http://SOMEPATH/SOMETEXT.html'>SOMETEXT</a>

SOMEDATEandTIME are in this format:  2011/03/04-15:10:20

How can I make it?

Thanks,

0
Comment
Question by:Tolgar
  • 5
5 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35160380
You can read file line by lin and then
find the line where

s.indexOf( "<a href='http://SOMEPATH/SOMETEXT.html'>SOMETEXT</a>") > -1)


0
 
LVL 47

Expert Comment

by:for_yan
ID: 35160389


DataInputStream in = new DataInputStream(new FileInputStream("file_path_nmae"));
String buff = null;
while((buff= in.readline()) != null){
if(buff.indexOf( "<a href='http://SOMEPATH/SOMETEXT.html'>SOMETEXT</a>") > -1)
System.out.println(buff);

}
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35160400

String s;

try
{
DataInputStream in = new DataInputStream(new FileInputStream("file_path_nmae"));
String buff = null;
while((buff= in.readline()) != null){
if(buff.indexOf( "<a href='http://SOMEPATH/SOMETEXT.html'>SOMETEXT</a>") > -1)
System.out.println(buff);
s=buff;

}

in.close();

}catch(Exception ex){
ex.printStackTrace();
}

System.out.println("s: " + s);

This would actully print the last line which matches this pattern

 
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 35160414
corrected (forgot braces in the above):

String s;

try
{
DataInputStream in = new DataInputStream(new FileInputStream("file_path_nmae"));
String buff = null;
while((buff= in.readline()) != null){
if(buff.indexOf( "<a href='http://SOMEPATH/SOMETEXT.html'>SOMETEXT</a>") > -1){
System.out.println(buff);
s=buff;
}

}

in.close();

}catch(Exception ex){
ex.printStackTrace();
}

System.out.println("s: " + s);

This would actully print the last line which matches this pattern
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35160433
If you can have some other lines which have the same
ending but different format of date - then you'll want to add
another clause i the "if" check in order to differentiate between the
two time formats.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
java.io.BufferedReader   readLine()  //returns the entire file ? 4 73
nextBoolean(double p) for Random class 3 40
table example 4 29
servlet filter example 37 63
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question