Example usage for java.nio.file FileAlreadyExistsException FileAlreadyExistsException

List of usage examples for java.nio.file FileAlreadyExistsException FileAlreadyExistsException

Introduction

In this page you can find the example usage for java.nio.file FileAlreadyExistsException FileAlreadyExistsException.

Prototype

public FileAlreadyExistsException(String file, String other, String reason) 

Source Link

Document

Constructs an instance of this class.

Usage

From source file:umich.ms.batmass.filesupport.core.api.ExternalFileImporter.java

/**
 * Should create a descriptor file in the project under a sub-folder.
 * E.g. <code>$PROJ_DIR/msfiles/example.mzxml.file_desc</code> for
 * <code>C:/example.mzxml</code>.<br/>
 * (NOTE: for the exact extension used for the descriptors, see
 * {@link FileDescriptor#EXT}).<br/>
 * Use ProjectSubfolderProvider to get a concrete sub-folder in the project,
 * never search for a folder in a project manually.
 * @param origFile java.io.File for the original file being imported
 * @param destinationFolder a directory in $PROJECT_DIR into which to import the file
 * @param fileType can be null, but then the import wont be too useful
 * @param fileCategory can be null, but then the import wont be too useful
 * @return the newly created descriptor file or
 * @throws java.nio.file.FileAlreadyExistsException
 * @throws org.apache.commons.configuration.ConfigurationException
 *//*from w w  w. j  a  va2  s .c o  m*/
public static FileObject importFile(File origFile, DataFolder destinationFolder, String fileType,
        String fileCategory) throws FileAlreadyExistsException, IOException, ConfigurationException {
    // first create java.io.File in that DataFolder, and check if the actual file exists;
    FileObject origFo = FileUtil.toFileObject(origFile);
    long origFileSize = origFo.getSize();

    String origFileName = origFo.getNameExt();
    String descFileExt = (FileDescriptor.EXT).toLowerCase();
    String descFileName = PathUtils.filename(origFileName, descFileExt);

    FileObject descFo = destinationFolder.getPrimaryFile().getFileObject(origFileName, descFileExt);
    // if the descriptor does not yet exist for this orignial file, then create it,
    // if the file is already there, just skip it
    //
    // TODO: we should ask the user, if he wants to delete existing file
    //       and reimport a new copy, deleting all the associated processing
    //       data and the likes.
    if (descFo != null) {
        throw new FileAlreadyExistsException(origFile.getAbsolutePath(), // imported file
                FileUtil.toFile(descFo).getAbsolutePath(), // the descriptor we wanted to craete
                "The file has already been imported into that directory"); // reason for error
    }

    // the folder into which we're importing
    File folderFile = FileUtil.toFile(destinationFolder.getPrimaryFile());
    // full path of the file, into which we'll store the descriptor
    Path descFilePath = Paths.get(folderFile.getAbsolutePath(), descFileName);

    // create and save the descriptor
    FileDescriptor desc = new FileDescriptor(origFile.toPath().toAbsolutePath(), origFileSize);
    if (fileType != null) {
        desc.setFileType(fileType);
    }
    if (fileCategory != null) {
        desc.setFileCategory(fileCategory);
    }
    FileDescriptor.writeToFile(descFilePath.toFile(), desc);

    // craete the FileObject and hope that ProjectExplorer sees it
    descFo = FileUtil.createData(descFilePath.toFile());
    return descFo;
}

From source file:edu.rit.flick.AbstractFlickFile.java

private void defaultDeflationInflationVerification() throws Exception {
    // Verify the input file exists
    if (!fileIn.exists())
        throw new NoSuchFileException(fileIn.getPath(), null, FILE_NOT_FOUND_EXCEPTION_MESSAGE);

    // Verify the output file does not exist
    if (fileOut.exists()) {
        // Since file does exist:

        // Verify the force is on
        if (!configuration.getFlag(FORCE_FLAG))
            throw new FileAlreadyExistsException(fileIn.getPath(), fileOut.getPath(),
                    CANT_OVERWRITE_EXISTING_FILE_WITHOT_FORCE_FLAG);

        // Verify the output file is not a directory
        if (fileOut.isDirectory())
            throw new FileAlreadyExistsException(FILE_ALREADY_EXISTS_AS_DIRECTORY_EXCEPTION);

        FileUtils.deleteQuietly(fileOut);
    }/*  w w w .j  a  v  a2 s.co m*/

    // Subclass's verification
    deflationInflationVerification();
}

