简体   繁体   English

在BufferedReader之后使用DataInputStream

[英]Using DataInputStream after BufferedReader

A sample binary PGM file is as follows: 示例二进制PGM文件如下:

P5
# This is a comment
10 10
255
#image intensity information in bytes that I am unable to copy paste here

like 喜欢

在此处输入图片说明

When I try to read the file using the following code: 当我尝试使用以下代码读取文件时:

import java.io.*;

public class Pgm_reader2 {
    public static void main(String[] args) throws IOException {
        try {
            FileInputStream inRaw = new FileInputStream("A.pgm");
            DataInputStream dis = new DataInputStream(inRaw);
            int i = 0;
            while(i < 4){
                System.out.println(dis.readLine());
                i++;
            }
            while(dis.available() != 0){
                System.out.print(dis.readUnsignedByte() + " ");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

It works perfectly and gives the following output: 它运行完美,并提供以下输出:

P5
# This is a comment
10 10
255
0 255 255 255 255 255 255 255 255 255 255 0 255 255 255 255 255 255 255 255 255 255 0 255 255 255 255 255 255 255 255 255 255 0 255 255 255 255 255 255 255 255 255 255 0 255 255 255 255 255 255 255 255 255 255 0 255 255 255 255 255 255 255 255 255 255 0 255 255 255 255 255 255 255 255 255 255 0 255 255 255 255 255 255 255 255 255 255 0 255 255 255 255 255 255 255 255 255 255 0

but it shows that the DataInputStream.readLine() method is deprecated. 但它表明不赞成使用DataInputStream.readLine()方法。 So to avoid using this method I tried using the BufferedReader.readLine() method, as follows: 因此,为避免使用此方法,我尝试使用BufferedReader.readLine()方法,如下所示:

import java.io.*;

public class Pgm_reader2 {
    public static void main(String[] args) throws IOException {
        try {
            FileInputStream inRaw = new FileInputStream("A.pgm");
            DataInputStream dis = new DataInputStream(inRaw);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inRaw));
            int i = 0;
            while(i < 4){
                System.out.println(bufferedReader.readLine());
                i++;
            }
            while(dis.available() != 0){
                System.out.print(dis.readUnsignedByte() + " ");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

And the output changed to: 输出更改为:

P5
# This is a comment
10 10
255

and it seems that the second while loop ie 而且似乎第二个while循环即

while(dis.available() != 0){
    System.out.print(dis.readUnsignedByte() + " ");
}

is not working. 不管用。

What could be the possible reason for this? 这可能是什么原因?

I tried using a larger image ie 400x400 image instead of 10x10 image, 我尝试使用较大的图像,即400x400图像而不是10x10图像, 在此处输入图片说明

and tried to copy the bytes from one image and paste them into the new file using the following code: 并尝试使用以下代码从一个图像中复制字节并将其粘贴到新文件中:

import java.io.*;

public class Pgm_reader2 {
    public static void main(String[] args) throws IOException {
        try {
            FileInputStream inRaw = new FileInputStream("A.pgm");
            FileOutputStream outRaw = new FileOutputStream("B_test.pgm");
            DataInputStream dis = new DataInputStream(inRaw);
            DataOutputStream dos = new DataOutputStream(outRaw);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inRaw));
            String line = null;
            int i = 0;
            while(i < 4){
                line = bufferedReader.readLine();
                dos.writeBytes(line);
                dos.writeBytes("\n");
                i++;
            }
            int intbyte = 0;
            while(dis.available() != 0){
                intbyte = dis.readUnsignedByte();
                dos.writeByte(intbyte);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

and the output image came out like this: 然后输出图像如下所示:

在此处输入图片说明

If I use DataInputStream.readLine() in the whole code then the output image comes exactly the same as the input image. 如果我在整个代码中使用DataInputStream.readLine() ,则输出图像与输入图像完全相同。 How is it happening? 怎么回事

BufferedReader is buffered which means it will read as much as possible (up the the buffer's size) to minimise the number of calls to the underlying Reader or Stream. BufferedReader已缓冲,这意味着它将尽可能多地读取(增大缓冲区的大小),以最大程度地减少对基础Reader或Stream的调用次数。 As such it is not suitable for changing the stream later. 因此,它不适合以后更改流。 In general it is bad idea to change the wrapper of an underlying stream or reader unless you really know what you are doing. 通常,除非您真的知道自己在做什么,否则更改底层流或阅读器的包装是个坏主意。

In this case, the simplest solution is to use the deprecated method. 在这种情况下,最简单的解决方案是使用不建议使用的方法。 You could write your own replacement for readLine() but that might just introduce more problems than it helps. 您可以编写自己的readLine()替代品,但这可能只会带来更多问题,而无济于事。

@deprecated This method does not properly convert bytes to characters. @deprecated此方法不能正确将字节转换为字符。

This is true, however for ASCII characters it will probably do what you need. 的确如此,但是对于ASCII字符,它可能会满足您的需求。

I got the answer to why the image is shifted a bit upwards and leftwards and why there is black strip at the bottom when I use bufferedReader before dataInputStream . 我得到了答案,为什么在我在dataInputStream之前使用bufferedReader时,图像会稍微向上和向左移动,以及为什么在底部有黑条? Explaination: 阐释:

we had 160000 bytes to read after header if the image size was 400*400. 如果图片尺寸为400 * 400,则标头后有160000个字节需要读取。 Since bufferedReader reads extra and keep in its buffer, lets assume that it 25000 bytes extra after the header. 由于bufferedReader会额外读取并保留在其缓冲区中,因此假定它在标头之后额外增加25000个字节。 So when the dataInputStream starts to read the image intensities, it gets to read from the 25001th byte. 因此,当dataInputStream开始读取图像强度时,它将从第25001个字节开始读取。 And it puts the 2500001th byte at 1st position, and therefore all the pixel intensities are shifted 250000 position behind ie 62 rows up and 200 pixels left. 并且将第2500001个字节放在第一个位置,因此所有像素强度都向后移250000个位置,即向上移62行,向左移200个像素。 Since there are 25000 bytes less in the last so 0 is written from pixel (160000-25000+1) to 160000, hence the black strip at the bottom. 由于最后一个字节少了25000个字节,因此从像素(160000-25000 + 1)到160000写入了0,因此底部的黑色条带被写入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM