Copy a file to ClipBoard using java.util.Scanner



import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.Toolkit;
import java.io.*;
import java.util.Scanner;

public final class TextTransfer implements ClipboardOwner {

  public static void main (String...  aArguments ){
    TextTransfer textTransfer = new TextTransfer();
     
    String fileName = "";
    //use .\\ in defDir (3'rd parameter in below line) parameter to set default path opening window to eclipse    workspace clipboard
    fileName = textTransfer.loadFile(new Frame(), "", "", "");
    /*
     * In case user clicks on "Cancel" button of dialogue box, program will terminate
     * to avoid FileNotFoundException.
     */
    if(fileName.equalsIgnoreCase("nullnull")){
    System.exit(0);
    }
    else{
    textTransfer.readFile(fileName);
    System.out.println("Clipboard contains:" + textTransfer.getClipboardContents() );
    }
  }

   /**
   * Empty implementation of the ClipboardOwner interface.
   */
   public void lostOwnership( Clipboard aClipboard, Transferable aContents) {
     //do nothing
   }
 
   /**
    * Generates a FileDialog for user to select a file to load
    * @param f
    * @param title
    * @param defDir
    * @param fileType
    * @return
    */
   public String loadFile
    (Frame f, String title, String defDir, String fileType) {
 FileDialog fd = new FileDialog(f, title, FileDialog.LOAD);
 fd.setFile(fileType);
 fd.setDirectory(defDir);
 fd.setLocation(50, 50);
 fd.setVisible(true);
 return fd.getDirectory()+fd.getFile();
   }

   /**
    * reads the content of file by scanner and copies the same to clipboard
    * @param fileName
    */
   public void readFile(String fileName) {
  try {
   Scanner scanner = new Scanner(new File(fileName));
 
   // use \\z to read entire file by scanner
   scanner.useDelimiter("\\z");
   StringSelection stringSelection = new StringSelection( scanner.next() );
   Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
   clipboard.setContents( stringSelection, this );
   scanner.close();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }
 }
 
  /**
  * Place a String on the clipboard, and make this class the
  * owner of the Clipboard's contents.
  */
  public void setClipboardContents( String aString ){
    StringSelection stringSelection = new StringSelection( aString );
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    clipboard.setContents( stringSelection, this );
  }

  /**
  * Get the String residing on the clipboard.
  *
  * @return any text found on the Clipboard; if none found, return an
  * empty String.
  */
  public String getClipboardContents() {
    String result = "";
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    //odd: the Object param of getContents is not currently used
    Transferable contents = clipboard.getContents(null);
    boolean hasTransferableText =
      (contents != null) && contents.isDataFlavorSupported(DataFlavor.stringFlavor);
    if ( hasTransferableText ) {
      try {
        result = (String)contents.getTransferData(DataFlavor.stringFlavor);
      }
      catch (UnsupportedFlavorException ex){
        //highly unlikely since we are using a standard DataFlavor
        System.out.println(ex);
        ex.printStackTrace();
      }
      catch (IOException ex) {
        System.out.println(ex);
        ex.printStackTrace();
      }
    }
    return result;
  }
}

Comments

Popular posts from this blog

JMeter – Working with JSON

Hyperlocal Startups Creating a Better Living

Existence: Absolute & Relative