How to convert files from Dos to Unix

Code Hungry picture Code Hungry · Feb 21, 2012 · Viewed 31.3k times · Source

I am having several files which I want to convert from Dos to Unix. Is there any API or method which will help me to do this?

Answer

parasietje picture parasietje · Feb 21, 2012

There are linux tools that can do this (dos2unix for example).

In Java it can be done with String.replaceAll().

DOS uses \r\n for line termination, while UNIX uses a single \n.

String unixText = windowsText.replaceAll("\r\n", "\n"); // DOS2UNIX

So no, no API exists. Yes, it is dead easy.