2011-05-23 74 views
3

如何複製文件並將其粘貼到使用Java的剪貼板?我的程序可以複製但不能粘貼。它給如何複製文件並使用Java將其粘貼到剪貼板?

異常在線程 「主要」 java.lang.ClassCastException:
java.util.Arrays中的$ ArrayList中不能轉換成java.io.File

我的代碼:

class FileTransferable implements Transferable { 
    private final File file; 

    public FileTransferable(File file) { 
     this.file = file; 
    } 

    @Override 
    public DataFlavor[] getTransferDataFlavors() { 
     return new DataFlavor[] { DataFlavor.javaFileListFlavor }; 
    } 

    @Override 
    public boolean isDataFlavorSupported(DataFlavor flavor) { 
     return DataFlavor.javaFileListFlavor.equals(flavor); 
    } 

    @Override 
    public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { 
     final ArrayList<File> files = new ArrayList<File>(); 
     files.add(file); 
     return files; 
    } 
} 
+0

我不認爲異常由部分代碼拋出了我已刪除的習慣或具體細節你已包括在內。請檢查例外的行號,並將相關部分的代碼添加到您的問題中。 – Pops 2011-05-23 19:04:23

+0

你爲什麼要返回一個ArrayList?我不是很熟悉java的這個領域,但考慮到異常涉及從ArrayList轉換......也許嘗試直接返回'file'? – ApproachingDarknessFish 2013-03-19 18:08:36

回答

-6

Java無法處理特定的操作系統相關操作,如干擾剪貼板等,因爲它是獨立於平臺的語言。在JLS和Java API中,沒有用於剪貼板干涉的方法,但是有一些方法可以處理在Java API中聲明的文件系統操作,作爲JLS(Java語言規範)的一部分,JLS實現在大多數JVM實現中。爲了做到這一點,您應該使用JNI(Java本地接口)本地開發C/C++(使用Win32 API等)或使用現成的二進制文件並在您的Java應用程序中調用它們。

+0

啊,來吧。剪貼板操作對Java來說不成問題。 (-1表明它在... C/C++ *不寒而慄*) – 2011-05-23 19:10:23

+0

Java可以處理剪貼板嗎? – 2011-05-23 19:35:39

+1

是的。 http://download.oracle.com/javase/1.4.2/docs/api/java/awt/Toolkit.html#getSystemClipboard%28%29 – Jberg 2011-05-23 19:39:24

0

我無法在實際代碼中檢測到任何問題。我想,這個投射錯誤發生在你實際上收到的內容從複製/粘貼操作中,你收到一個文件,包裹在一個列表中,並且可能會將列表投到File

0

下面是從DGuitar源代碼採取一個完整的示例根據您的評論

/* 
* ADropTargetListener 
* Created on 20/04/2005 
* 
* 
*/ 

import java.awt.datatransfer.DataFlavor; 
import java.awt.datatransfer.Transferable; 
import java.awt.datatransfer.UnsupportedFlavorException; 
import java.awt.dnd.DnDConstants; 
import java.awt.dnd.DropTargetDragEvent; 
import java.awt.dnd.DropTargetDropEvent; 
import java.awt.dnd.DropTargetEvent; 
import java.awt.dnd.DropTargetListener; 
import java.io.File; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 


/** 
* The Drop action happens in this order: 1. dragEnter = 
when the mouse enters 
* the component 2. dragOver = after the mouse has entered 
but has not been 
* released 3. drop = when the mouse is released 
* 
* @author Mauricio Gracia G 
*/ 
public class ADropTargetListener implements DropTargetListener { 
    /** 
    * creates a DropTargetListener 
    */ 
    public ADropTargetListener() { 

    } 

    /** 
    * overrides the DROP method. 
    * 
    * @see java.awt.dnd.DropTargetListener#drop(java.awt.dnd.DropTargetDropEvent) 
    */ 
    public void drop(DropTargetDropEvent dtde) { 
     Transferable transfer; 

     transfer = dtde.getTransferable(); 

     //we must accept the transfer to process it 
     dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); 

