-1

I am trying to read a text file, line-by-line and concatenate the lines to create a single string. But while creating that unified string, 0A is being added after each line. The string itself is just one line and I cannot see the 0A in normal text/Java editor, but when I open it in an Hex editor, I can see '0A' after each line. I am working on a Linux (Ubuntu) platform.

I have tried every possible thing to remove them, specifically Java How to remove carriage return (HEX 0A) from String?

But I am not able to remove them. Any thoughts about how to do this?

UPDATE:

File workingFolderLocal = new File("src/test/resources/testdata");
String expected = "String1";

MyClass myClass = new MyClass();
myClass.createPopFile(workingFolderLocal);

// Read the created file and compare with expected output
FileInputStream fin = new FileInputStream(workingFolderLocal + "/output.xyz");
BufferedReader myInput = new BufferedReader(new InputStreamReader(fin));
StringBuilder actual = new StringBuilder("");
String temp = "";
while ((temp = myInput.readLine()) != null) {
    String newTemp = temp.replaceAll("\r", "");
    actual.append(newTemp);
}
System.out.println("actual: " + actual.toString());
myInput.close();

Assert.assertEquals(expected, actual);

Here is the output/error I am getting:

actual: String1
FAILED: testCreatPopFile
junit.framework.AssertionFailedError: expected:<String1> but was:<String1>
    at junit.framework.Assert.fail(Assert.java:47)
    at junit.framework.Assert.failNotEquals(Assert.java:277)
    at junit.framework.Assert.assertEquals(Assert.java:64)
    at junit.framework.Assert.assertEquals(Assert.java:71)
Community
  • 1
  • 1
Bhushan
  • 18,329
  • 31
  • 104
  • 137
  • 2
    The recipe in the other question should work. If it doesn't, please provide a small runnable test case demonstrating what exactly is not working for you and in what way. – NPE Jan 17 '13 at 17:42
  • 1
    If you use `BufferedReader.readLine()` they will be removed for you. How are you reading the file? – Peter Lawrey Jan 17 '13 at 17:59
  • @PeterLawrey: Reading it with `BufferedReader.readLine()` only. – Bhushan Jan 17 '13 at 18:06
  • @NPE: I can post the code snippet, but I think there is no way to post the input file, which I think is the most important thing here. :( – Bhushan Jan 17 '13 at 18:07
  • Added code snippets, hope that helps a bit. – Bhushan Jan 17 '13 at 18:13
  • Your code doesn't make sense and neither does your question. After readLine() there won't be any line terminators in the string. – user207421 Jan 17 '13 at 18:16
  • 1
    In the assert, do you need to use actual.toString since its a stringbuilder? – Mike Jan 17 '13 at 18:18
  • Try printing `System.out.println("actual: " + Arrays.toString(actual.getBytes("UTF-8")));` I suspect your special character is not what you think it is. – Peter Lawrey Jan 17 '13 at 18:24
  • @Mike: If you post your comment as answer, I will accept it. Such a silly mistake. Others, thanks a lot for your efforts. – Bhushan Jan 17 '13 at 18:27
  • @Bhushan added my comment as an answer. Thanks! – Mike Jan 17 '13 at 20:29

3 Answers3

2

The type of the expected variable is String while the type of the actual variable is StringBuilder. These objects will never be equal in the sence of ...

Assert.assertEquals(expected, actual);

, because they have different types.

oheyder
  • 121
  • 3
1

The '0A' is the newline character ("\n"). You are only removing the carriage return character ("\r") (0D). Try replacing "\n" as well in the same way you are replacing "\r". As someone commented, the readline() call should take care of that though.

In windows, lines end with both \r\n In *nix lines en only with \n

See newline

pakman
  • 1,676
  • 3
  • 23
  • 41
1

In the assert, do you need to use actual.toString since its a stringbuilder?

Added this from the comments in order to accept the answer.

@oheyder stumbled upon this as well. Gave him a +1.

Mike
  • 3,186
  • 3
  • 26
  • 32