Copy a file to clipboard - java

I'm trying to copy a file into the system's clipboard. As i found in the internet I'm trying to achieve this like this:
final List<File> files = new ArrayList<File>();
files.add(new File("pathToFile"));
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(
new Transferable() {
#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 {
return files;
}
}, null);
However after I run that code I'm not able fo paste the file from clipboard (tried on the desktop and in the system explorer). I have even tried to put in a sleep afterwards because I have read that the JVM must be running in order to be ablke to paste that clipboard content but it didn't work for me.
Nevertheless Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null); is returning the respective file I copied into clipboard.
One thing that I did find out was that Toolkit.getDefaultToolkit().getSystemClipboard().isDataFlavorAvailable(DataFlavor.javaFileListFlavor); returns false. Does that mean that the clipboard does not support files? That wouldn't make sense as I am able to copy paste files in the explorer using Ctlr+C and Ctrl+V.
I don't know if it's important but I'm running my tests under Linux Mint.
Can anyone explain to me why I can't paste that copied file or how I have to copy it in order to be able to paste it via Ctrl+V system wide?

You can use Terminal Command
String[] args = new String[] {"/bin/bash", "-c","Your command at terminal", "with", "args"};
Process proc = new ProcessBuilder(args).start();

Related

Showing a list of only filenames while having full file paths connected

I would like to be able to display a list of files showing only the file name not the whole file path.
Currently I have a list of files. When I click one of these files the listener passes this to a method out of scope which loads the file.
This means if I was to just pass it a list of just file names it would no longer work as my listener requires a full file path. I do not have any ideas as to how I would go about both storing a list of filenames whilst simultaneously linking them to the full file path.
Happy to answer any questions you may have. Many thanks,
Note: the small for loop shows how I could potentially extract the filename from the file path, but I am not currently doing anything with it at this time. It's just an example to show you how far I have gotten.
public void GetFilesFromFolder(String dirName) throws IOException {
File dir = new File(dirName);
File[] files = dir.listFiles((File dir1, String filename) -> filename.endsWith(".mp3"));
String[] fileName = new String[files.length];
int x = 0;
for (File file : files) {
String fileTemp = file.toString();
fileTemp = fileTemp.substring(fileTemp.lastIndexOf("\\" + 1));
System.out.println(fileTemp);
fileName[x] = fileTemp;
System.out.println(fileName[x]);
x++;
}
observableList.clear();
observableList.addAll(files);
}
public void SetFileListView() throws IOException {
listView.setItems(null);
}
public VBox listStack() throws IOException {
vbox = new VBox();
vbox.getChildren().add(listView);
listView.setItems(observableList);
listView.setMinHeight(500);
MusicDataModel mdm = MainView.getMainView().musicDataModel;
MusicDataViewController mdv = MainView.getMainView().musicDataViewController;
listView.getSelectionModel().selectedItemProperty().addListener((ObservableValue<? extends File> observable, File oldValue, File newValue) -> {
try {
mdm.load(newValue.toString());
mdv.SetValues();
} catch (UnsupportedTagException | InvalidDataException | IOException | NotSupportedException ex) {
Logger.getLogger(FileListView.class.getName()).log(Level.SEVERE, null, ex);
}
});
return vbox;
}
Populate the list view with Files, as you currently do, and use a cell factory on the list view to change the way the file is displayed:
listView.setCellFactory(lv -> new ListCell<File>() {
#Override
protected void updateItem(File file, boolean empty) {
super.updateItem(file, empty);
setText(file == null ? null : file.getName());
}
});
This will ensure each cell in the list view displays only the file name (the last component of the file's full path), though it still retains the File instance as its data (so you can still get the selected File, etc).

How to select all files in a FileDialog?

I want to select all files to exclude them from being displayed in my FileDialog.
FileDialog fileDialog = new FileDialog(this, "Some Title", FileDialog.LOAD);
fileDialog.setFilenameFilter(new FilenameFilter() {
#Override
public boolean accept(File dir, String name) {
if(name.endsWith(".*")) {
return false;
}else {
return true;
}
}
});
fileDialog.setVisible(true);
In my code you can see, that I am trying to to that with the String ".*", to select all files. This doesn't work however and I don't know why.
I only want to show directories.
Thanks for your help!
You can use a JFileChooser, using a FileFilter to check the File object to see if it is a directory
#Override
public boolean accept( File file ) {
return file.isDirectory();
}
A FileDialog's FileFilter should work similarly. Also note the API for the FileDialog's setFileFilter method:
"Filename filters do not function in Sun's reference implementation for Microsoft Windows."
As previously stated, I found the answer to this question with the help of #JigarJoshi.
This is the working code to show ONLY Directories on a AWT FileDialog:
fileDialog.setFilenameFilter(new FilenameFilter() {
#Override
public boolean accept(File dir, String name) {
return dir.isFile();
}
});
Please note that using FileDialog over JFileChooser is only recommendet, if you are on a non-Windows system. However on Mac and Linux, you should prefer to use FileDialog since it looks more native.
Thank you very much for your input!

Java - Paste a file from clipboard after it has been downloaded

I have some trouble with the Windows Explorer.
My Application starts the download of a file and puts it into the system clipboard meanwhile. If the User now pastes the file at some place of the Windows Explorer, the file isn't having its real size, but the size depending on the progress of the download.
Does somebody have an idea of how I could advise the Explorer to wait until the download has finished and copy the file to the target directory afterwards?
I also tried to "tunnel" the file over the loopback Interface using the local SMB server but it didnt help unfortunately...
Thanks in advance
Downloading the file and calling the paste to clipboard method:
new Thread(new Runnable() {
#Override
public void run() {
final String where = getText();
int selectedRows[] = CustomJTableModel.table.getSelectedRows();
List<File> fileList = new ArrayList<File>();
for( int i=0; selectedRows.length>i; i++ ) {
// Build the relative file/folder path
String fileName = (String)table.getValueAt(table.getSelectedRow(), 0);
final String value = buildPath(where, fileName);
new Thread(new Runnable() {
#Override
public void run() {
staticNetworkDaemon.getFile(value, where);
}
}).start();
fileList.add(new File("\\\\127.0.0.1\\r$\\downloaded" + value.replace("/", "\\")));
}
setClipboardFile(fileList);
}
}).start();
Copying the file to the clipboard:
public static void setClipboardFile(final List<File> files) {
Transferable trans = new Transferable() {
List<File> fileList = new ArrayList<File>();
public DataFlavor[] getTransferDataFlavors() {
for( File f: files ) {
fileList.add(f);
}
return new DataFlavor[] { DataFlavor.javaFileListFlavor };
}
public boolean isDataFlavorSupported(DataFlavor flavor) {
return DataFlavor.javaFileListFlavor.equals(flavor);
}
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if (isDataFlavorSupported(flavor))
return fileList;
throw new UnsupportedFlavorException(flavor);
}
};
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(trans, null);
}
What you're doing is simply wrong. Don't put something in the clipboard unless it's ready to be pasted -- you're breaking the contract that you should be following when using the clipboard.
Maybe my answer is a little bit simplistic, but why not simply wait until the file is completely downloaded before setting it in the clipboard? What about using something like a Future to wait for it, then set it in the clipboard.
This...
fileList.add(new File("\\\\127.0.0.1\\r$\\downloaded" + value.replace("/", "\\")))
Is so wrong as to be mind boggling. You only want to add the where to the fileList but only AFTER it has been successfully downloaded.
Once the file has been downloaded, only then do you want to add it to the clipboard.
The creation of your Transferable is also a "little" troubling, the Transferable should make a copy of the File List the moment it is created, this prevents the List from been modified before it is used, which could change the results
In languages like C++ or Delphi it is possible to put to clipboard IDataObject object. IDataObject object can contain absolutely different formats. If your can paste IDataObject with CFSTR_FILEDESCRIPTOR and CFSTR_FILECONTENTS formats to clipboard your problem will be solved.
When shell requests files from clipboard it checks for CF_HDROP, CF_IDLIST, CF_FILEDESCRIPTORW/CF_FILEDESCRIPTORA or CF_FILENAMEW/CF_FILENAMEA formats. And if clipboard contains CF_FILEDESCRIPTOR format only shell will use this format for file transfer.
CF_FILEDESCRIPTOR format contains names, attributes and dates of files. CF_FILECONTENTS format contains IStream of file. And you are absolutely free to create any implementation of IStream object. When shell call IStream.Read and request the part of file which is already downloaded just return this part of file. But when shell call IStream.Read and request the part of file which is not downloaded yet - just wait and after the part is downloaded return it.

