蓝墨云班课 编写MyCP.java
要求:
编写MyCP.java 实现类似Linux下cp XXX1 XXX2的功能,要求MyCP支持两个参数:
- java MyCP -tx XXX1.txt XXX2.bin 用来把文本文件(内容为十进制数字)转化为二进制文件
- java MyCP -xt XXX1.bin XXX2.txt 用来二进制文件把转化为文本文件(内容为十进制数字)
提交测试代码和运行结果截图,加上学号水印,提交码云代码链接。
代码托管
- 我将主函数定义为MYDC;
- 然后通过调用输入文档,将其中的数转2进制到输出文档中,类代码如下:
public static void dumpToTwo(InputStream src, OutputStream dest)
throws IOException {
try (InputStream input = src; OutputStream output = dest) {
byte[] data = new byte[1];
int length;
while ((length = input.read(data)) != -1) {
String str = Integer.toBinaryString((data[0]&0xFF)+0x100).substring(1);
data[0] = Byte.parseByte(str);
output.write(data, 0, length);
}
}
}
- 调用输入文档,将其中的数转十进制到输出文档中,类代码如下:
public static void dumpToTen(InputStream src, OutputStream dest)
throws IOException {
try (InputStream input = src; OutputStream output = dest) {
byte[] data = new byte[1];
int length;
while ((length = input.read(data)) != -1) {
data[0] = Byte.parseByte(String.valueOf(data[0]),10);
output.write(data, 0, length);
}
}
}
- 这是我的完整代码:
/**
* Created by zx on 17-6-1.
*/
import java.io.*;
class MyCP{
public static void dumpToTwo(InputStream src, OutputStream dest)
throws IOException {
try (InputStream input = src; OutputStream output = dest) {
byte[] data = new byte[1];
int length;
while ((length = input.read(data)) != -1) {
String str = Integer.toBinaryString((data[0]&0xFF)+0x100).substring(1);
data[0] = Byte.parseByte(str);
output.write(data, 0, length);
}
}
}
public static void dumpToTen(InputStream src, OutputStream dest)
throws IOException {
try (InputStream input = src; OutputStream output = dest) {
byte[] data = new byte[1];
int length;
while ((length = input.read(data)) != -1) {
data[0] = Byte.parseByte(String.valueOf(data[0]),10);
output.write(data, 0, length);
}
}
}
public static void main(String[] args) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("/home/zx/代码托管/shuru.java");
fos = new FileOutputStream("/home/zx/代码托管/shuchu.java");
dumpToTen(fis, fos);
}catch(Exception e) {
System.out.println(e);
}
}
}
- 截图如下: