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, boolean ownerOnly) 

Source Link

Document

Sets the owner's or everybody's write permission for this abstract pathname.

Usage

From source file:com.mods.grx.settings.utils.Utils.java

public static void delete_file(String filename) {
    File f = new File(filename);
    if (f.exists()) {
        f.setReadable(true, false);/*  ww  w.  j a va2  s.  c o  m*/
        f.setWritable(true, false);
        f.delete();
    }
}

From source file:com.mods.grx.settings.utils.Utils.java

public static boolean rename_grx_tmp_file(String tmp_name) {

    String n_f = tmp_name.replace("grxtmp", "");
    File f_t = new File(tmp_name);
    if (f_t.exists()) {
        File f_f = new File(n_f);
        f_t.renameTo(f_f);/*  w  w w . j a  va2 s . c  om*/
        f_f.setReadable(true, false);
        f_f.setWritable(true, false);
        return true;
    }
    return false;
}

From source file:com.meltmedia.cadmium.core.FileSystemManager.java

public static void copyAllContent(final String source, final String target, final boolean ignoreHidden)
        throws Exception {
    File sourceFile = new File(source);
    File targetFile = new File(target);
    if (!targetFile.exists()) {
        targetFile.mkdirs();//from  w w w  .j  a  v  a2  s.  com
    }
    if (sourceFile.exists() && sourceFile.canRead() && sourceFile.isDirectory() && targetFile.exists()
            && targetFile.canWrite() && targetFile.isDirectory()) {
        List<File> copyList = new ArrayList<File>();
        FilenameFilter filter = new FilenameFilter() {

            @Override
            public boolean accept(File file, String name) {
                return !ignoreHidden || !name.startsWith(".");
            }

        };
        File files[] = sourceFile.listFiles(filter);
        if (files.length > 0) {
            copyList.addAll(Arrays.asList(files));
        }
        for (int index = 0; index < copyList.size(); index++) {
            File aFile = copyList.get(index);
            String relativePath = aFile.getAbsoluteFile().getAbsolutePath()
                    .replaceFirst(sourceFile.getAbsoluteFile().getAbsolutePath(), "");
            if (aFile.isDirectory()) {
                File newDir = new File(target, relativePath);
                if (newDir.mkdir()) {
                    newDir.setExecutable(aFile.canExecute(), false);
                    newDir.setReadable(aFile.canRead(), false);
                    newDir.setWritable(aFile.canWrite(), false);
                    newDir.setLastModified(aFile.lastModified());
                    files = aFile.listFiles(filter);
                    if (files.length > 0) {
                        copyList.addAll(index + 1, Arrays.asList(files));
                    }
                } else {
                    log.warn("Failed to create new subdirectory \"{}\" in the target path \"{}\".",
                            relativePath, target);
                }
            } else {
                File newFile = new File(target, relativePath);
                if (newFile.createNewFile()) {
                    FileInputStream inStream = null;
                    FileOutputStream outStream = null;
                    try {
                        inStream = new FileInputStream(aFile);
                        outStream = new FileOutputStream(newFile);
                        streamCopy(inStream, outStream);
                    } finally {
                        if (inStream != null) {
                            try {
                                inStream.close();
                            } catch (Exception e) {
                            }
                        }
                        if (outStream != null) {
                            try {
                                outStream.flush();
                            } catch (Exception e) {
                            }
                            try {
                                outStream.close();
                            } catch (Exception e) {
                            }
                        }
                    }
                    newFile.setExecutable(aFile.canExecute(), false);
                    newFile.setReadable(aFile.canRead(), false);
                    newFile.setWritable(aFile.canWrite(), false);
                    newFile.setLastModified(aFile.lastModified());
                }
            }
        }
    }
}

From source file:com.mods.grx.settings.utils.Utils.java

