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.adaptris.fs.StandardWorkerTest.java

@Test
public void testListFiles() throws Exception {
    File[] files = new File[] { new File("a"), new File("b"), new File("c") };
    File dir = Mockito.mock(File.class);
    Mockito.when(dir.canWrite()).thenReturn(true);
    Mockito.when(dir.exists()).thenReturn(true);
    Mockito.when(dir.canRead()).thenReturn(true);
    Mockito.when(dir.isDirectory()).thenReturn(true);
    Mockito.when(dir.listFiles((FileFilter) Mockito.any())).thenReturn(files);
    StandardWorker worker = createWorker();
    File[] listFiles = worker.listFiles(dir);
    assertEquals(3, listFiles.length);/* w ww. j  a  v a  2s  . co  m*/
    try {
        worker.listFiles(null);
        fail();
    } catch (FsException | IllegalArgumentException expected) {

    }
}

From source file:com.github.fritaly.dualcommander.Utils.java

public static void copyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException {
    if (srcFile == null) {
        throw new NullPointerException("Source must not be null");
    }/*  w  w w .  j  a  v  a2s  .  c o m*/
    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");
    }
    File parentFile = destFile.getParentFile();
    if (parentFile != null) {
        if (!parentFile.mkdirs() && !parentFile.isDirectory()) {
            throw new IOException("Destination '" + parentFile + "' 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:com.edu.duke.FileResource.java

private void initWrite(File f) {
    try {//from ww  w . j  a v a  2  s .  c  o m
        mySaveFile = f;
        if (f.exists() && f.canWrite()) {
            initRead(f);
        } else {
            mySource = "";
            myPath = f.getCanonicalPath();
        }
    } catch (Exception e) {
        throw new ResourceException("FileResource: cannot access " + f, e);
    }
}

From source file:com.adaptris.fs.StandardWorkerTest.java

@Test
public void testPutFile() throws Exception {
    FsWorker worker = createWorker();/*from   www .ja v  a2  s  .c  o m*/
    String[] testFiles = createTestFiles();
    String newFilename = new GuidGenerator().safeUUID();
    worker.put(DATA.getBytes(), new File(baseDir, newFilename));
    byte[] result = worker.get(new File(baseDir, newFilename));
    assertEquals(DATA, new String(result));

    File failingFile = Mockito.mock(File.class);
    Mockito.when(failingFile.canWrite()).thenReturn(true);
    Mockito.when(failingFile.getPath()).thenThrow(new RuntimeException());
    try {
        worker.put(DATA.getBytes(), failingFile);
        fail();
    } catch (FsException e) {
    }
}

From source file:com.alexnederlof.jasperreport.JasperReporter.java

/**
 * Check if the output directory exist and is writable. If not, try to create an output dir and
 * see if that is writable.// w  w  w  .j  a  v  a2  s  .co  m
 *
 * @param outputDirectory
 * @throws MojoExecutionException
 */
private void checkOutDirWritable(File outputDirectory) throws MojoExecutionException {
    if (outputDirectory.exists()) {
        if (outputDirectory.canWrite()) {
            return;
        } else {
            throw new MojoExecutionException("The output dir exists but was not writable. "
                    + "Try running maven with the 'clean' goal.");
        }
    }
    checkIfOutputCanBeCreated();
    checkIfOutputDirIsWritable();
    if (verbose) {
        log.info("Output dir check OK");
    }
}

From source file:de.iew.imageread.Main.java

protected void testAndCreateDirectory(File fileToTest) throws IOException {
    if (fileToTest.exists()) {
        if (!fileToTest.canWrite() || !fileToTest.isDirectory()) {
            throw new IOException("The file " + fileToTest + " exists but is not readable or not a directory");
        }/*from w w w  . j  a  v a  2  s.c  o  m*/
    } else {
        if (!fileToTest.mkdir()) {
            throw new IOException("Cannot create directory " + fileToTest);
        }
    }
}

From source file:asl.plotmaker.PlotMaker2.java

private Boolean checkFileOut(File file) {

    // Check that dir either exists or can be created

    File dir = file.getParentFile();

    Boolean allIsOkay = true;//from   w  w w  .  j  a  va2s .  c om

    if (dir.exists()) { // Dir exists --> check write permissions
        allIsOkay = dir.isDirectory() && dir.canWrite();
    } else { // Dir doesn't exist --> try to make it
        allIsOkay = dir.mkdirs();
    }

    if (!allIsOkay) { // We were unable to make output dir --> return false
        return false;
    }

    // Check that if file already exists it can be overwritten

    if (file.exists()) {
        if (!file.canWrite()) {
            return false;
        }
    }

    return true;

}

From source file:au.org.ala.layers.intersect.IntersectConfig.java

private static void isValidPath(String path, String desc) {
    File f = new File(path);

    if (!f.exists()) {
        logger.error("Config error. Property \"" + desc + "\" with value \"" + path
                + "\"  is not a valid local file path.  It does not exist.");
    } else if (!f.isDirectory()) {
        logger.error("Config error. Property \"" + desc + "\" with value \"" + path
                + "\"  is not a valid local file path.  It is not a directory.");
    } else if (!f.canRead()) {
        logger.error("Config error. Property \"" + desc + "\" with value \"" + path
                + "\"  is not a valid local file path.  Not permitted to READ.");
    } else if (!f.canWrite()) {
        logger.error("Config error. Property \"" + desc + "\" with value \"" + path
                + "\"  is not a valid local file path.  Not permitted to WRITE.");
    }/*from w w  w  .  j  a  v  a 2s  . c o  m*/

}

From source file:mendeley2kindle.KindleDAO.java

public void commit() throws IOException {
    File path = new File(kindleLocal);
    File file = new File(path, KINDLE_COLLECTIONS_JSON);
    log.log(Level.FINER, "writing collections data: " + file);
    if (!file.exists() || file.canWrite()) {
        file.getParentFile().mkdirs();//from w  w w  .j a v  a  2s.c o  m
        FileWriter fw = new FileWriter(file);
        fw.write(collections.toString());
        fw.close();
        log.log(Level.FINE, "Saved kindle collections: " + file);
    } else {
        log.log(Level.SEVERE, "CANNOT write Kindle collections data. Aborting..." + file);
    }
}

From source file:FileTableDemo.java

public Object getValueAt(int row, int col) {
    File f = new File(dir, filenames[row]);
    switch (col) {
    case 0://www . j  av  a  2 s  .c o m
        return filenames[row];
    case 1:
        return new Long(f.length());
    case 2:
        return new Date(f.lastModified());
    case 3:
        return f.isDirectory() ? Boolean.TRUE : Boolean.FALSE;
    case 4:
        return f.canRead() ? Boolean.TRUE : Boolean.FALSE;
    case 5:
        return f.canWrite() ? Boolean.TRUE : Boolean.FALSE;
    default:
        return null;
    }
}