MyCP课下作业

任务详情

  • 编写MyCP.java 实现类似Linux下cp XXX1 XXX2的功能,要求MyCP支持两个参数:
    • java MyCP -tx XXX1.txt XXX2.bin 用来把文本文件(内容为十进制数字)转化为二进制文件
    • java MyCP -xt XXX1.bin XXX2.txt 用来二进制文件把转化为文本文件(内容为十进制数字)

代码

import java.io.*;
public class MyCP {
    public static void main(String[] args) {
        String transformMethod = args[0];
        File source = new File(args[1]);
        File target = new File(args[2]);
        try {
            Reader in = new FileReader(source);
            BufferedReader bufferedReader = new BufferedReader(in);
            String srcNum = bufferedReader.readLine();
            String tarNum = "";
            if(transformMethod.equals("MyCP-tx")){             //十进制转二进制
                tarNum = Integer.toBinaryString(Integer.parseInt(srcNum));
            }
            else{
                tarNum = Integer.valueOf(srcNum,2).toString();
            }
            Writer out = new FileWriter(target);
            BufferedWriter bufferedWriter = new BufferedWriter(out);
            bufferedWriter.write(tarNum);
            bufferedReader.close();
            bufferedWriter.close();
        } catch (Exception e) {
            System.out.println(e);
        }


    }
}

运行结果

posted @ 2019-04-28 22:46  20175207冷南  阅读(105)  评论(0编辑  收藏  举报