linux -cp java_编写MyCP.java 实现类似Linux下cp XXX1 XXX2的功能

具体实现

产品代码:

import java.io.*;

public class MyCP {

public static void main(String args[]) {

String choose = args[0];

//获得第一个参数

String File1 = args[1];

//获得第二个参数:文件名

String File2 = args[2];

//获得第三个参数:文件名

File sourceFile = new File(File1);

//读取的文件

File targetFile = new File(File2);

//写入的文件

int ch = 0;

String result = "";

//转换结果

if (choose.equals("-tx")) {

ch = 1;

}

else if (choose.equals("-xt")) {

ch = 2;

}

//参数判断

else {

System.out.println("输入参数错误!");

System.exit(0);

}

//如果参数输入错误,退出程序

try {

FileWriter out = new FileWriter(targetFile);

//指向目的地的输出流

FileReader in = new FileReader(sourceFile);

//指向源的输入流

BufferedReader infile = new BufferedReader(in);

BufferedWriter outfile = new BufferedWriter(out);

//缓冲流

String number = infile.readLine();

if (ch == 1) {

int n, temp = Integer.parseInt(number);

for (int i = temp; i > 0; i = i / 2) {

if (i % 2 == 0)

n = 0;

else

n = 1;

result = n + result;

}

} else if (ch == 2) {

result = Integer.valueOf(number, 2).toString();

}

outfile.write(result);

outfile.flush();

outfile.close();

} catch (IOException e) {

System.out.println("Error " + e);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值