具体实现
产品代码:
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);
}
}