A sample binary PGM file is as follows:
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. So to avoid using this method I tried using the BufferedReader.readLine()
method, as follows:
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 i.e.
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 i.e. 400x400 image instead of 10x10 image,
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. 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. 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.
@deprecated This method does not properly convert bytes to characters.
This is true, however for ASCII characters it will probably do what you need.