zoukankan      html  css  js  c++  java
  • 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.*;
    /**
    *Created by xiang on 2018/6/10.
    */
    public 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);//Byte.parseByte(Integer.toBinaryString(num)); //转换为二进制文件
                    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("D:/Java/from.txt");
                fos = new FileOutputStream("D:/Java/to.txt");
                dumpToTen(fis, fos);
            }catch(Exception e) {
                System.out.println(e);
            }
        }
    }
    

    结果

  • 相关阅读:
    ssh 使用密钥文件
    VS2015企业版,社区版,专业版详细对比
    Redis 与 数据库处理数据的两种模式(转)
    工业级物联网项目架构设计思想(转)
    C# and Redis,安装作为服务
    C# CRC32
    c++,C# 转换
    app配置智能硬件的解决方案
    C# 与C++的数据转换
    C++ 对数组sizeof 和对数组元素sizeof
  • 原文地址:https://www.cnblogs.com/musea/p/9164723.html
Copyright © 2011-2022 走看看