zoukankan      html  css  js  c++  java
  • 课堂实践5-31

    编写MyOD.java 用java MyOD XXX实现Linux下od -tx -tc XXX的功能.提交测试代码和运行结果截图,加上学号水印,提交码云代码链接。

    转化为ascii的函数

    public static String format(byte[] bt,FileReader fr) throws IOException{
            int line = 0,line1=1;
            int ch = 0;
            StringBuilder buf = new StringBuilder();
            for (byte d : bt) {
                if (line % 16 == 0)
                    buf.append(String.format("%05d: ", line));
    
                buf.append(String.format("%02x  ", d));
                line++;
                if (line % 16 == 0)
                    buf.append("
    ");
            }
            buf.append("
    ");
            return buf.toString();
    
    
        }
    

    在这里我选择在main函数里新建txt文件
    具体代码如下

     File file = new File("d:/temp", "in.txt");
    
            try {
                file.createNewFile(); // 创建文件
            } catch (IOException e) {
                e.printStackTrace();
            }
    

    在main函数中向文件中写入内容

    String str = "1234567897asd#23#%$$@$as6";
            byte bt[] = new byte[1024];
            bt = str.getBytes();
            try {
                FileOutputStream in = new FileOutputStream(file);
                try {
                    in.write(bt, 0, bt.length);
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
    

    进行转化并输出

     String aa = myod.format(bt,fr);
            System.out.println(aa);
    

    image
    将得到的结果输出到out.txt文件中

     byte[] bt1 =aa.getBytes();
            try {
                FileOutputStream out = new FileOutputStream(file1);
                try {
                    out.write(bt1, 0, bt1.length);
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            }
    

    读取文件readFile函数

        public static byte[] readFile(String file) throws IOException {
            InputStream is = new FileInputStream(file);
            int length = is.available();
            byte bt[] = new byte[length];
            is.read(bt);
            return bt;
    
        }
    

    最后得到的结果imageimage

    码云链接

  • 相关阅读:
    Activit 5.13 工作流部署新版本后回退到上一个版本
    一个java的http请求的封装工具类
    FastJSON使用例子
    SoapUI、Postman测试WebService
    PLSQL连接oracle数据库
    python函数修饰符@的使用
    QEMU KVM Libvirt手册(8): 半虚拟化设备virtio
    QEMU KVM Libvirt手册(7): 硬件虚拟化
    多个router和多个network
    nova file injection的原理和调试过程
  • 原文地址:https://www.cnblogs.com/paypay/p/6924826.html
Copyright © 2011-2022 走看看