-2

When I use \r\n in Windows, to print a newline in a file, it works if the file is <anyfile>.v, i.e., it's extension is .v. But if the file extension is changed to .ucf, it starts printing some garbage. I do it through Java code. Any help?

.v is a verilog file and .ucf is a Xilinx propietory constraints file. Both are handled by Xilinx software.

On running,

import java.io.*;
public class ucf {
public static void main(String[] args) throws IOException {
FileWriter output = new FileWriter("sample.ucf");
output.write("foo\r\n\bar\r\n");
output.close();
}
}

garbage is produced.

kamalbanga
  • 1,881
  • 5
  • 27
  • 46

1 Answers1

3

Sounds like you are writing to a Writer or a PrintWriter.

Writers require a CharSet (eg UTF-8) to convert characters to binary data. If you didn't explicitly provide a charset when constructing your Writer, the JVM's default charset will be used.

I'm guessing that you write the file using one charset and Xilinx is reading the file using a different charset thus causing garbage in Xilinx.

Note that \r\n is not standard across all operating systems. To get the OS's newline at runtime you can use System.getProperty("line.separator") instead of hard coding.

lance-java
  • 25,497
  • 4
  • 59
  • 101
  • 1
    +1 Nicely guessed. I whish posters would expend half of that energy to formulate their questions and provide examples of code that behaves wrong. – Ingo Nov 12 '13 at 13:09
  • Yeah, since I usually use raw streams, I didn't think of the whole Writer issue. Good call. – MadConan Nov 12 '13 at 13:11
  • @lance thanks for `System.getProperty`, exactly what i needed :) – kamalbanga Nov 12 '13 at 13:20