1

i have a software that creates a file which i send to a bank software that reads the file. But this software is rejecting my file because it says my file does not have CRLF as line separator. Until now, the file was only created on windows machines, and for line separator i use always "\r\n". I searched about it and found out that if i do it on windows, it generates in a different way than generating on linux or mac. But how can i always generate CRLF independent of platform?

UPDATE 1

This is the method i use to create the file, i replace all \r\n for \n and then \n to \r\n, just to ensure that when whoever calls this method passing only \n as line separator, the file will be generated correctly with \r\n:

public static void createFile(String filePath,String content){
    try{
        File parentFile=new File(filePath).getParentFile();
        if(parentFile!=null && !parentFile.exists()){
            parentFile.mkdirs();
        }
        BufferedWriter bw=new BufferedWriter(new FileWriter(filePath));
        bw.write(content.replaceAll("\r\n","\n").replaceAll("\n","\r\n"));
        bw.flush();
        bw.close();
    }catch(Exception e){
        throw new RuntimeException("Error creating file", e);
    }
}
Mateus Viccari
  • 7,389
  • 14
  • 65
  • 101
  • would this generate CRLF both on linux AND windows? – Mateus Viccari Jun 27 '14 at 11:40
  • 1
    no, the system dependent representation. CRLF is always CRLF, no matter which platform it is generated on. – Smutje Jun 27 '14 at 11:41
  • yeah, i need a way to generate CRLF, not system separator. – Mateus Viccari Jun 27 '14 at 11:44
  • 1
    AFAIK `\r\n` _is_ `CRLF`, so is there any chance the software that should read the file doesn't provide the correct message? Is no file accepted or only those generated on linux and mac? If so, did you check the file's encoding? – Thomas Jun 27 '14 at 11:46
  • possible duplicate of [Carriage Return\Line feed in Java](http://stackoverflow.com/questions/2832756/carriage-return-line-feed-in-java) and [CRLF into Java string](http://stackoverflow.com/questions/13821578/crlf-into-java-string) – Pavel Horal Jun 27 '14 at 11:50

1 Answers1

2

The \r\n always prints two characters CR and LF on all platforms. From http://docs.oracle.com/javase/tutorial/java/data/characters.html

\n Insert a newline in the text at this point.
\r Insert a carriage return in the text at this point.

You can test this yourself with

   FileWriter fw = new FileWriter("test");
   fw.write("\r\n");
   fw.close();
   FileInputStream fis = new FileInputStream("test");
   for(int ch; (ch = fis.read())!=-1;) {
       System.out.printf("0x%02x%n", ch);
   }
   fis.close();

run on a Ubuntu machine prints

0x0d
0x0a
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Yeah i ran your test here on the file i created and it shows exactly as you wrote. I updated my question with the code i used to create the file, can you look at it to see if maybe there is something wrong? – Mateus Viccari Jun 27 '14 at 12:52