0

I've been trying to format some text using java on a windows virtual machine. Text from the file looks like:

      Local

      RPLSTRGKD2

      Rcpt/Disb

I'm trying to replace this text block with just

Local Rcpt/Disb

When searching for this block of text in the document originally started with just \n for newline however I switched to using \r\n when I found out that is a how it should be on windows.

On further debugging through eclipse I found that the text was actually:

Local\r\r RPLSTRGKD2\r\r Rcpt/Disb

This code will be deployed to Unix server so I think I may need to change the \r to \n before deploying the code.

I have 2 questions:

  1. Is there any explanatation for why \r is being used instead of \r\n which is expected on windows.

  2. Is there any way to deploy this to a Unix server where \n is supposed to be used instead of \r without having to change the code for deployment?

djfried
  • 27
  • 1
  • 11
  • Are you saying that you already have a file that has `\r\r` in it, or that you're trying to write a file with `\r\n` but getting `\r\r` instead? If you're writing the file out, you need to show the code you're using to write it. IIRC the default Writer behavior is to translate `\n` into whatever the platform's EOL sequence is. – Daniel Pryden Feb 20 '18 at 18:32
  • Possibly related : https://bugs.eclipse.org/bugs/show_bug.cgi?id=329981 and https://stackoverflow.com/questions/14792478/how-to-get-carriage-return-without-line-feed-effect-in-eclipse-console – Vasan Feb 20 '18 at 18:53
  • I'm reading a file that I had assumed was \r\n due to the way windows handles new lines and because `System.getProperty("line.separator") == "\r\n"` however I am seeing \r\r and wondering why it is \r\r and not \r\n and how I can protect myself when moving to a unix environment. – djfried Feb 20 '18 at 19:37
  • Protecting yourself isn't a big deal when you're matching. Use a regex to do the match and simply search for `[\r\n]*` where you expect a newline - will cover all scenarios. – Vasan Feb 20 '18 at 19:40
  • Thanks I ended up just using a regex to match instead of String comparison. I wanted to maintain the same pattern that was being used in the existing code but realized wasn't worth my time. If you reply with an actual answer I will mark it correct. – djfried Feb 22 '18 at 14:49

0 Answers0