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:Main.java

public static void main(String[] args) {
    File f = new File("C:/test.txt");

    boolean bool = f.setWritable(true, false);

    System.out.println(bool);/*w  w  w.ja v  a 2  s  .  co m*/

    bool = f.canWrite();

    System.out.print(bool);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    File f = new File("name.txt");

    if (!f.exists()) {
        System.out.println("File not found.");
        return;// w w  w  .  j a va  2 s.c  om
    }
    if (f.canRead())
        System.out.println("  Readable");
    else
        System.out.println("  Not Readable");

    if (f.canWrite())
        System.out.println("  Writable");
    else
        System.out.println("  Not Writable");
    System.out.println("  Last modified on " + new Date(f.lastModified()));

    long t = Calendar.getInstance().getTimeInMillis();
    if (!f.setLastModified(t))
        System.out.println("Can't set time.");

    if (!f.setReadOnly())
        System.out.println("Can't set to read-only.");

    if (f.canRead())
        System.out.println("  Readable");
    else
        System.out.println("  Not Readable");

    if (f.canWrite())
        System.out.println("  Writable");
    else
        System.out.println("  Not Writable");
    System.out.println("  Last modified on " + new Date(f.lastModified()));

    if (!f.setWritable(true, false))
        System.out.println("Can't return to read/write.");

    if (f.canRead())
        System.out.println("  Readable");
    else
        System.out.println("  Not Readable");

    if (f.canWrite())
        System.out.println("  Writable");
    else
        System.out.println("  Not Writable");
}

From source file:com.cnten.platform.util.FileUtil.java

public static void createDir(String dirPath) {
    if (StringUtils.isEmpty(dirPath)) {
        return;/*from w w w  .  j  a  v a  2  s .c o  m*/
    }
    File file = new File(dirPath);
    if (!file.exists()) {
        file.setWritable(true, false);//linux java ???
        file.mkdirs();
    }
}

From source file:org.apache.whirr.util.KeyPair.java

/**
 * Set file permissions to 600 (unix)/*from w  w w .ja v a  2 s . c om*/
 */
public static void setPermissionsTo600(File f) {
    f.setReadable(false, false);
    f.setReadable(true, true);

    f.setWritable(false, false);
    f.setWritable(true, true);

    f.setExecutable(false);
}

From source file:org.jncc.base.file.Fileupload.java