public static void set_read_write_file_permissions(String file_name) {
    if (file_name == null || file_name.isEmpty())
        return;/* www .  j a va 2s  .c  o m*/
    try {
        File file = new File(file_name);
        if (file.exists()) {
            file.setReadable(true, false);
            file.setWritable(true, false);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:ezbake.deployer.impl.Files.java

@SuppressWarnings("ResultOfMethodCallIgnored")
public static void setPosixFilePermissions(File file, Set<PosixFilePermission> filePermissions) {
    if (filePermissions.contains(PosixFilePermission.OWNER_EXECUTE))
        file.setExecutable(true, filePermissions.contains(PosixFilePermission.GROUP_EXECUTE)
                || filePermissions.contains(PosixFilePermission.OTHERS_EXECUTE));
    if (filePermissions.contains(PosixFilePermission.OWNER_READ))
        file.setReadable(true, filePermissions.contains(PosixFilePermission.GROUP_READ)
                || filePermissions.contains(PosixFilePermission.OTHERS_READ));
    if (filePermissions.contains(PosixFilePermission.OWNER_WRITE))
        file.setWritable(true, filePermissions.contains(PosixFilePermission.GROUP_WRITE)
                || filePermissions.contains(PosixFilePermission.OTHERS_WRITE));
}

From source file:com.ghgande.j2mod.modbus.utils.TestUtils.java

/**
 * This method will extract the appropriate Modbus master tool into the
 * temp folder so that it can be used later
 *
 * @return The temporary location of the Modbus master tool.
 * @throws Exception/*from ww w .  j  a va 2s  . com*/
 */
public static File loadModPollTool() throws Exception {

    // Load the resource from the library

    String osName = System.getProperty("os.name");

    // Work out the correct name

    String exeName;
    if (osName.matches("(?is)windows.*")) {
        osName = "win32";
        exeName = "modpoll.exe";
    } else {
        osName = "linux";
        exeName = "modpoll";
    }

    // Copy the native modpoll library to a temporary directory in the build workspace to facilitate
    // execution on some platforms.
    File tmpDir = Files.createTempDirectory(Paths.get("."), "modpoll-").toFile();
    tmpDir.deleteOnExit();

    File nativeFile = new File(tmpDir, exeName);

    // Copy the library to the temporary folder

    InputStream in = null;
    String resourceName = String.format("/com/ghgande/j2mod/modbus/native/%s/%s", osName, exeName);

    try {
        in = SerialPort.class.getResourceAsStream(resourceName);
        if (in == null) {
            throw new Exception(String.format("Cannot find resource [%s]", resourceName));
        }
        pipeInputToOutputStream(in, nativeFile, false);
        nativeFile.deleteOnExit();

        // Set the correct privileges

        if (!nativeFile.setWritable(true, true)) {
            logger.warn("Cannot set modpoll native library to be writable");
        }
        if (!nativeFile.setReadable(true, false)) {
            logger.warn("Cannot set modpoll native library to be readable");
        }
        if (!nativeFile.setExecutable(true, false)) {
            logger.warn("Cannot set modpoll native library to be executable");
        }
    } catch (Exception e) {
        throw new Exception(
                String.format("Cannot locate modpoll native library [%s] - %s", exeName, e.getMessage()));
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                logger.error("Cannot close stream - {}", e.getMessage());
            }
        }
    }

    return nativeFile;
}

From source file:org.everit.osgi.dev.maven.util.FileManager.java

private static void setPermissionsOnFileInNonPosixSystem(final File file,
        final Set<PosixFilePermission> perms) {

    if (perms.contains(PosixFilePermission.OWNER_EXECUTE)) {
        file.setExecutable(true, !perms.contains(PosixFilePermission.OTHERS_EXECUTE));
    }/*ww  w.j  a va 2s.  c om*/

    if (perms.contains(PosixFilePermission.OWNER_READ)) {
        file.setReadable(true, !perms.contains(PosixFilePermission.OTHERS_READ));
    }

    if (perms.contains(PosixFilePermission.OWNER_WRITE)) {
        file.setWritable(true, !perms.contains(PosixFilePermission.OTHERS_WRITE));
    }

}

From source file:net.sf.jasperreports.eclipse.util.FileUtils.java

public static File createTempDir(String prefix) throws IOException {
    final File sysTempDir = new File(System.getProperty("java.io.tmpdir")); //$NON-NLS-1$
    File newTempDir;
    final int maxAttempts = 9;
    int attemptCount = 0;
    do {/*from  w  ww.  j  av  a 2 s .  com*/
        attemptCount++;
        if (attemptCount > maxAttempts)
            throw new IOException(NLS.bind(Messages.FileUtils_ImpossibleToCreateTempDirectory, maxAttempts));
        String dirName = prefix + System.currentTimeMillis();// gUUID.randomUUID().toString();
        newTempDir = new File(sysTempDir, dirName);
    } while (newTempDir.exists());

    if (newTempDir.mkdirs()) {
        newTempDir.deleteOnExit();
        newTempDir.setWritable(true, false);
        newTempDir.setReadable(true, false);
        return newTempDir;
    } else
        throw new IOException(
                NLS.bind(Messages.FileUtils_UnableToCreateDirectory, newTempDir.getAbsolutePath()));
}

From source file:org.mule.test.infrastructure.process.rules.MuleInstallation.java

private void chmodRwx(File destFile) {
    destFile.setExecutable(true, false);
    destFile.setWritable(true, false);
    destFile.setReadable(true, false);
}

From source file:org.eclipse.cdt.internal.p2.touchpoint.natives.actions.UnpackAction.java

private void chmod(File file, int mode) {
    file.setExecutable((mode & 0111) != 0, (mode & 0110) == 0);
    file.setWritable((mode & 0222) != 0, (mode & 0220) == 0);
    file.setReadable((mode & 0444) != 0, (mode & 0440) == 0);
}