Java Code Examples for com.sun.jna.platform.FileUtils#getInstance()

The following examples show how to use com.sun.jna.platform.FileUtils#getInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Utils.java    From opsu with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Deletes a file or directory.  If a system trash directory is available,
 * the file or directory will be moved there instead.
 * @param file the file or directory to delete
 * @return true if moved to trash, and false if deleted
 * @throws IOException if given file does not exist
 */
public static boolean deleteToTrash(File file) throws IOException {
	if (file == null)
		throw new IOException("File cannot be null.");
	if (!file.exists())
		throw new IOException(String.format("File '%s' does not exist.", file.getAbsolutePath()));

	// move to system trash, if possible
	FileUtils fileUtils = FileUtils.getInstance();
	if (fileUtils.hasTrash()) {
		try {
			fileUtils.moveToTrash(new File[] { file });
			return true;
		} catch (IOException e) {
			Log.warn(String.format("Failed to move file '%s' to trash.", file.getAbsolutePath()), e);
		}
	}

	// delete otherwise
	if (file.isDirectory())
		deleteDirectory(file);
	else
		file.delete();
	return false;
}
 
Example 2
Source File: Utils.java    From opsu-dance with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Deletes a file or directory.  If a system trash directory is available,
 * the file or directory will be moved there instead.
 * @param file the file or directory to delete
 * @return true if moved to trash, and false if deleted
 * @throws IOException if given file does not exist
 */
public static boolean deleteMaybeToTrash(File file) throws IOException
{
	if (file == null)
		throw new IOException("File cannot be null.");
	if (!file.exists())
		throw new IOException(String.format("File '%s' does not exist.", file.getAbsolutePath()));

	// FileUtils is com.sun, soooo big ass try around it
	try {
		// move to system trash, if possible
		FileUtils fileUtils = FileUtils.getInstance();
		if (fileUtils.hasTrash()) {
			fileUtils.moveToTrash(new File[] { file });
			return true;
		}
	} catch (Throwable t) {
		softErr(t, "Failed to move file '%s' to trash.", file.getAbsolutePath());
	}

	// delete otherwise
	if (file.isDirectory())
		deleteDirectory(file);
	else
		file.delete();
	return false;
}