1

I would like to write file in JAVA

  • Encoding : ANSI
  • New line: CR LF

Example:

FileUtils.writeLines(new File(baseFolder.getAbsolutePath() + File.separatorChar + filename), data);

I am using Apache FileUtils.writeLines but it is always in UTF-8 not ANSI and new line as LF and not CR LF?

Server is Linux not Windows

mbrc
  • 3,523
  • 13
  • 40
  • 64
  • Have a look at http://stackoverflow.com/questions/3708991/setting-java-vm-line-separator and read beyond the accepted answer. – PM 77-1 Jul 14 '16 at 13:03

1 Answers1

4

Invoke the appropriate overload of writeLines, with the parameters in the right order:

public static void writeLines(File file,
          String encoding,
          Collection<?> lines,
          String lineEnding)
                   throws IOException

e.g.

FileUtils.writeLines(
    new File(baseFolder.getAbsolutePath(), filename),
    "Cp1252",
    data,
    "\r\n");

Note that Windows ANSI isn actually called Cp1252 in Java, according to this answer.


You are passing the encoding as the last parameter to this overload, which is actually the line ending parameter.

public static void writeLines(File file,
          Collection<?> lines,
          String lineEnding)
                   throws IOException
Community
  • 1
  • 1
Andy Turner
  • 137,514
  • 11
  • 162
  • 243