     importData(transfer); 
    } 
    /** 
    * Imports the data of the Drop action 
    * @param t the transferable object 
    */ 

    public void importData(Transferable t) { 
     if (canImport(t.getTransferDataFlavors())) { 
      try { 
       DataFlavor[] flavors = t.getTransferDataFlavors(); 
       for (int i = 0; i < flavors.length; i++) { 
        //  Drop from Windows 
        if (DataFlavor.javaFileListFlavor.equals(flavors[i])) { 

         Object gtd ; 

         gtd = t.getTransferData(DataFlavor.javaFileListFlavor) ; 
         if (gtd instanceof List) { 
          @SuppressWarnings("unchecked") 
          List<File> fileList = (List<File>) gtd; 

                 //YOUR CODE here to process the list of Files 

         } 

        } 
        //  Drop from GNOME or kde 
        // delimited by \n (\r\n) on gnome 
        // will need to remove file:// at start 
        else if (DataFlavor.stringFlavor.equals(flavors[i])) { 
         if (t.getTransferData(DataFlavor.stringFlavor) 
instanceof String) { 
          String path = (String) t 
            .getTransferData(DataFlavor.stringFlavor); 
          List<File> fileList = convertStringsToFileList(path); 
          //YOUR CODE here to process the list of Files 

         } 

        } 
       } 
      } catch (UnsupportedFlavorException e) { 
       e.printStackTrace(); 

      } catch (IOException e) { 
       e.printStackTrace(); 

      } 
     } 
    } 
    /** 
    * converts Strings in certain format to a FileList 
    * @param filestr the list of files in a single string 
    * @return   a list of files 
    */ 

    private ArrayList<File> convertStringsToFileList(String filestr) { 
     ArrayList<File> files = new ArrayList<File>(); 
     String[] tokenizedFiles = filestr.split("\n"); 
     for (int i = 0; i < tokenizedFiles.length; i++) { 
      String path = tokenizedFiles[i]; 
      if (path.startsWith("file://")) { 
       if (path.endsWith("\r")) { 
        //appears to be the case for gnome but not kde 
        path = path.substring(7); 
        path = path.replaceAll("\r", ""); 

        path = path.replaceAll("%20", " "); 
       } else { 
        path = path.substring(7); 
       } 
      } 
      files.add(new File(path)); 

     } 
     return files; 
    } 
    /** 
    * Determine if it can import certain flavor 
    * 
    * @param flavors an array of DataFlavors 
    * @return true or not depending on the flavor 
    */ 
    public boolean canImport(DataFlavor[] flavors) { 
     for (int i = 0; i < flavors.length; i++) { 
      if (DataFlavor.javaFileListFlavor.equals(flavors[i])) { 
       return true; 
      } else if (DataFlavor.stringFlavor.equals(flavors[i])) { 
       return true; 
      } 
     } 
     return false; 
    } 


    /** 
    * method that is called when the drag starts or enters the window 
    * @param dtde a DropTargetDragEvent 
    * 
    * @see java.awt.dnd.DropTargetListener#dragEnter(java.awt.dnd.DropTargetDragEvent) 
    */ 
    public void dragEnter(DropTargetDragEvent dtde) { 
     //DEBUG 
     //System.out.println("dragEnter" + ":" + dtde) ; 
    } 

    /** 
    * a method that is called when the DRAG is OVER ?? 
    * 
    * @see java.awt.dnd.DropTargetListener#dragOver(java.awt.dnd.DropTargetDragEvent) 
    */ 

    public void dragOver(DropTargetDragEvent dtde) { 
     //  DEBUG 
     //System.out.println("dragOver" + ":" + dtde) ; 
    } 

    /** 
    * when the drop action is changes this method is called 
    * 
    * @see java.awt.dnd.DropTargetListener#dropActionChanged(java.awt.dnd.DropTargetDragEvent) 
    */ 
    public void dropActionChanged(DropTargetDragEvent dtde) { 
     //  DEBUG 
     //System.out.println("dropActionChanged" + ":" + dtde) ; 
    } 

    /** 
    * method called when the drag exits the window 
    * 
    * @see java.awt.dnd.DropTargetListener#dragExit(java.awt.dnd.DropTargetEvent) 
    */ 
    public void dragExit(DropTargetEvent dte) { 
     //  DEBUG 
     //System.out.println("dragExit" + ":" + dte) ; 
    } 

} 
+0

@camickr作爲答案說的是一個例子,我更新了代碼,以便它只使用標準API。在這裏查找說你的代碼的行來處理文件列表 – 2015-07-05 01:08:17

相關問題