将大量字符串写入java中的文件

-3 java

我试图在java中运行此代码.但是,当我打开文件时,它完全是空的.我不明白为什么,需要帮助.出于某些图像处理目的,我尝试用RGB值来提取每个像素.为什么我的文件是空的?

        class Pixel {
           BufferedImage image;
           int width;
           int height;

           public Pixel() {
              try {
                 File input = new File("originalLeopard.jpg");
                 image = ImageIO.read(input);
                 width = image.getWidth();
                 height = image.getHeight();
                 BufferedWriter writer = new BufferedWriter(new FileWriter("pixy.txt"));

                 for(int y=0; y < height; y++){

                    for(int x=0; x <width; x++){

                       Color c = new Color(image.getRGB(y, x));
                       writer.write(y + " " + x +" "+ c.getRed()+ " " + c.getGreen()+ " "+ c.getBlue()+"\n");

                    }
                 }
                 writer.close();
              }

              catch (Exception e) {

              }

           }

           static public void main(String args[]) throws Exception 
           {
              Pixel obj = new Pixel();
           }
        }
Run Code Online (Sandbox Code Playgroud)

dub*_*gbu 5

您正在捕获所有异常但不打印堆栈跟踪.这会告诉你你的代码正在抛出:

java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
at sun.awt.image.ByteInterleavedRaster.getDataElements(Unknown Source)
at java.awt.image.BufferedImage.getRGB(Unknown Source)
at com.unionsystemsltd.optimus.secure.jwt.Pixel.<init>(Pixel.java:35)
at com.unionsystemsltd.optimus.secure.jwt.Pixel.main(Pixel.java:51)
Run Code Online (Sandbox Code Playgroud)