3

I have a created text file in unix enviroment using java code.

For writing the text file i am using java.io.FileWriter and BufferedWriter. and for newline after each row i am using bw.write("\r\n") method. (where bw is object of BufferedWriter) and sending that text file by attaching in mail from unix environment itself to other environments like(mainframe,windows)

My issue is, if my client's download the text file in Mainframe system, they found that in text file a "special character"(like small rectangular box) presenting and data not properly aligned.

bw.write("\r\n") is not working i think so..(but working fine in windows).

I want same text file alignment as it is in unix environment and without any special
character symbol too, if they opened the text file in mainframe ,windows environments or any other enviroments.

How to resolve the problem. Thanks for your help in advance.

pasting my piece of java code here for your reference..(running java code in unix
environment)

File f = new File(strFileGenLoc);
BufferedWriter bw = new BufferedWriter(new FileWriter(f, false));
rs = stmt.executeQuery("select * from jpdata");
while ( rs.next() ) {
   bw.write(rs.getString(1)==null? "":rs.getString(1));
   bw.newLine();
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Manu
  • 3,179
  • 23
  • 57
  • 69

2 Answers2

7

Contrary to your claim, your code clearly isn't writing "\r\n" after each line. It's calling BufferedWriter.newLine() which uses the current platform's default line separator (unless you explicitly set the line.separator system property). On Unix systems this will be "\n".

Now, you need to think about what you want the result to be on the target systems where it will be read. For Windows, that means you should explicitly write "\r\n"... I don't know about the mainframes you mention though - you'd need to give more details.

Note that the fact that there are different line separators available means that no one text file will be universally suitable. Many text editors on different platforms cope with any line separator... but there will always be some that don't :(

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

To write a file with environmentally correct line endings, the recommended class is PrintWriter. The println(String) method will output the given string and the appropriate line ending to the output file.


Jon Skeet is right, though: Your problem isn't so much technical as figuring out which file format you need on which system. If a file is processed on a Windows system, it is expected that lines end in \r\n; on Unix systems, just \n.

All this is complicated even more when you use FTP to transfer files between different systems: ASCII mode will automatically translate line endings for you, binary will not.

Email is also not kind to text formats: Outlook frequently mutilates text files sent across system boundaries.

People here on SO know about this kind of stuff. Give us some more details to work with and we can propose solutions. But it will likely not be possible to write a file on one system that will be correctly read on any system.

Carl Smotricz
  • 66,391
  • 18
  • 125
  • 167
  • PrintWriter is nasty - it swallows exceptions, which is a very bad idea. Personally I would suggest *explicitly* writing the line separator if the one you want isn't the default for the platform you're running on. (I'm not keen on setting a system property for the sake of this. Just be explicit.) – Jon Skeet Jul 14 '10 at 16:38