title: Automatically capture clip board data and save it into a file
body: <p>Capture a image from the clipboard whenever Print screen is pressed and save it in  a file (.doc) using java</p>
  
  <p>Main aim is to copy the data from the clipboard and automatically save it into local disk without going to the desired program (i.e MS Word)- click new - pressing (Ctrl+V) to paste and save it with a name. </p>
  
  <p>The code should perform all of the above three steps automatically. </p>
  
  <p>My Source Code</p>
  
  <pre><code>public class CaptureScreenShot {
  
      private static String DIR  ="C:\\QUIS\\";
      private static JTextField txtDocNumber;
      public static void main(String[] args) throws Exception{
          txtDocNumber = new JTextField();
          Robot robot = new Robot();
  
  
          Dimension d = new Dimension(Toolkit.getDefaultToolkit().getScreenSize());
          int width = (int) d.getWidth();
          int height = (int) d.getHeight();
  
          robot.delay(5000);
  
          Image image = robot.createScreenCapture(new Rectangle(0, 0, width,
                  height));
          BufferedImage  bi = new BufferedImage(width, height,
                  BufferedImage.TYPE_INT_RGB);
          Graphics g = bi.createGraphics();
          g.drawImage(image, 0, 0, width, height, null);
  
          String fileNameToSaveTo = "C:/QUIS/screenCapture_" + createTimeStampStr() + ".PNG";
          String newFile = "C:/QUIS/x" + ".org";
          File newFilee = new File(newFile);
  
          writeImage(bi, fileNameToSaveTo, "PNG");
  
          System.out.println("Screen Captured Successfully and Saved to:\n"+fileNameToSaveTo);
  
          Desktop desktop = Desktop.getDesktop();  
          writeImage(bi, newFile, "org");
          desktop.open(newFilee); 
      }
  
      public static int writeImage(BufferedImage img, String fileLocation,
              String extension) {
          try {
              BufferedImage bi = img;
              File outputfile = new File(fileLocation);
              ImageIO.write(bi, extension, outputfile);
          } catch (IOException e) {
              e.printStackTrace();
          }
          return 1;
      } 
      public static String createTimeStampStr() throws Exception {
          Calendar mycalendar = Calendar.getInstance();
          SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd_hhmmss");
          String timeStamp = formatter.format(mycalendar.getTime());
  
          return timeStamp;
      }
  }
  </code></pre>