• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Java Program to copy selected text from one file to another.

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
copying entire file to another file is one thing but to copy the text between 2 delimiters is different. I have written basic programs to copy one file to another using scanner class but I'm trying to write a program which finds a string say <start> and start writing the text from here to another file till it finds another string say <end>. Then keeps on reading for next start/end strings.
This is how my file would look like -


<SomeOtherstartTimeStamp> <start> some text <end> <start> some other text <end><start> someother text <end><start> some text <end><SomeOtherEndTimeStamp>
<startTimeStamp> <start> some text <end> <start> some other text <end><start> someother text <end><start> some text <end><EndTimeStamp>
<SomeOtherstartTimeStamp> <start> some text <end> <start> some other text <end><start> someother text <end><start> some text <end><SomeOtherEndTimeStamp>
<SomeOtherstartTimeStamp> <start> some text <end> <start> some other text <end><start> someother text <end><start> some text <end><SomeOtherEndTimeStamp>

So I basically need to write everything between <startTimeStamp> and <EndTimeStamp> but broken into logical chunks of <start> and <end>. once a text between start/end is written, insert a blank line and then write next set of text between start/end.

Any help would be appreciated.
Thanks!
 
Marshal
Posts: 76885
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

What is the exact format of the start and end Strings? Can you create a regular expression to find them?
 
Ashutosh Khare
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch

What is the exact format of the start and end Strings? Can you create a regular expression to find them?



This is plain text file (a log basically) which will have constant strings for <start> and <end>. yes I can find them using a regular expression. Apologies if my question is too broad. I'm new to Java and not sure how to make full use of scanner to achieve my purpose.
 
Campbell Ritchie
Marshal
Posts: 76885
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are there any spaces inside the <start> or <stop> tags? Are you sure that the last <start> tag is followed by a <stop> tag? Otherwise you may get an Exception. You can simply make a List of results, and loop with the Scanner until a stop tag is found.That code assumes that successive tokens are separated by whitespace as is the default for Scanner. You can change it to recognise >< as a delimiter if you prefer.

There are ways to make a Scanner find a match with a regex, but I have never tried them. Look through the Scanner documentation.
 
Sheriff
Posts: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could try something like this:

This assumes that there are no "<" characters between <start> and <end>.
 
Ashutosh Khare
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much Guys. I will try out these options and will let you know the result. Thanks!
 
You guys wanna see my fabulous new place? Or do you wanna look at this tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic