How To Set And Get System Clipboard Contents In Java

The clipboard is a software facility that can be used for short-term data storage and/or data transfer between documents or applications, via copy and paste operations. It is most commonly a part of a GUI environment and is usually implemented as an anonymous, temporary block of memory that can be accessed from most or all programs within the environment via defined programming interfaces. A typical application accesses clipboard functionality by mapping user input (keybindings, menu selections, etc.) to these interfaces. [Wikipedia]

The following tutorial demonstrate how to read the content of a clipboard or set data in the clipboard.

package com.kushal.utils;

 * @Author Kushal Paudyal
 * Last Modfiied On 31st July 2009
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;

public class SystemClipboardDataManipulate {

	 * When you do a cut or copy of text in the operating system, the text is
	 * stored in the clipboard.
	 * The following method returns the content that is currently in the
	 * clipboard.
	public static String getClipboardData() {
		String clipboardText;
		Transferable trans = Toolkit.getDefaultToolkit().getSystemClipboard()

		try {
			if (trans != null
					&& trans.isDataFlavorSupported(DataFlavor.stringFlavor)) {
				clipboardText = (String) trans
				return clipboardText;
		} catch (Exception e) {
		return null;

	 * This method will set any parameter string to the system's clipboard.
	public static void setClipboardData(String string) {
		StringSelection stringSelection = new StringSelection(string);
				stringSelection, null);

	 * Testing the clipboard set and get methods.
	public static void main(String[] args) {

		 * If something is already there in the clipboard, printing the value.

		 * Setting our own clipboard data

		 * Printing the Clipboard Data We Just Set.



Here is the output (The first line depends on what is there in your system clipboard).
Most environments support a single clipboard transaction. Each cut or copy overwrites the previous contents.

  1. How to extract plain Text from HTML Website easily in Java
  2. How To Find What Java Version You Are Using?
  3. Computing the total, free and usable disk space easily using JDK 1.6
  4. Creating and java.util.sql objects from Calendar object
  5. Bar Code Generation In Java Using iText API
  6. Simple Tag Generator In Java For Music Blogs
  7. Tutorial on Converting an List of Strings or Numbers to a CSV file with optional sorting
  8. Calling URL Browser From Java Application
  9. Java Reflection Tutorial – List Methods Of A Class
  10. Cleaning Punctuation And White Spaces From A String
  11. Java Tutorial – Using JCIFS to copy files to shared network drive using username and password
  12. Prime Number Finder In Java
  13. Utility Class For Getting SQL Connection and Closing It
  14. Java Design Pattern – Singleton Pattern
  15. Reading / Writing File in Java and String Manipulation
  16. How to capture a screenshot using Java and save it to a file?
  17. Calculating The Difference Between Two Dates In Terms of Months
  18. Getting The Name Of Currently Logged In User In The System
  19. How To Set And Get System Clipboard Contents In Java
  20. RGB To Hex and Hex To RGB Conversion Using Java and Regular Expressions
  21. Generating Nepali Unicode Sequence For Characters
  22. Java Tool To Compare Two Lists and Spit Out The Differences – Working Example
  23. Replacing non-ASCII characters using Java Regular Expressions
  24. Calculating Folder Size In Java
Tagged , , , , , , , , . Bookmark the permalink.

3 Responses to Calculating Folder Size In Java

  1. Pingback: » Latest Updates

  2. Jamie says:

    This approach uses less memory:

    public static class SizeCounter implements FileFilter
    private long total = 0;
    public SizeCounter(){};
    public boolean accept(File pathname) {
    if ( pathname.isFile()) {
    } else {
    return false;
    public long getTotal()
    return total;

    private static long getFileOrDirectorySize(File file) {
    SizeCounter counter = new SizeCounter();
    return counter.getTotal();

  3. kushalzone says:

    Thank you Jamie for your optimized solution.

Leave a Reply

Your email address will not be published. Required fields are marked *