C0deAttack
C0deAttack

Reputation: 24667

Comparing file contents cross platform

I have a file that has been created on Windows, so it has carriage returns "\r\n".

The file is used in a test, the contents of the file are compared to the contents of a file that is generated during the test. The test passes on Windows because the generated file has the same carriage return.

But the test doesn't pass on Unix/Mac.

It fails because the generated file has linefeeds, ie. only "\n".

Can any one advise how to deal with this?

Edit: The test is a JUnit (Java) test.

Upvotes: 2

Views: 335

Answers (3)

C0deAttack
C0deAttack

Reputation: 24667

I ended up not using a real file, and had the test create the file on the platform at runtime using the system property to get the platforms line terminator.

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533880

You can try the following to avoid reading line by line with

String text = FileUtils.readFileAsString(filename)
                       .replaceAll("(\r\n|\r|\n)", "\n");

This replaces all new-lines of any type with "\n"

Upvotes: 1

Nikita Skvortsov
Nikita Skvortsov

Reputation: 4923

  • You could compare files reading them line-by-line
  • You could have two different sample files for unix/windows

Upvotes: 1

Related Questions