• 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

Handling of back slash (\) while writing to a file.

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I need to read certain fileds from database and write them to a .txt file of fixed line length. Each column of data is fit to a fixed length either by truncating or by padding accordingly. But when by chance back slash (\) is part of data I read, after writing to a file, one character decreases as back slash goes off. It results in deformatting the rest of fields.
Please suggest to overcome this problem. One way I feel is to check for presence of back slash and add one more back slash as escape sequence. But this requires parsing of whole data and file to be written is huge.
Any other suggestions?
Krishna.
 
Ranch Hand
Posts: 1143
1
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Krishna,
A backslash in java is used as an "escape character". For example, "\n" means a new-line character (i.e. a single character) and not two characters (a backslash followed by an "n"). If you want to write the backslash character to your file, then you need to write a double backslash -- "\\" -- then a single backslash will be written to your file.
Hope this answers your question.
Good Luck,
Avi.
 
Krishna Mudda
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Avi,
Thanks for the info. As I mentioned in my problem description, I'm looking for some other solution than using a escape sequence character (Like replacing the every occurence of '\' with '\\'). As this involves processing of huge data.
Please suggest.
Krishna.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand why this would be happening. Are you sure that the data is stored in the database correctly? It's possible that the SQL statement which originally stored the data did not perform a required escape. (Assuming \ has special significance in some forms of SQL - dunno.) One way to avoid needing additional escapes may be to use PreparedStatement rather than Statement to do your insert/updates.
If the data is correct in the database but not after it's been read and written to a file, that's very strange. Try inserting a System.out.println() to display the data after it's been read and before it's been written - this way you can determine if the problem is with the read or the write. Then show us the code that you use to perform the read or write.
Although \ is treated as a special escape characer when it's encountered in a String or char literal in a Java source file which is being compiled, most other methods which read and write do not treat \ as anything special. (SQL to insert or update may be an exception.) I'll be interested to hear where this problem really comes from.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic