How to convert files from Dos to Unix

拥有回忆 提交于 2019-11-27 07:40:10

问题


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?


回答1:


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.




回答2:


Just an alternate way (than the one parasietje described) using dox2unix. Say you all dos files are in one folder

Runtime.getRuntime().exec("dos2unix /path/to/dos/files/*");



回答3:


Most unix/linux distributions have utility named unix2dos and dos2unix commands.

EDIT: Just copy your file to unix machine and run dos2unix *.

You can also find this utility for Windows and do the same.




回答4:


There is a utility/command in Linux/Unix called dos2unix that will help you convert your files from dos to unix format. To install simply type in console(you may need root privileges)

yum install dos2unix

To do the conversion you have to use command dos2unix followed by filename. For example

[aniket@localhost ~]$ dos2unix sample.txt 
dos2unix: converting file sample.txt to UNIX format ...

For all files in a directory you can simply use

dos2unix *



回答5:


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

The above line should remove all \r but for some reason it also removes the \n so I had to add it back when printing unixText to a file: unixText + "\n"



来源:https://stackoverflow.com/questions/9374991/how-to-convert-files-from-dos-to-unix

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!