这是另一个我的Java问题。

请在此代码看一看:


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();  
  }

 } 

在output.txt的我只得到非可读文本,仿佛打开的二进制文件例如

是否有问题一定当国王和BufferedImage的被BufferedWriter将同时实例化?我使用的是相同的程序写入到文件中的其他类,其中一个文件被打开阅读,而另一个被打开写作,工作的事情就好了。

P.S。如何进入&LT字符没有打破代码???是否需要将其输入为&lt;?

由于

有帮助吗?

解决方案

您调用

out.write(avg)

写INT平均到输出。

这将调用方法BufferedWriter.write(int);这将写出与Unicode代码点avg的字符。这可能不是你想要的东西; - )

要打印的数量avg以十进制格式,使用

out.write(String.valueOf(avg))
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top