Pergunta

Esta é mais uma das minhas perguntas de Java.

Por favor, dê uma olhada neste 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();  
  }

 } 

No output.txt, só recebo texto não legível, como se fosse aberta o arquivo binário, por exemplo.

Existe algum rei de problema quando o buffermage e o bufferwriter sendo instanciados simultaneamente? Estou usando o mesmo procedimento para gravar para arquivar em outra classe, onde um arquivo é aberto para leitura, e outro é aberto para escrever, e as coisas funcionam muito bem.

PS como entrar <personagem sem quebrar o código ??? Eu preciso entrar como & lt;?

Obrigado

Foi útil?

Solução

Você invoca

out.write(avg)

para escrever o int avg na saída.

Isso invocará o método BufferedWriter.write(int); Isso escreverá o personagem com o ponto de código Unicode avg. Provavelmente não é isso que você quer ;-).

Para imprimir o número avg em formato decimal, use

out.write(String.valueOf(avg))
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top