Example usage for java.io File setReadable

List of usage examples for java.io File setReadable

Introduction

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

Prototype

public boolean setReadable(boolean readable, boolean ownerOnly) 

Source Link

Document

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

Usage

From source file:Main.java

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

    // set read permission
    boolean bool = f.setReadable(true, true);

    System.out.println("setReadable() succeeded?: " + bool);

    // checks whether the file is readable
    bool = f.canRead();/*ww w  . ja v a 2 s  . c  om*/

    System.out.print("Is file readable?: " + bool);

}

From source file:Main.java

public static Uri getUriFromPath(String FilePath) {
    File f = new File(FilePath);
    f.setReadable(true, false);
    Uri returnUri = Uri.fromFile(f);/* w  w  w .ja va  2  s.c o  m*/
    return returnUri;
}

From source file:Main.java

public static boolean bitmapToFile(Bitmap bmp, String fileName) {
    try {//from   w w  w  .  ja v  a  2s.  c  o m
        FileOutputStream out = new FileOutputStream(fileName);
        bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
        out.close();
        File iconFile = new File(fileName);
        iconFile.setReadable(true, false);
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

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

/**
 * Set file permissions to 600 (unix)/*w  w w.  j  ava2 s.c o m*/
 */
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:io.digibyte.tools.qrcode.QRUtils.java

private static File saveToExternalStorage(Bitmap bitmapImage, Activity app) {
    if (app == null) {
        Log.e(TAG, "saveToExternalStorage: app is null");
        return null;
    }/*  ww w .  jav  a  2 s. c  o  m*/

    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    String fileName = "qrcode.jpg";

    bitmapImage.compress(Bitmap.CompressFormat.PNG, 0, bytes);
    File f = new File(app.getCacheDir(), fileName);
    f.setReadable(true, false);
    try {
        boolean a = f.createNewFile();
        if (!a)
            Log.e(TAG, "saveToExternalStorage: createNewFile: failed");
    } catch (IOException e) {
        e.printStackTrace();
    }
    Log.e(TAG, "saveToExternalStorage: " + f.getAbsolutePath());
    if (f.exists())
        f.delete();

    try {
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            bytes.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return f;
}

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

public static void setFilePermissionsTo700(File file) throws IOException {
    file.createNewFile();//from   ww w.ja  v 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: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.
 *//*from w w w  . j a v a2s  .  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 {/*  www. ja v a2s .co 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();// w  ww.  j a va2 s.  co m
    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: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   w ww  . ja  va  2 s.  c  o  m
                    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!");
    }
}