【问题标题】:print the binary data from a file in 16 bytes in each line using java?使用java以每行16个字节打印文件中的二进制数据?
【发布时间】:2014-09-05 20:57:18
【问题描述】:

我也在复制代码和输出我想知道为什么在提供更大的数据文件作为输入时执行程序需要这么多时间。

请看透代码并给我建议并尝试执行。

java程序:

FileInputStream fis = new FileInputStream("rose.jpg"); //use your own binary file name
DataInputStream dis = new DataInputStream(fis);
try {
    byte buffer[] = new byte[16];

    while((line = dis.read(buffer)) != -1)
    {
        for(int i = 0; i < line; i++)
        {

                value = Integer.toHexString(0xFF & buffer[i] | 0x100).substring(1);
                nextvalue = nextvalue+""+value;

        }
            if(a == 0)
            {
                incValue = nextvalue.substring(0, 32);
                System.out.println(incValue);
            }
            else
            {
                counter = counter + 32;
                incValue = nextvalue.substring(counter, counter + 32);
                System.out.println(incValue);
            }
            a++;

输出:

ffd8ffe000104a464946000101020025
00250000ffdb00430002010101010102
01010102020202020403020202020504
04030406050606060506060607090806
0709070606080b08090a0a0a0a0a0608
0b0c0b0a0c090a0a0affdb0043010202
02020202050303050a0706070a0a0a0a
0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a

输出的每一行将有 16 个字节的值。

请帮我解决这个程序我必须修改和更改的内容,以便程序快速执行。

【问题讨论】:

  • 这是一个很好的问题,可以用来学习几种编程技术。不要害怕尝试并在你第一次开始工作后尝试改进你所拥有的东西。对于额外的挑战,在右侧的第 3 列中打印等效的 ASCII 字符,使用 . 或类似的“代替”不可打印的字符。你最终会得到一个方便的工具。

标签: java core


【解决方案1】:

您应该做两件事:1) 将文件输入流包装在 BufferedInputStream 中。

InputStream bis = new BufferedInputStream( fileInputStream, 1024*1024 );

这将减少您从存储中读取的次数。 2) 通过附加到 StringBuffer 而不是直接写入输出来缓冲输出。请注意,如果您的文件非常大,您可能需要编写字符串缓冲区并定期重置它...当然取决于文件的大小。

【讨论】:

  • 1MB 缓冲区大小完全超出了默认大小的 8K,不会显着提高性能。
  • @Durandal 这完全取决于文件的大小、机器与存储的速度、延迟等。他应该尝试为他的情况找到合适的大小。
  • while((line = bis.read(buffer)) != -1) { for(int i = 0; i
【解决方案2】:

使用new BufferedInputStream(new FileInputStream(...))

不要使用String nextValue,而是使用StringBuilder。字符串+ 速度大大降低。

你可能会使用

String.format("%02x", buffer[i] & 0xFF);

【讨论】:

  • 更好的方法可能是使用零参数构造函数创建单个 Formatter,因为它将在内部创建一个 StringBuilder。 String.format 每次调用都会创建一个新的 Formatter。
  • 我需要从文件中获取二进制数据并逐行打印数据 16 字节是否可以通过使用 append 方法或任何其他建议。
  • 主要的速度问题在于String +, += IMO:取消nextValue(PrintWriter,或者至少是StringBuilder)。可以使用"%02x%02x%02x...", ints[0], ints[1], ... ,但@VGR 的想法更直接。
【解决方案3】:

你让nextvalue越来越大。对它的子字符串操作变得昂贵。 substring 是一个O(n) 操作。

而不是

            if(a == 0)
            {
                incValue = nextvalue.substring(0, 32);
                System.out.println(incValue);
            }
            else
            {
                counter = counter + 32;
                incValue = nextvalue.substring(counter, counter + 32);
                System.out.println(incValue);
            }
            a++;

为什么不只是:

System.out.println(nextvalue);
nextvalue = "";

【讨论】:

  • 0x100 的 ORing 可能是为小数字添加前导零 - 比如如果 buffer[i] 是 5,你会想要打印 05。
  • 因为我需要以十六进制格式打印二进制数据
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-22
  • 2014-01-10
  • 1970-01-01
  • 2023-04-07
  • 2019-08-22
  • 2013-01-18
相关资源
最近更新 更多