Example usage for java.io File canWrite

List of usage examples for java.io File canWrite

Introduction

In this page you can find the example usage for java.io File canWrite.

Prototype

public boolean canWrite() 

Source Link

Document

Tests whether the application can modify the file denoted by this abstract pathname.

Usage

From source file:com.dvlcube.cuber.utils.FileUtils.java

public static void copyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException {
    if (srcFile == null) {
        throw new NullPointerException("Source must not be null");
    }/* ww  w. j  a va2 s. c om*/
    if (destFile == null) {
        throw new NullPointerException("Destination must not be null");
    }
    if (srcFile.exists() == false) {
        throw new FileNotFoundException("Source '" + srcFile + "' does not exist");
    }
    if (srcFile.isDirectory()) {
        throw new IOException("Source '" + srcFile + "' exists but is a directory");
    }
    if (srcFile.getCanonicalPath().equals(destFile.getCanonicalPath())) {
        throw new IOException("Source '" + srcFile + "' and destination '" + destFile + "' are the same");
    }
    if (destFile.getParentFile() != null && destFile.getParentFile().exists() == false) {
        if (destFile.getParentFile().mkdirs() == false) {
            throw new IOException("Destination '" + destFile + "' directory cannot be created");
        }
    }
    if (destFile.exists() && destFile.canWrite() == false) {
        throw new IOException("Destination '" + destFile + "' exists but is read-only");
    }
    doCopyFile(srcFile, destFile, preserveFileDate);
}

From source file:fr.inrialpes.exmo.align.cli.GroupAggreg.java

public void run(String[] args) throws Exception {
    try {/*from  w  w  w.ja  v a  2 s  .  c  o m*/
        CommandLine line = parseCommandLine(args);
        if (line == null)
            return; // --help

        // Here deal with command specific arguments
        if (line.hasOption('t'))
            threshold = Double.parseDouble(line.getOptionValue('t'));
        if (line.hasOption('T'))
            cutMethod = line.getOptionValue('T');
        if (line.hasOption('m'))
            aggMethod = line.getOptionValue('m');

        if (line.hasOption('l')) {
            listAlgo = line.getOptionValues('l');
            size = listAlgo.length;
        }
        if (line.hasOption('w'))
            srcDir = line.getOptionValue('w');
        if (line.hasOption('o'))
            dirName = line.getOptionValue('o');
    } catch (ParseException exp) {
        logger.error(exp.getMessage());
        usage();
        System.exit(-1);
    }

    // check that dirName exist and is writable
    File outDir = new File(dirName);
    if (!outDir.isDirectory() || !outDir.canWrite()) {
        logger.error("Directory {} must exist and be writable", dirName);
        throw new AlignmentException("Cannot output to " + dirName);
    }

    // Run it
    iterateDirectories();
}

From source file:org.seedstack.seed.core.internal.diagnostic.DefaultDiagnosticReporter.java

@Override
public void writeDiagnosticReport(Map<String, Object> diagnosticInfo) throws IOException {
    File diagnosticFile;//from   w  w  w. java  2 s. co m

    File diagnosticDirectory = new File(System.getProperty("java.io.tmpdir"), "seedstack-diagnostics");
    if (!diagnosticDirectory.exists() && !diagnosticDirectory.mkdirs() || !diagnosticDirectory.isDirectory()
            || !diagnosticDirectory.canWrite()) {
        diagnosticDirectory = new File(System.getProperty("java.io.tmpdir"));
    }

    diagnosticFile = new File(diagnosticDirectory,
            String.format("seedstack-diagnostic-%s.json", DATE_FORMAT.format(new Date())));
    writeDiagnosticReport(diagnosticInfo, new FileOutputStream(diagnosticFile));
    LOGGER.warn("Diagnostic information dumped to file://{}", diagnosticFile.toURI().toURL().getPath());
}

From source file:com.zotoh.maedr.device.FilePicker.java

private File testDir(File dir) throws Exception {
    if (dir.exists() && dir.isDirectory() && dir.canRead() && dir.canWrite()) {
    } else {/*from ww w  . j a v a  2  s  . c om*/
        throw new Exception(
                "FilePicker: Folder: " + dir.getCanonicalPath() + " must be a valid directory with RW access");
    }
    return dir;
}

From source file:info.magnolia.module.files.BasicFileExtractorOperation.java

/**
 * @return a java.io.File corresponding to the absoluteTargetPath if the path is validated,
 *         or null if the file should not be extracted. TODO ugly.
 */// w w w .  ja  v a2s . c om
protected File checkOutput() throws IOException {
    final File targetFile = new File(absoluteTargetPath);
    final File parent = targetFile.getParentFile();
    if (!parent.exists() && !parent.mkdirs()) {
        throw new IOException("Can't create directories for " + targetFile.getAbsolutePath());
    } else if (!parent.canWrite()) {
        throw new IOException("Can't write to " + targetFile.getAbsolutePath());
    }
    return targetFile;
}

From source file:com.jaxio.celerio.output.FolderSourceFile.java

private void createFolder(String folder) {
    File dir = new File(folder);
    try {// w  w  w .  j  av a 2 s  .  co m
        if (!dir.exists()) {
            dir.mkdirs();
        }
    } catch (Exception e) {
        // ignore
    }
    if (!dir.isDirectory() || !dir.canWrite()) {
        throw new RuntimeException("Cannot write to: " + folder);
    }
}

From source file:it.geosolutions.opensdi2.configurations.dao.PropertiesDAO.java

private boolean basicsDirectoryChecks(File dir) {
    if (dir == null || !dir.exists() || !dir.isDirectory() || !dir.canRead() || !dir.canWrite()) {
        return false;
    }//w  ww .java 2 s.co m
    return true;
}

From source file:net.idlesoft.android.apps.github.activities.NewsFeed.java

@Override
public boolean onOptionsItemSelected(final MenuItem item) {
    switch (item.getItemId()) {
    case 1://from  w  w  w.j a v  a 2s.co  m
        mEditor.clear().commit();
        final Intent intent = new Intent(NewsFeed.this, Hubroid.class);
        startActivity(intent);
        finish();
        return true;
    case 2:
        final File root = Environment.getExternalStorageDirectory();
        if (root.canWrite()) {
            final File hubroid = new File(root, "hubroid");
            if (!hubroid.exists() && !hubroid.isDirectory()) {
                return true;
            } else {
                hubroid.delete();
                return true;
            }
        }
    }
    return false;
}

From source file:com.flysystem.core.adapter.local.Local.java

public Visibility getVisibility(String path) {
    File file = new File(applyPathPrefix(path));
    if (file.canExecute() && file.canRead() && file.canWrite())
        return Visibility.PUBLIC;
    return Visibility.PRIVATE;
}

From source file:de.halirutan.spectralis.examples.sloexporter.Controller.java

public void exportFiles() {
    File outputDir = showDirectorySelectDialog("Select output directory");
    if (!outputDir.canWrite()) {
        Alert alert = new Alert(AlertType.ERROR, "Cannot write to output directory");
        alert.show();//from w  w w.  ja  v a  2 s .c om
    }

    Task<Integer> exportTask = new ExportWorker(outputDir);
    Service<Integer> service = new Service<Integer>() {
        @Override
        protected Task<Integer> createTask() {
            return exportTask;
        }
    };

    service.start();

}