分享

Dos2Unix

 魔奇工作室 2014-05-07
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();
            }
        }
    }
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多