Java 写入文件行为异常

发布于 2024-09-29 06:18:34 字数 1528 浏览 7 评论 0原文

这是我的另一个 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 同时实例化时是否存在一些问题?我使用相同的过程写入另一个类中的文件,其中打开一个文件进行读取,打开另一个文件进行写入,并且一切正常。

ps 如何在不破坏代码的情况下输入 &lt 字符???我需要将其输入为 &是吗?

谢谢

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

 } 

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 < character without breaking the code??? Do I need to enter it as & lt ;?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

屌丝范 2024-10-06 06:18:34

您调用

out.write(avg)

将 int avg 写入输出。

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

要以十进制格式打印数字avg,请使用

out.write(String.valueOf(avg))

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))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文