public static File file_Upload(String contextPath, File fileupload, String origFilename) {
    String path = ServletActionContext.getServletContext().getRealPath(contextPath);
    File file = new File(path); // ?,?
    if (!file.exists()) {
        file.mkdir();// w w w .j a v  a  2 s  .  com
    }
    try {
        if (fileupload != null) {
            // String fileName = java.util.UUID.randomUUID().toString(); //
            // +UUID?????
            java.text.DateFormat formatStr = new java.text.SimpleDateFormat("yyMMddhhmmss");
            String fileName = formatStr.format(new Date());
            fileName = fileName + origFilename;
            File saveFile = new File(path, fileName);

            // upload.renameTo(saveFile);
            if (saveFile.exists()) {
                saveFile.setWritable(true, false);
                saveFile.delete();
            }
            FileUtils.moveFile(fileupload, saveFile);
            return saveFile;
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return null;
}

From source file:com.google.jenkins.plugins.credentials.oauth.KeyUtils.java

/**
 * Sets the permissions of the file to owner-only read/write.
 *
 * @throws IOException if filesystem interaction fails.
 *//*  w w  w.j  av a 2 s  . co  m*/
public static void updatePermissions(File file) throws IOException {
    if (file == null || !file.exists()) {
        return;
    }
    // Set world read/write permissions to false.
    // Set owner read/write permissions to true.
    if (!file.setReadable(false, false) || !file.setWritable(false, false) || !file.setReadable(true, true)
            || !file.setWritable(true, true)) {
        throw new IOException("Failed to update key file permissions");
    }
}

From source file:org.ow2.proactive_grid_cloud_portal.cli.utils.FileUtility.java

public static void writeStringToFile(File file, String content) {
    try {//from w  w  w.  j  a  va 2 s . c o m
        FileUtils.writeStringToFile(file, content);
    } catch (IOException ioe) {
        throw new CLIException(REASON_OTHER, ioe);
    }
    file.setReadable(true, true);
    file.setWritable(true, true);
}

From source file:brooklyn.util.io.FileUtil.java

public static void setFilePermissionsTo600(File file) throws IOException {
    file.createNewFile();/*  ww w . ja va 2  s.  c om*/
    file.setExecutable(false, false);
    file.setReadable(false, false);
    file.setWritable(false, false);
    file.setReadable(true, true);
    file.setWritable(true, true);

    boolean setRead = file.setReadable(false, false) & file.setReadable(true, true);
    boolean setWrite = file.setWritable(false, false) & file.setWritable(true, true);
    boolean setExec = file.setExecutable(false, false);

    if (setRead && setWrite && setExec) {
        if (LOG.isTraceEnabled())
            LOG.trace("Set permissions to 600 for file {}", file.getAbsolutePath());
    } else {
        if (loggedSetFilePermissionsWarning) {
            if (LOG.isTraceEnabled())
                LOG.trace(
                        "Failed to set permissions to 600 for file {}: setRead={}, setWrite={}, setExecutable={}",
                        new Object[] { file.getAbsolutePath(), setRead, setWrite, setExec });
        } else {
            if (Os.isMicrosoftWindows()) {
                if (LOG.isDebugEnabled())
                    LOG.debug(
                            "Failed to set permissions to 600 for file {}; expected behaviour on Windows; setRead={}, setWrite={}, setExecutable={}; subsequent failures (on any file) will be logged at trace",
                            new Object[] { file.getAbsolutePath(), setRead, setWrite, setExec });
            } else {
                LOG.warn(
                        "Failed to set permissions to 600 for file {}: setRead={}, setWrite={}, setExecutable={}; subsequent failures (on any file) will be logged at trace",
                        new Object[] { file.getAbsolutePath(), setRead, setWrite, setExec });
            }
            loggedSetFilePermissionsWarning = true;
        }
    }
}

From source file:brooklyn.util.io.FileUtil.java

public static void setFilePermissionsTo700(File file) throws IOException {
    file.createNewFile();//from   w w  w  .  j  av  a2 s  .  c  o m
    boolean setRead = file.setReadable(false, false) & file.setReadable(true, true);
    boolean setWrite = file.setWritable(false, false) & file.setWritable(true, true);
    boolean setExec = file.setExecutable(false, false) & file.setExecutable(true, true);

    if (setRead && setWrite && setExec) {
        if (LOG.isTraceEnabled())
            LOG.trace("Set permissions to 700 for file {}", file.getAbsolutePath());
    } else {
        if (loggedSetFilePermissionsWarning) {
            if (LOG.isTraceEnabled())
                LOG.trace(
                        "Failed to set permissions to 700 for file {}: setRead={}, setWrite={}, setExecutable={}",
                        new Object[] { file.getAbsolutePath(), setRead, setWrite, setExec });
        } else {
            if (Os.isMicrosoftWindows()) {
                if (LOG.isDebugEnabled())
                    LOG.debug(
                            "Failed to set permissions to 700 for file {}; expected behaviour on Windows; setRead={}, setWrite={}, setExecutable={}; subsequent failures (on any file) will be logged at trace",
                            new Object[] { file.getAbsolutePath(), setRead, setWrite, setExec });
            } else {
                LOG.warn(
                        "Failed to set permissions to 700 for file {}: setRead={}, setWrite={}, setExecutable={}; subsequent failures (on any file) will be logged at trace",
                        new Object[] { file.getAbsolutePath(), setRead, setWrite, setExec });
            }
            loggedSetFilePermissionsWarning = true;
        }
    }
}

From source file:net.amigocraft.mpt.command.InstallCommand.java

@SuppressWarnings("unchecked")
public static void downloadPackage(String id) throws MPTException {
    JSONObject packages = (JSONObject) Main.packageStore.get("packages");
    if (packages != null) {
        JSONObject pack = (JSONObject) packages.get(id);
        if (pack != null) {
            if (pack.containsKey("name") && pack.containsKey("version") && pack.containsKey("url")) {
                if (pack.containsKey("sha1") || !Config.ENFORCE_CHECKSUM) {
                    String name = pack.get("name").toString();
                    String version = pack.get("version").toString();
                    String fullName = name + " v" + version;
                    String url = pack.get("url").toString();
                    String sha1 = pack.containsKey("sha1") ? pack.get("sha1").toString() : "";
                    if (pack.containsKey("installed")) { //TODO: compare versions
                        throw new MPTException(ID_COLOR + name + ERROR_COLOR + " is already installed");
                    }//from ww w. j av  a 2s  . com
                    try {
                        URLConnection conn = new URL(url).openConnection();
                        conn.connect();
                        ReadableByteChannel rbc = Channels.newChannel(conn.getInputStream());
                        File file = new File(Main.plugin.getDataFolder(),
                                "cache" + File.separator + id + ".zip");
                        file.setReadable(true, false);
                        file.setWritable(true, false);
                        file.getParentFile().mkdirs();
                        file.createNewFile();
                        FileOutputStream os = new FileOutputStream(file);
                        os.getChannel().transferFrom(rbc, 0, MiscUtil.getFileSize(new URL(url)));
                        os.close();
                        if (!sha1.isEmpty() && !sha1(file.getAbsolutePath()).equals(sha1)) {
                            file.delete();
                            throw new MPTException(ERROR_COLOR + "Failed to install package " + ID_COLOR
                                    + fullName + ERROR_COLOR + ": checksum mismatch!");
                        }
                    } catch (IOException ex) {
                        throw new MPTException(
                                ERROR_COLOR + "Failed to download package " + ID_COLOR + fullName);
                    }
                } else
                    throw new MPTException(ERROR_COLOR + "Package " + ID_COLOR + id + ERROR_COLOR
                            + " is missing SHA-1 checksum! Aborting...");
            } else
                throw new MPTException(ERROR_COLOR + "Package " + ID_COLOR + id + ERROR_COLOR
                        + " is missing required elements!");
        } else
            throw new MPTException(ERROR_COLOR + "Cannot find package with id " + ID_COLOR + id);
    } else {
        throw new MPTException(ERROR_COLOR + "Package store is malformed!");
    }
}