• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Modify Newline character in java

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a few text files which has text in the form of Key-Value pairs. The files are in different languages including Japanese & Chinese.
For ex:
StringID1 "String Value1"
StringID2 "String Value2"
StringID3 "String Value3"

In some cases, the last quote is in a new line. i.e:

StringID3 "String Value3
"
This needs to be fixed. I have tried with the following code which reads the file line by line.



Could someone tell me how to solve this?

Thanks in Advance
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Quoting JavaDoc, readLine() returns:

A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached



Try something like:


Note that I haven't tested this code.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And I'm pretty sure I would have done this in Perl, awk, sed, Ruby, Python, Groovy, or... well, just about anything else before doing it in Java, unless it's part of a bigger process (in which case I'd do it as part of that process rather than reading the file twice).

Other than that, I agree--I'd just check to make sure each line ended with a ".
 
Martin Vanyavchich
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:And I'm pretty sure I would have done this in Perl, awk, sed, Ruby, Python, Groovy, or... well, just about anything else before doing it in Java, unless it's part of a bigger process



I second that :wink:
 
Raj S Kumar
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it is part of a bigger process, David.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then why create a new file? Why not fix up each line as part of the overall process? Reading and writing the file is (relatively) expensive.
 
Raj S Kumar
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi David,
I have to modify the contents of an existing file. Without reading and writing it, is there any other way to do it??

Please help me.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think what he means is, if this is part of a larger process, you could either fix the process that creates the file in the first place, or fix the process that reads the file later.
 
Raj S Kumar
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it now Matthew.
Actually, this is part of a larger process. But the files already exist.
Hope this clarifies.
 
You get good luck from rubbing the belly of a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic