编写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("\n");
        }
        buf.append("\n");
        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

码云链接