List all files of one type in android

I need to retrieve a list of all files of a certain type on my internal and external storage. I found this (-> List all of one file type on Android device?) example, but it's very mp3 specific.
My app creates a .zip file which is renamed to the extension *.message
Another activity should display a list of all available .message files, from which the user can choose one to open it.
Does anyone has an idea how to begin?
Thanks!
To obtain a list of files with a specific extension you can use File.list() with a FilenameFilter. For example:
File dir = new File(".");
String[] names = dir.list(
new FilenameFilter()
{
public boolean accept(File dir, String name)
{
return name.endsWith(".message");
}
});
Note that the returned strings are file names only, they do not contain the full path.
Here it is
File root = new File(globalVar.recordDir);
{
try {
if (root.listFiles().length > 0) {
for (File file : root.listFiles()) {
if (file.getName().endsWith(".mp3")) {
//DO somthings with it
}
}
}
} else {
}
} catch (Exception e) {
}
}

Drag and Drop compressed files from swing to native [update]

I am trying to move files from a compressed archive to the native system (basycally, windows' eplorer) through drag and drop operations.
My only idea at this moment is to create a TransferHandler, which, when launched, will decompress the file in a temporary directory and set up that file as Transferable. Here is a snippet of code to make myself more clear:
private class FileTransferHandler extends TransferHandler {
protected Transferable createTransferable(JComponent c) {
List<File> files = new ArrayList<File>();
try {
File temp = createTempDirectory();
String path = temp.getAbsolutePath();
decompressTo(path);
files.add(new File(path));
} catch (Exception e) { e.printStackTrace(); };
return new FileTransferable(files);
}
public int getSourceActions(JComponent c) {
return COPY;
}
}
private class FileTransferable implements Transferable {
private List<File> files;
public FileTransferable(List<File> files) {
this.files = files;
}
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{ DataFlavor.javaFileListFlavor };
}
public boolean isDataFlavorSupported(DataFlavor flavor) {
return flavor.equals(DataFlavor.javaFileListFlavor);
}
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if (!isDataFlavorSupported(flavor)) {
throw new UnsupportedFlavorException(flavor);
}
return files;
}
}
(not valid anymore: The thing that puzzles me is that this way it somehow works: but only if after I release the mouse button I am not doing anything else.)
update: After more testing I observed that actually the file data is transferred from the temp directory to destination after I click in a zone that accepts that DataFlavor. If I click in a folder, the temp files are transferred to that folder. If I click in the console window, the path to the temp file appears in the console window.
So, if you please, I would like some pointers to direct me in the right way.
p.s.: the idea with decompressing to temp folder first came after observing that WinRar is doing the same thing.
p.p.s.: sorry if the question seems stupid, but I am mostly a web programmer just dabbling in desktop programming.
Well, this is apparently nearing the best results you can get.
I'll check your code when I have some time for it.

Categories

Resources