-3

In Windows, by default the EOL Character is "\r\n". But I have to change the EOL Character as "\n".

How can I convert the string from windows format to UNIX format?

This is the string which I am sending from windows to unix server.

ASPII90 ASPII90 dsascot 00000066YHDASCDRCD\t0700\t9\tASPII90\tA2185\t1234\n YRQ\t\t\t000415832\n YTRENDDRCD\t3\n

But in server, each \n is converted into whitespace followed by "\".(i.e)

YHDASCDRON>0400>9>ASPII90>A2185>128 \ 
stema
  • 90,351
  • 20
  • 107
  • 135
  • 5
    Please post your attempt. – Jerry Dec 18 '13 at 07:04
  • 1
    (And contrary to your tags, you shouldn't need a regex.) – Jon Skeet Dec 18 '13 at 07:05
  • This is the string which i am sending from windows to unix server. ASPII90 ASPII90 dsascot 00000066YHDASCDRCD\t0700\t9\tASPII90\tA2185\t1234\n YRQ\t\t\t000415832\n YTRENDDRCD\t3\n But in server, each \n is converted into whitespace followed by "\".(i.e)YHDASCDRON>0400>9>ASPII90>A2185>128 \ – Sathesh Subburaj Dec 18 '13 at 07:05
  • This question seems unrelated to windows EOL characters. The example input you've provided does **NOT** contain any `\r` characters. Neither does the example output you've provided! – jahroy Dec 18 '13 at 07:29
  • You should provide some details about **how** you're sending strings from windows to a UNIX server... and how you're retrieving them (and printing them) on the server. Are you sending requests to a servlet? Are you using sockets? How are we supposed to guess? – jahroy Dec 18 '13 at 07:32
  • Hi jahroy, If " \n " specified in the string, it will automatically appends the " \r\n ". But i won't be visble. It will happen in back end. please read about the EOL Characters in different operating systems. – Sathesh Subburaj Dec 18 '13 at 07:35
  • I am **very** familiar with the EOL characters used by different operating systems. I have dealt with this issue many times. If you're really having a simple newline issue, the answers below should solve your problem. My guess is your problem is somewhere else. Feel free to ignore me. – jahroy Dec 18 '13 at 07:37
  • Here's [another way](https://code.google.com/p/ec2manager/source/browse/trunk/Ec2+Manager/src/com/unience/util/Dos2Unix.java?spec=svn4&r=4) to do it... – jahroy Dec 18 '13 at 07:46
  • @Jahroy: While i am firing the request from putty, If i created the unix formatted request using notepad++ feature, the server accepts and giving the response. If i use windows format, Its throwing same error as what i have asked in question. – Sathesh Subburaj Dec 18 '13 at 08:32

4 Answers4

0

here is an possible example Code (link to docs: String.replace():

public class HelloWorld{

 public static void main(String []args){
    System.out.println("Hello World");
    String myString ="asdasdasdasdassdasdasd\r\n";
    System.out.println(myString.replace("\r\n","\n"));
 }
}
Aruscher
  • 153
  • 1
  • 10
  • I have tried this. no use. By default, the windows will take the " \r\n " instead of " \n ". Because that is the default EOL Character in windows. – Sathesh Subburaj Dec 18 '13 at 07:18
0

Use notepad++

Open file in notepad++, select Edit, EOL Conversion, UNIX Format.

Done

mcalex
  • 6,628
  • 5
  • 50
  • 80
0

You can use BufferedReader's readLine() method to read each line.

readLine()method will remove \r\n for you

Then use BuffereWriter.write() to write this line append with '\n'

  BufferedReader reader = new BufferedReader(new FileReader("xx"));

    BufferedWriter writer = new BufferedWriter(new FileWriter("yy"));

    String line = null;
    while ((line = reader.readLine()) != null) {
        writer.write(line + "\n");
    }
oxygen
  • 26
  • 5
0

String is immutable, but you can create a new local string to be used as the return value:

String newString = originalStringNameWithManyLineBreaks.replaceAll("\r\n" , "\n");

now only send the value of newString to your server

ghoulfolk
  • 326
  • 7
  • 17