具體實現html
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); } } }
參數爲-tx時
java
參數爲-xt時
git