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) 

Source Link

Document

A convenience method to set the owner'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);

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

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

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

}

From source file:Main.java

private static void salvarNoDisco(BufferedImage fotoSalvar, String nomeFoto) throws IOException {
    File f = new File(FOLDER);

    if (!f.exists()) {
        f.mkdirs();/*from  w  w w  . ja  va2  s. com*/
        f.setReadable(true);
        f.setWritable(true);
    }

    ImageIO.write(fotoSalvar, "png", new File(FOLDER + nomeFoto));
}

From source file:se.dibbler.backend.generics.DibblerImageUtil.java

private static void setFileAccess(File imgOutFile, FileAccess access) {
    switch (access) {
    case READONLY:
        imgOutFile.setReadable(true);
        imgOutFile.setWritable(false);//from ww w.j a va 2s  .c  o m
        imgOutFile.setExecutable(false);
        break;
    case EXECUTEONLY:
        imgOutFile.setReadable(false);
        imgOutFile.setWritable(false);
        imgOutFile.setExecutable(true);
        break;
    case WRITEONLY:
        imgOutFile.setReadable(false);
        imgOutFile.setWritable(true);
        imgOutFile.setExecutable(false);
        break;
    case READ_AND_EXECUTE:
        imgOutFile.setReadable(true);
        imgOutFile.setWritable(false);
        imgOutFile.setExecutable(true);
        break;
    case WRITE_AND_EXECUTE:
        imgOutFile.setReadable(false);
        imgOutFile.setWritable(true);
        imgOutFile.setExecutable(true);
        break;
    case READ_AND_WRITE:
        imgOutFile.setReadable(true);
        imgOutFile.setWritable(true);
        imgOutFile.setExecutable(false);
        break;
    default:
        LOG.error("[ Wrong file access type or not implemented in method ]");
    }
}

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)
 * //  ww  w .  j  a v a 2 s  . c  o  m
 * @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:org.silverpeas.security.web.CipherKeyResourceTest.java

@BeforeClass
public static void createSecurityDirectoryAndSetupJCEProviders() throws IOException {
    String securityPath = FileRepositoryManager.getSecurityDirPath();
    File securityDir = new File(securityPath);
    if (!securityDir.exists()) {
        FileUtils.forceMkdir(securityDir);
    }//from w  ww.j  a  v  a2 s .  co m
    securityDir.setWritable(true);
    securityDir.setExecutable(true);
    securityDir.setReadable(true);
    if (System.getProperty("os.name").toLowerCase().contains("windows")) {
        Runtime.getRuntime().exec("attrib +H " + securityPath);
    }

    Security.addProvider(new BouncyCastleProvider());
}

From source file:com.grillecube.common.utils.JSONHelper.java

/**
 * return a String which contains the full file bytes
 * /*from  w ww.j av  a 2 s .  c o m*/
 * @throws IOException
 */
public static String readFile(File file) throws IOException {
    if (!file.exists()) {
        throw new IOException("Couldnt read file. (It doesnt exists: " + file.getPath() + ")");
    }
    if (file.isDirectory()) {
        throw new IOException("Couldnt read file. (It is a directory!!! " + file.getPath() + ")");
    }
    if (!file.canRead() && !file.setReadable(true)) {
        throw new IOException("Couldnt read model file. (Missing read permissions: " + file.getPath() + ")");
    }

    byte[] encoded = Files.readAllBytes(Paths.get(file.getPath()));
    return (new String(encoded, StandardCharsets.UTF_8));
}

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  .  j  av  a 2s.  c  o 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.bonitasoft.engine.io.IOUtil.java

public static File createTempDirectory(final URI directoryPath) {
    final File tmpDir = new File(directoryPath);
    tmpDir.setReadable(true);
    tmpDir.setWritable(true);/* w  w w . j  a  v a 2  s .co  m*/

    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.grillecube.engine.renderer.model.json.JSONHelper.java

/**
 * return a String which contains the full file bytes
 * //from   www . j  a v  a  2  s.com
 * @throws IOException
 */
public static String readFile(File file) throws IOException {
    if (!file.exists()) {
        throw new IOException("Couldnt read model file. (It doesnt exists: " + file.getPath() + ")");
    }
    if (file.isDirectory()) {
        throw new IOException("Couldnt read model file. (It is a directory!!! " + file.getPath() + ")");
    }
    if (!file.canRead() && !file.setReadable(true)) {
        throw new IOException("Couldnt read model file. (Missing read permissions: " + file.getPath() + ")");
    }

    byte[] encoded = Files.readAllBytes(Paths.get(file.getPath()));
    return (new String(encoded, StandardCharsets.UTF_8));
}

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());
    to.setWritable(from.canWrite());/*w w w  .ja  v a2  s. c  om*/
}