Solved

how to find the specific line of a file

Posted on 2011-03-17
5
207 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
Comment Utility
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
Comment Utility


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
Comment Utility

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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bunnyEars challenge 6 62
json example 39 110
wordmultiple challenge 12 89
java. non-English characters encoding problem. intellij idea 3 40
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now