From source file:org.cryptomator.ui.model.Vault.java

public void create(CharSequence passphrase) throws IOException {
    try {/*from   w  ww  .  java 2s . c  o  m*/
        FileSystem fs = getNioFileSystem();
        if (fs.files().map(File::name).filter(s -> s.endsWith(VAULT_FILE_EXTENSION)).count() > 0) {
            throw new FileAlreadyExistsException("masterkey.cryptomator", null, "Vault location not empty.");
        } else if (fs.folders().count() > 0) {
            throw new DirectoryNotEmptyException(fs.toString());
        }
        cryptoFileSystemFactory.initializeNew(fs, passphrase);
    } catch (UncheckedIOException e) {
        throw new IOException(e);
    }
}

From source file:de.tiqsolutions.hdfs.HadoopFileSystemProvider.java

static void rethrowRemoteException(RemoteException e, Path p1, Path p2) throws IOException {
    switch (e.getClassName()) {
    case "org.apache.hadoop.fs.PathIsNotEmptyDirectoryException":
        throw new DirectoryNotEmptyException(p1.toString());

    case "org.apache.hadoop.fs.PathExistsException":
    case "org.apache.hadoop.fs.FileAlreadyExistsException":
        throw new FileAlreadyExistsException(Objects.toString(p1), Objects.toString(p2),
                e.getLocalizedMessage());

    case "org.apache.hadoop.fs.PathPermissionException":
    case "org.apache.hadoop.fs.PathAccessDeniedException":
        throw new AccessDeniedException(Objects.toString(p1), Objects.toString(p2), e.getLocalizedMessage());

    case "org.apache.hadoop.fs.ParentNotDirectoryException":
    case "org.apache.hadoop.fs.DirectoryListingStartAfterNotFoundException":
    case "org.apache.hadoop.fs.PathIsNotDirectoryException":
        throw new NotDirectoryException(Objects.toString(p1));

    case "org.apache.hadoop.fs.PathIsDirectoryException":
    case "org.apache.hadoop.fs.InvalidPathException":
    case "org.apache.hadoop.fs.PathNotFoundException":
        throw new NoSuchFileException(Objects.toString(p1), Objects.toString(p2), e.getLocalizedMessage());

    case "org.apache.hadoop.fs.UnresolvedLinkException":
        throw new NotLinkException(Objects.toString(p1), Objects.toString(p2), e.getLocalizedMessage());

    case "org.apache.hadoop.fs.PathIOException":
    case "org.apache.hadoop.fs.ChecksumException":
    case "org.apache.hadoop.fs.InvalidRequestException":
    case "org.apache.hadoop.fs.UnsupportedFileSystemException":
    case "org.apache.hadoop.fs.ZeroCopyUnavailableException":

    }//from w  w w  .  j a v a  2 s . c  o m

    throw new IOException(e.getLocalizedMessage(), e);
}

From source file:org.zaproxy.zap.extension.autoupdate.ExtensionAutoUpdate.java

private static File copyAddOnFileToLocalPluginFolder(File file) throws IOException {
    if (isFileInLocalPluginFolder(file)) {
        return file;
    }/*from  w w w.  j a va  2s  . co  m*/

    File targetFile = new File(Constant.FOLDER_LOCAL_PLUGIN, file.getName());
    if (targetFile.exists()) {
        throw new FileAlreadyExistsException(file.getAbsolutePath(), targetFile.getAbsolutePath(), "");
    }

    FileCopier fileCopier = new FileCopier();
    fileCopier.copy(file, targetFile);

    return targetFile;
}

From source file:de.haber.xmind2latex.XMindToLatexExporter.java

/**
 * Stores the given <b>content</b> into the configured target file.
 *  /*  ww  w  . ja va 2  s  .c  om*/
 * @param content content to save
 * @throws IOException either writer {@link IOException}, or if the target file already exists and fore overwrite is not enabled.
 */
private void save(String content) throws IOException {
    File tf = getTargetFile();
    if (tf.getParentFile() != null && !tf.getParentFile().exists()) {
        tf.getParentFile().mkdirs();
    }
    if (!tf.exists() || isOverwriteExistingFile()) {
        PrintWriter pw = new PrintWriter(tf, "UTF-8");
        pw.write(content);
        pw.close();
    } else {
        throw new FileAlreadyExistsException(tf.getAbsolutePath(), "",
                "If you want to overwrite existing files use param " + CliParameters.FORCE);
    }
}