java.io.FileNotFoundException:C:\ Users \ Lance Dean \ Desktop \ Java \ End.jpg(访问被拒绝)

时间:2017-12-30 04:31:12

标签: java

当我运行代码时,文件显示并消失,并显示此错误。它在代码执行过程中消失。The error shows up

Java文件夹只有Sample.jpg,ColorGet.java和ColorGet.class

import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.lang.*;

public class ColorGet{
  public static void main(String args[])throws IOException{
    BufferedImage img = null;
    File f = null;
    File m = null;
    int c = 0;
    try{
      f = new File("C:\\Users\\Lance Dean\\Desktop\\Java\\Sample.jpg");
      img = ImageIO.read(f);
    }catch(IOException e){
      System.out.println(e);
    }
    int width = img.getWidth();
    int height = img.getHeight();
    for (int i=0; i < width;i++){
        for (int j=0; j < height;j++){
            int p = img.getRGB(i,j);
            int r = (p>>16) & 0xff;
            int g = (p>>8) & 0xff;
            int b = p & 0xff;
            c++;
            System.out.println(c);
            int a = 4 * (int)(Math.floor(255/4));
            int x = 4 * (int)(Math.floor((double)(r/4)));
            int y = 4 * (int)(Math.floor((double)(g/4)));
            int z = 4 * (int)(Math.floor((double)(b/4)));
            p = (a<<24) | (x<<16) | (y<<8) | z;
            img.setRGB(i, j, p);
            try{
                m = new File("C:\\Users\\Lance Dean\\Desktop\\Java\\End.jpg");
                if (!m.canRead()){
                    m.setReadable(true);
                }
                ImageIO.write(img, "jpg", m);
            }catch(IOException e){
                System.out.println(e);
            }
        }
    }
  }
}

1 个答案:

答案 0 :(得分:0)

您的代码中有很多IO操作。假设图像是400x300,那么你已经打开并写入了12k次文件。由于IO操作可能会阻塞,因此如果文件被先前的操作阻止,您可能会面临访问被拒绝的问题。最好只在最后做一次写操作。

public static void main(String args[]) throws IOException {
    BufferedImage img = null;
    File f = null;
    File m = null;
    int c = 0;
    try {
        f = new File("C:\\Users\\Lance Dean\\Desktop\\Java\\Sample.jpg");
        img = ImageIO.read(f);
    } catch (IOException e) {
        System.out.println(e);
    }
    int width = img.getWidth();
    int height = img.getHeight();
    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {
            int p = img.getRGB(i, j);
            int r = (p >> 16) & 0xff;
            int g = (p >> 8) & 0xff;
            int b = p & 0xff;
            c++;
            System.out.println(c);
            int a = 4 * (int) (Math.floor(255 / 4));
            int x = 4 * (int) (Math.floor((double) (r / 4)));
            int y = 4 * (int) (Math.floor((double) (g / 4)));
            int z = 4 * (int) (Math.floor((double) (b / 4)));
            p = (a << 24) | (x << 16) | (y << 8) | z;
            img.setRGB(i, j, p);
        }
    }
    try {
        m = new File("C:\\Users\\Lance Dean\\Desktop\\Java\\End.jpg");
        if (!m.canRead()) {
            m.setReadable(true);
        }
        ImageIO.write(img, "jpg", m);
    } catch (IOException e) {
        System.out.println(e);
    }
}