开发者

Java writing to file misbehaving

This is yet another of my Java questions.

Please take a look at this code:


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 = n开发者_开发技巧ew 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();  
  }

 } 

In output.txt I only get non-readable text, as if opening binary file for example.

Is there some king of problem when BufferedImage and BufferedWriter being instantiated simultaneously? I'm using the same procedure to write to file in another class, where one file is opened for reading, and another is opened for writing, and things work just fine.

p.s. How to enter &lt character without breaking the code??? Do I need to enter it as & lt ;?

Thanks


You invoke

out.write(avg)

to write the int avg to the output.

This will invoke the method BufferedWriter.write(int); this will write out the character with the Unicode code point avg. This is probably not what you want ;-).

To print the number avg in decimal format, use

out.write(String.valueOf(avg))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