Eine Probe binäre PGM-Datei wird wie folgt dar:Mit Datainputstream nach BufferedReader
P5
# This is a comment
10 10
255
#image intensity information in bytes that I am unable to copy paste here
wie
Wenn ich versuche, die Datei mit dem folgenden Code zu lesen:
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();
}
}
}
Es funktioniert perfekt und gibt die folgende Ausgabe:
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
aber es zeigt, dass die DataInputStream.readLine()
Methode veraltet ist. So vermeiden Sie diese Methode habe ich versucht, die BufferedReader.readLine()
Verfahren wie folgt verwendet:
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();
}
}
}
Und die Ausgabe geändert:
P5
# This is a comment
10 10
255
und es scheint, dass die zweite while
Schleife dh
while(dis.available() != 0){
System.out.print(dis.readUnsignedByte() + " ");
}
funktioniert nicht.
Was könnte der mögliche Grund dafür sein?
Ich habe versucht, statt 10x10 Bild um ein größeres Bild dh 400x400 Bild mit,
und versuchten, das Bytes von einem Bild zu kopieren und sie in die neue Datei fügen Sie den folgenden Code verwenden:
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();
}
}
}
und das Ausgangsbild kam wie folgt aus:
Wenn ich DataInputStream.readLine()
im gesamten Code verwende, dann entspricht das Ausgabebild genau dem Eingabebild. Wie ist es passiert?
Können Sie 'DataInputStream # readUTF()' nicht verwenden? –
@VinceEmigh Ich habe versucht, es gerade jetzt zu verwenden, und es gibt 'java.io.EOFException' Ausnahme – cnova
Sie nicht sagen, Sie möchten gepufferten Leser verwenden? dann warum die beiden mischen? – gpasch