• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

file i/o extracting strings from file ..

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
My requirement is that i have a file eg : abc.txt, then I need to search the string 'black box'. let's say that i found 6 occurences of the string black box in my file abc.txt, now after this string black box, I have some text of type:
1, 2323, 9849438, 539039
2, 4394983, 494938, 4954095
3, 5o343049, 4934, 999349838
... and so on.
here I need to take out all the lines after string black box, which is not fixed there may be 10 or 20 lines. one more thing to note is that I have to only take out those after black box which have the value in next line always incremented by 1.
ie, if the sample file is of type :
black box
1, 3232, 32323, 32323, 54854
2, iuriu, 238372, 68685, 96589
3, uuery, 585, 459584, 59
kklsfjsjisiof
fsifsfsjsjklfsklfj
skfkljsf
black box
4, iuwieuo, wour, 3943434
5, oiieq, 4874, 945848
oritr]iprit]r
trotirtrtoorotirit
trtrtioirt]irtiurtrr
black box
6, iidsdjks, 88343, 95696586
7, iieuiure, 3847, 3943948
8, 73, itr, 849584, kkjfg
9, eirieu,iiuru, woeooiwe

So I don't have to take 'kklsfjsjisiof' after '3, uuery, 585, 459584, 59
also there is no fixed no of lines to expect after black box., it could vary. the only input the user would be providing is the filename and nothing else. so far I have been able to find all the occurences of string blackbox but am stuck after that.
Regards
shalu
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
try this code. U can extract the data..
public void String parseData() throws IOException
{
String str ;
String stp ;
while((str = buf.readLine()) != null ) // buf, BufferedReader object to read from the file
{
if(str.trim().equals("black box"))
{
while((stp = buf.readLine()) != null && (stp.indexOf(",")) != -1)
sbuf.append(stp+"\n");// sbuf, StringBuffer object
}
}
return sbuf.toString();
}
Regards
Nayan
 
Alas, poor Yorick, he knew this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic