import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.PrintWriter; public class Dos2Unix { private enum Type { DOS2UNIX, UNIX2DOS } private Dos2Unix() { super(); } /** * @param dosFilePath * @param unixFilePath */ public static void dos2unix(String dosFilePath, String unixFilePath) { dos2unix(new File(dosFilePath), new File(unixFilePath)); } /** * @param dosFile * @param unixFile */ public static void dos2unix(File dosFile, File unixFile) { perform(dosFile, unixFile, Type.DOS2UNIX); } /** * @param unixFilePath * @param dosFilePath */ public static void unix2dos(String unixFilePath, String dosFilePath) { unix2dos(new File(unixFilePath), new File(dosFilePath)); } /** * @param unixFile * @param dosFile */ public static void unix2dos(File unixFile, File dosFile) { perform(unixFile, dosFile, Type.UNIX2DOS); } /** * @param oldFile * @param newFile * @param type */ private static void perform(File oldFile, File newFile, Type type) { BufferedReader reader = null; PrintWriter writer = null; try { reader = new BufferedReader(new FileReader(oldFile)); writer = new PrintWriter(new FileOutputStream(newFile)); String tmp = null; while ((tmp = reader.readLine()) != null) { writer.write(tmp + (type == Type.DOS2UNIX ? '\u005c\u006e' : "\u005c\u0072\u005c\u006e")); writer.flush(); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (reader != null) { reader.close(); } if (writer != null) { writer.close(); } } catch (Exception e) { e.printStackTrace(); } } } } |
|