Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes I/O and Streams and the fly likes Handling of back slash (\) while writing to a file. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Handling of back slash (\) while writing to a file." Watch "Handling of back slash (\) while writing to a file." New topic

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

Krishna Mudda

Joined: Jun 20, 2001
Posts: 10
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?
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1141

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,
Krishna Mudda

Joined: Jun 20, 2001
Posts: 10
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.
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
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.

"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: Handling of back slash (\) while writing to a file.
It's not a secret anymore!