• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 218
  • Last Modified:

how to find the specific line of a file

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
Tolgar
Asked:
Tolgar
  • 5
1 Solution
 
for_yanCommented:
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
 
for_yanCommented:


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
 
for_yanCommented:

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
 
for_yanCommented:
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
 
for_yanCommented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now