0

I have transferred my code from a method (button press action) to a new class. Its function is to capture screen image (much like print screen) and saves it somewhere in the computer. (in this case, drive c) It displays the following error message:

java.io.FileNotFoundException: c:\z\1.jpg (The system cannot find the path specified)

public class printScreen{

    public static void main(String args[]) throws AWTException, IOException
    {
        Robot robot = new Robot();

        Dimension a = Toolkit.getDefaultToolkit().getScreenSize();

        Rectangle rect = new Rectangle(a);

        BufferedImage img = robot.createScreenCapture(rect);

        ImageIO.write(img, "jpg", new File("c:/z/1.jpg"));
        ImageIO.write(img, "bmp", new File("c:/z/2.bmp"));
        ImageIO.write(img, "png", new File("c:/z/3.png"));
    }
}

Any thoughts? All help will greatly be appreciated! Thank you!

1 Answers1

0
File f = new File("c:/z/1.jpg")
f.createNewFile();
ImageIO.write(img, "jpg", f);
RamonBoza
  • 8,898
  • 6
  • 36
  • 48
  • 2
    `ImageIO#write` creates the file if it doesn't exist, so this solution won't fix the issue. The method will not, however, create the parent directories of the file if they do not exist, which is more likely the problem in this case. – FThompson Dec 11 '13 at 17:10