1

How can I convert Binary PGM Files to ASCII PGM Files using Java?

When I use the following code, I am unable to write ASCII values in the B.pgm. I've tried using dos.writeInt also.

FileInputStream inRaw = null;
FileOutputStream outRaw = null;
try {
     inRaw = new FileInputStream("A.pgm");
     outRaw = new FileOutputStream("B.pgm");
} catch (FileNotFoundException e) {
     e.printStackTrace();
}

DataInputStream dis = new DataInputStream(inRaw);
DataOutputStream dos = new DataOutputStream(outRaw);
int i = 0;
String temp = null;
temp = dis.readLine();
System.out.println(temp);
dos.writeBytes("P2");
dos.writeBytes("\n");
while(i < 3){
    temp = dis.readLine();
    System.out.println(temp);
    dos.writeBytes(temp);
    dos.writeBytes("\n");
    i++;
}
int t = 0;
while(dis.available() != 0){
    t = dis.read();
    System.out.println(t);
    fileWriter.write(t);
    dos.writeInt(t);
    dos.writeBytes("\n");
}

 dis.close();
 dos.close();

I tried to use FileWriter instead of DataOutputStream and the code produces an empty file, I can't figure out why?

FileInputStream inRaw = null;
FileOutputStream outRaw = null;
try {
     inRaw = new FileInputStream("A.pgm");
     outRaw = new FileOutputStream("B.pgm");
} catch (FileNotFoundException e) {
     e.printStackTrace();
}

DataInputStream dis = new DataInputStream(inRaw);
FileWriter fileWriter = new FileWriter("B.pgm");
int i = 0;
String temp = null;
temp = dis.readLine();
System.out.println(temp);
fileWriter.write("P2");
fileWriter.write("\n");
while(i < 3){
    temp = dis.readLine();
    System.out.println(temp);
    fileWriter.write(temp);
    fileWriter.write("\n");
    i++;
}
int t = 0;
while(dis.available() != 0){
    t = dis.read();
    System.out.println(t);
    fileWriter.write(t);
    fileWriter.write(t);
    fileWriter.write("\n");
}

 dis.close();
cnova
  • 725
  • 2
  • 9
  • 22
  • In the DataOutputStream case, you don't want to be using `writeInt(t)` to write an ascii integer - use `writeBytes(String.valueOf(t))` - otherwise, it will write the data in binary. – Andy Turner Mar 16 '16 at 10:13
  • @AndyTurner Thank you. :) Now I am able to write ASCII pgm files. – cnova Mar 16 '16 at 12:06

1 Answers1

1

(This might not solve your issue, but it will make your code much more robust).

One of the possible reasons why data isn't written to a file is that you don't actually close the stream - it can be held in an in-memory buffer until then, and is never written out. You certainly don't make an explicit call to fileWriter.close().

You can try to close the streams manually, but it's normally unnecessary, and surprisingly tricky to do it correctly in all circumstances.

It is easier to use try-with-resources - this automatically manages your streams, and guarantees they are closed by the end of the block.

try (FileInputStream inRaw = new FileInputStream("A.pgm");
     FileOutputStream outRaw = new FileOutputStream("B.pgm");
     DataInputStream dis = new DataInputStream(inRaw);
     DataOutputStream dos = new DataOutputStream(outRaw)) {
  // The logic to read dis and write it to dos.
} catch (IOException e) {
  e.printStackTrace();
}
// No need to close the streams - that happened automatically.

You can do similarly with the second version by substituting the FileWriter fileWriter = new FileWriter(outRaw) for DataOutputStream dos = ....


If this doesn't solve the immediate issue, I would suggest that there is an error in your logic. Try stepping through the code with a debugger - make sure that the write calls are actually being performed.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • 1
    I tried using the try-catch block, but still unable to write anything in the file. I used the following FileWriter constructor `FileWriter fileWriter = new FileWriter("B.pgm")`. I'll debug as you told and see what is the problem. Thanks. – cnova Mar 16 '16 at 12:18