la escritura de Java para el mal comportamiento de archivos
-
26-09-2019 - |
Pregunta
Esta es otra de mis preguntas Java.
Por favor, eche un vistazo a este código:
public static void main(String[] args) {
if( args.length != 5) {
System.out.println("Error. Wrong number of params!");
System.exit(0);
}
File file = new File(args[0]);
try {
BufferedImage image = ImageIO.read(file);
FileWriter fstream = new FileWriter("output.txt");
BufferedWriter out = new BufferedWriter(fstream);
int avg = 0;
int w = image.getWidth();
int h = image.getHeight();
Rectangle r = new Rectangle();
double whiteHelp = Double.parseDouble(args[4]);
avg = (int) (avg / 1);
int startX = Integer.parseInt(args[2]);
int startY = Integer.parseInt(args[3]);
r.width = r.height = Integer.parseInt(args[1]);
for(int i = startY; i <= h - r.height; i += r.height) {
for(int j = startX; j <= w - r.width; j += r.width) {
r.x = j;
r.y = i;
avg = getTileColor(r, image, whiteHelp);
//System.out.print(avg);
out.write(avg);
}
//System.out.println();
out.write("\n");
}
out.close();
image.flush();
System.out.println("Finished parsing the image. Solving...");
} catch (Exception e) {
System.out.println("Error.");
e.printStackTrace();
}
}
En salida.txt solo me dan el texto no legible, como si al abrir el archivo binario, por ejemplo.
¿Hay algún rey del problema cuando BufferedImage y BufferedWriter se crean instancias de forma simultánea? Estoy usando el mismo procedimiento para escribir en el archivo en otra clase, donde se abre un archivo para lectura, y otro se abre para escribir, y las cosas funcionan bien.
p.s. ¿Cómo alcanzar y carácter lt sin romper el código ??? ¿Es necesario que introducirlo como <?
Gracias
Solución
invoke
out.write(avg)
para escribir el int promedio a la salida.
Esto invocará el método BufferedWriter.write(int)
; esto va a escribir el carácter con el punto de código Unicode avg
. Esto probablemente no es lo que quiere; -).
Para imprimir el avg
número en formato decimal, el uso
out.write(String.valueOf(avg))