Example usage for java.io File setWritable

List of usage examples for java.io File setWritable

Introduction

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

Prototype

public boolean setWritable(boolean writable) 

Source Link

Document

A convenience method to set the owner's write permission for this abstract pathname.

Usage

From source file:org.jboss.tools.openshift.common.core.utils.FileUtils.java

/**
 * Replicates the owner permissions from the source to the destination. Due
 * to limitation in java6 this is the best we can do (there's no way in
 * java6 to know if rights are due to owner or group)
 * //w  w  w  . j  a v  a  2 s  .com
 * @param source
 * @param destination
 * 
 * @see File#canRead()
 * @see File#setReadable(boolean)
 * @see File#canWrite()
 * @see File#setWritable(boolean)
 * @see File#canExecute()
 * @see File#setExecutable(boolean)
 */
private static void copyPermissions(File source, File destination) {
    Assert.isLegal(source != null);
    Assert.isLegal(destination != null);

    destination.setReadable(source.canRead());
    destination.setWritable(source.canWrite());
    destination.setExecutable(source.canExecute());
}

From source file:com.isa.utiles.Utiles.java

public static void downloadFile(String linkDescarga, String rutaDestino)
        throws MalformedURLException, IOException {

    URL urlFile = new URL(linkDescarga);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    InputStream in = new BufferedInputStream(urlFile.openStream());

    byte[] buf = new byte[1024];
    int n = 0;/*from w w w  .  ja va  2 s  .co  m*/
    while (-1 != (n = in.read(buf))) {
        out.write(buf, 0, n);
    }
    out.close();
    in.close();

    /*
    byte[] response = out.toByteArray();
    FileOutputStream fos = new FileOutputStream(rutaDestino);
    fos.write(response);
    fos.close();
    */

    File file = new File(rutaDestino);
    file.setWritable(true);
    file.setReadable(true);

    BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
    bw.write(out.toString());
    bw.close();
}

From source file:org.silverpeas.core.util.file.FileUtil.java

/**
 * Copies the specified source file to the specified destination. If the destination exists, it is
 * then replaced by the source. If the destination can be overwritten, its write property is set
 * before the copy./*from   w ww  .  j a va 2 s  .c o m*/
 *
 * @param source the file to copy.
 * @param destination the destination file of the move.
 * @throws IOException if the source or the destination is invalid or if an error occurs while
 * copying the file.
 */
public static void copyFile(File source, File destination) throws IOException {
    if (destination.exists() && !destination.canWrite()) {
        destination.setWritable(true);
    }
    Files.copy(source.toPath(), destination.toPath(), REPLACE_EXISTING);
}

From source file:org.silverpeas.core.util.file.FileUtil.java

/**
 * Forces the deletion of the specified file. If the write property of the file to delete isn't
 * set, this property is then set before deleting.
 *
 * @param fileToDelete file to delete./*w w w.  ja v  a2  s  .  c o m*/
 * @throws IOException if the deletion failed or if the file doesn't exist.
 */
public static void forceDeletion(File fileToDelete) throws IOException {
    if (fileToDelete.exists() && !fileToDelete.canWrite()) {
        fileToDelete.setWritable(true);
    }
    try (Stream<Path> paths = Files.walk(fileToDelete.toPath())) {
        paths.sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
    }
}

From source file:utils.GlobalParameters.java

public static void deleteFile(File fileToDelete) {
    fileToDelete.setWritable(true);
    try {// w  w w . j a v  a  2  s . com
        fileToDelete = fileToDelete.getCanonicalFile();
    } catch (IOException e) {
        log.error("IP-Exception on CanonicalFile: " + fileToDelete.getAbsolutePath());
    }

    if (!fileToDelete.canWrite()) {
        log.trace("Set writable flag for file to delete");
        fileToDelete.setWritable(true);
    }

    boolean deleteFile = fileToDelete.delete();
    if (deleteFile) {
        log.info("Delete file: " + fileToDelete.getAbsolutePath() + " sucessfully.");
    } else {
        log.error("Cannot delete file: " + fileToDelete.getAbsolutePath());
        log.info("Try delete on exit.");
        fileToDelete.deleteOnExit();
    }
}

From source file:org.bonitasoft.engine.io.IOUtil.java

public static File createTempDirectory(final URI directoryPath) {
    final File tmpDir = new File(directoryPath);
    tmpDir.setReadable(true);/*from  w w  w.j  ava2  s. c o m*/
    tmpDir.setWritable(true);

    mkdirs(tmpDir);

    try {
        // to initialize internal class FilenameUtils. Otherwise it cannot load the class as the shutdown is in progress:
        FileUtils.isSymlink(tmpDir);
    } catch (IOException ignored) {
    }

    try {

        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            try {
                final boolean deleted = deleteDir(tmpDir);
                if (!deleted) {
                    System.err.println("Unable to delete directory: " + tmpDir
                            + ". Trying with an alternative force delete.");
                    FileUtils.forceDelete(tmpDir);
                }
            } catch (final IOException e) {
                throw new BonitaRuntimeException(e);
            }
        }));
    } catch (IllegalStateException ignored) {
        // happen in case of hook already registered and when shutting down
    }
    return tmpDir;
}

From source file:com.silverpeas.util.FileUtil.java

/**
 * Copies the specified source file to the specified destination. If the destination exists, it is
 * then replaced by the source. If the destination can be overwritten, its write property is set
 * before the copy.//from   w ww.j  av a  2s .  c  o m
 *
 * @param source the file to copy.
 * @param destination the destination file of the move.
 * @throws IOException if the source or the destination is invalid or if an error occurs while
 * copying the file.
 */
public static void copyFile(File source, File destination) throws IOException {
    if (destination.exists() && !destination.canWrite()) {
        destination.setWritable(true);
    }
    FileUtils.copyFile(source, destination);
}

From source file:com.silverpeas.util.FileUtil.java

/**
 * Forces the deletion of the specified file. If the write property of the file to delete isn't
 * set, this property is then set before deleting.
 *
 * @param fileToDelete file to delete./*  ww w.j  a  v a2  s  .  c  o m*/
 * @throws IOException if the deletion failed or if the file doesn't exist.
 */
public static void forceDeletion(File fileToDelete) throws IOException {
    if (fileToDelete.exists() && !fileToDelete.canWrite()) {
        fileToDelete.setWritable(true);
    }
    FileUtils.forceDelete(fileToDelete);
}

From source file:org.apache.slider.server.services.security.SecurityUtils.java

private static String getKeystorePassword(File secDirFile, boolean persistPassword) {
    File passFile = new File(secDirFile, SliderKeys.CRT_PASS_FILE_NAME);
    String password = null;/*from   w w w .  j a va2  s.c  om*/
    if (!passFile.exists()) {
        LOG.info("Generating keystore password");
        password = RandomStringUtils.randomAlphanumeric(Integer.valueOf(SliderKeys.PASS_LEN));
        if (persistPassword) {
            try {
                FileUtils.writeStringToFile(passFile, password);
                passFile.setWritable(true);
                passFile.setReadable(true);
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("Error creating certificate password file");
            }
        }
    } else {
        LOG.info("Reading password from existing file");
        try {
            password = FileUtils.readFileToString(passFile);
            password = password.replaceAll("\\p{Cntrl}", "");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return password;
}

From source file:com.igormaznitsa.jcp.utils.PreprocessorUtils.java

public static void copyFileAttributes(@Nonnull final File from, @Nonnull final File to) {
    to.setExecutable(from.canExecute());
    to.setReadable(from.canRead());/* www.  j ava  2s . com*/
    to.setWritable(from.canWrite());
}