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:org.openbase.bco.ontology.lib.testing.Measurement.java

private void putIntoExcelFile(final String sheetName, final List<Long> simpleQuMeasuredValues,
        final List<Long> complexQuMeasuredValues, int daysCurCount) {
    // https://www.mkyong.com/java/apache-poi-reading-and-writing-excel-file-in-java/
    XSSFWorkbook workbook;/*from w w w  .j  a v  a 2 s. c o  m*/
    XSSFSheet sheet;
    Row row;

    try {
        FileInputStream excelFile = new FileInputStream(new File(FILE_NAME));
        workbook = new XSSFWorkbook(excelFile);
        sheet = workbook.getSheet(sheetName);
    } catch (IOException ex) {
        workbook = new XSSFWorkbook();
        sheet = workbook.createSheet(sheetName);
        row = sheet.createRow(daysCurCount);

        row.createCell(0).setCellValue("Days");
        row.createCell(1).setCellValue("Triple");
        row.createCell(2).setCellValue("Mean of simple trigger");
        row.createCell(3).setCellValue("Mean of complex trigger");
    }

    row = sheet.createRow(daysCurCount + 1);

    System.out.println("simple: " + simpleQuMeasuredValues);
    System.out.println("simple count: " + simpleQuMeasuredValues.size());
    System.out.println("complex: " + complexQuMeasuredValues);
    System.out.println("complex count: " + complexQuMeasuredValues.size());

    long sumSimple = 0L;
    long sumComplex = 0L;

    for (final long valueSimple : simpleQuMeasuredValues) {
        sumSimple += valueSimple;
    }
    for (final long valueComplex : complexQuMeasuredValues) {
        sumComplex += valueComplex;
    }

    // number of days
    final Cell cellDay = row.createCell(0);
    cellDay.setCellValue(daysCurCount + 1);

    // number of triple
    final Cell cellTriple = row.createCell(1);
    cellTriple.setCellValue(numberOfTriple);

    // mean of simple trigger time
    final Cell cellMeanSimple = row.createCell(2);
    cellMeanSimple.setCellValue(sumSimple / simpleQuMeasuredValues.size());

    // mean of complex trigger time
    final Cell cellMeanComplex = row.createCell(3);
    cellMeanComplex.setCellValue(sumComplex / complexQuMeasuredValues.size());

    try {
        final File file = new File(FILE_NAME);
        file.setReadable(true, false);
        file.setExecutable(true, false);
        file.setWritable(true, false);
        System.out.println("Save data row ...");
        final FileOutputStream outputStream = new FileOutputStream(file);
        workbook.write(outputStream);
        workbook.close();
    } catch (IOException ex) {
        ExceptionPrinter.printHistory(ex, LOGGER);
    }
}

From source file:com.squareup.leakcanary.internal.DisplayLeakActivity.java

@SuppressLint("SetWorldReadable")
void shareHeapDump() {
    AnalyzedHeap visibleLeak = getVisibleLeak();
    final File heapDumpFile = visibleLeak.heapDump.heapDumpFile;
    AsyncTask.SERIAL_EXECUTOR.execute(new Runnable() {
        @Override/*w w w  . j a va 2 s  .co m*/
        public void run() {
            //noinspection ResultOfMethodCallIgnored
            heapDumpFile.setReadable(true, false);
            final Uri heapDumpUri = getUriForFile(getBaseContext(),
                    "com.squareup.leakcanary.fileprovider." + getApplication().getPackageName(), heapDumpFile);
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    startShareIntentChooser(heapDumpUri);
                }
            });
        }
    });
}

From source file:org.openbase.bco.ontology.lib.testing.Measurement.java

private void saveMemoryTestValues(final String sheetName, final List<Long> simpleQuMeasuredValues,
        final List<Long> complexQuMeasuredValues, final DataVolume dataVolume) {
    XSSFWorkbook workbook;/*w  w  w .  j a v  a  2s  . c o  m*/
    XSSFSheet sheet;
    Row rowSimple;
    Row rowComplex;

    try {
        FileInputStream excelFile = new FileInputStream(new File(FILE_NAME));
        workbook = new XSSFWorkbook(excelFile);
        sheet = workbook.getSheet(sheetName);
        rowSimple = sheet.getRow(1);
        rowComplex = sheet.getRow(2);
    } catch (IOException ex) {
        workbook = new XSSFWorkbook();
        sheet = workbook.createSheet(sheetName);
        final Row row = sheet.createRow(0);
        rowSimple = sheet.createRow(1);
        rowComplex = sheet.createRow(2);

        row.createCell(1).setCellValue("ConfigData only");
        row.createCell(2).setCellValue("ConfigData and dayData");
        row.createCell(3).setCellValue("ConfigData and 4x dayData");
    }

    long sumSimple = 0L;
    long sumComplex = 0L;

    for (final long valueSimple : simpleQuMeasuredValues) {
        sumSimple += valueSimple;
    }
    for (final long valueComplex : complexQuMeasuredValues) {
        sumComplex += valueComplex;
    }

    int column = 0;

    switch (dataVolume) {
    case CONFIG:
        column = 1;
        break;
    case CONFIG_DAY:
        column = 2;
        break;
    case CONFIG_WEEK:
        column = 3;
        break;
    default:
        break;
    }

    System.out.println("Save date in column: " + column);

    // mean of simple trigger time
    final Cell cellMeanSimple = rowSimple.createCell(column);
    cellMeanSimple.setCellValue(sumSimple / simpleQuMeasuredValues.size());

    // mean of complex trigger time
    final Cell cellMeanComplex = rowComplex.createCell(column);
    cellMeanComplex.setCellValue(sumComplex / complexQuMeasuredValues.size());

    try {
        final File file = new File(FILE_NAME);
        file.setReadable(true, false);
        file.setExecutable(true, false);
        file.setWritable(true, false);
        System.out.println("Save data row ...");
        final FileOutputStream outputStream = new FileOutputStream(file);
        workbook.write(outputStream);
        workbook.close();
    } catch (IOException ex) {
        ExceptionPrinter.printHistory(ex, LOGGER);
    }
}

From source file:org.apache.solr.core.TestCoreDiscovery.java

@Test
public void testSolrHomeNotReadable() throws Exception {
    File homeDir = solrHomeDirectory;
    setMeUp(homeDir.getAbsolutePath());//from   ww w.jav  a  2 s . c  o  m
    addCoreWithProps(makeCorePropFile("core1", false, true),
            new File(homeDir, "core1" + File.separator + CorePropertiesLocator.PROPERTIES_FILENAME));

    assumeTrue("Cannot make " + homeDir + " non-readable. Test aborted.", homeDir.setReadable(false, false));

    CoreContainer cc = null;
    try {
        cc = init();
        fail("Should have thrown an exception here");
    } catch (Exception ex) {
        assertThat(ex.getMessage(), containsString("Error reading core root directory"));
    } finally {
        if (cc != null) {
            cc.shutdown();
        }
    }
    // So things can be cleaned up by the framework!
    homeDir.setReadable(true, false);

}

From source file:com.buaa.cfs.utils.FileUtil.java

/**
 * Set permissions to the required value. Uses the java primitives instead of forking if group == other.
 *
 * @param f          the file to change/*  w  w  w. ja va2  s .co  m*/
 * @param permission the new permissions
 *
 * @throws IOException
 */
public static void setPermission(File f, FsPermission permission) throws IOException {
    FsAction user = permission.getUserAction();
    FsAction group = permission.getGroupAction();
    FsAction other = permission.getOtherAction();

    // use the native/fork if the group/other permissions are different
    // or if the native is available or on Windows
    if (group != other || NativeIO.isAvailable() || Shell.WINDOWS) {
        execSetPermission(f, permission);
        return;
    }

    boolean rv = true;

    // read perms
    rv = f.setReadable(group.implies(FsAction.READ), false);
    checkReturnValue(rv, f, permission);
    if (group.implies(FsAction.READ) != user.implies(FsAction.READ)) {
        rv = f.setReadable(user.implies(FsAction.READ), true);
        checkReturnValue(rv, f, permission);
    }

    // write perms
    rv = f.setWritable(group.implies(FsAction.WRITE), false);
    checkReturnValue(rv, f, permission);
    if (group.implies(FsAction.WRITE) != user.implies(FsAction.WRITE)) {
        rv = f.setWritable(user.implies(FsAction.WRITE), true);
        checkReturnValue(rv, f, permission);
    }

    // exec perms
    rv = f.setExecutable(group.implies(FsAction.EXECUTE), false);
    checkReturnValue(rv, f, permission);
    if (group.implies(FsAction.EXECUTE) != user.implies(FsAction.EXECUTE)) {
        rv = f.setExecutable(user.implies(FsAction.EXECUTE), true);
        checkReturnValue(rv, f, permission);
    }
}

From source file:com.oneops.inductor.AbstractOrderExecutor.java

/**
 * writes private key/*from   ww  w.ja  v  a2  s  .  c o  m*/
 *
 * @param key String
 */
protected String writePrivateKey(String key) {
    String uuid = UUID.randomUUID().toString();
    String fileName = config.getDataDir() + "/" + uuid;
    try (BufferedWriter bw = Files.newBufferedWriter(Paths.get(fileName))) {
        bw.write(key);
        bw.close();
        File f = new File(fileName);
        f.setExecutable(false, false);
        f.setReadable(false, false);
        f.setWritable(false, false);
        f.setReadable(true, true);
        f.setWritable(true, true);
        logger.debug("file: " + fileName);
    } catch (IOException e) {
        logger.error("could not write file: " + fileName + " msg:" + e.getMessage());
    }
    return fileName;
}

From source file:com.oneops.inductor.AbstractOrderExecutor.java

/**
 * Creates a chef config file for unique lockfile by ci.
 * returns chef config full path.//from   w ww  .j  a v a2s .  c o  m
 *
 * @param ci
 * @param cookbookRelativePath chef config full path
 * @param logLevel
 * @return
 */
protected String writeChefConfig(String ci, String cookbookRelativePath, String logLevel) {
    String filename = "/tmp/chef-" + ci;

    String cookbookDir = config.getCircuitDir();
    if (!cookbookRelativePath.equals(""))
        cookbookDir = config.getCircuitDir().replace("packer", cookbookRelativePath);

    cookbookDir += "/components/cookbooks";
    String sharedDir = config.getCircuitDir().replace("packer", "shared/cookbooks");

    String content = "cookbook_path [\"" + cookbookDir + "\",\"" + sharedDir + "\"]\n";
    content += "lockfile \"" + filename + ".lock\"\n";
    content += "file_cache_path \"/tmp\"\n";
    content += "log_level :" + logLevel + "\n";
    content += "verify_api_cert true\n";
    FileWriter fstream;
    try {
        fstream = new FileWriter(filename);
        BufferedWriter bw = new BufferedWriter(fstream);
        bw.write(content);
        bw.close();

        File f = new File(filename);
        f.setExecutable(false, false);
        f.setReadable(false, false);
        f.setWritable(false, false);
        f.setReadable(true, true);
        f.setWritable(true, true);
        logger.debug("file: " + filename);

    } catch (IOException e) {
        logger.error("could not write file: " + filename + " msg:" + e.getMessage());
    }
    return filename;
}

From source file:com.sdk.download.providers.downloads.DownloadThread.java

/**
 * Called after a successful completion to take any necessary action on the
 * downloaded file./*from  w w  w.j  a  v  a2s . c  om*/
 */
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
private void finalizeDestinationFile(State state) throws StopRequest {
    // make sure the file is readable
    // FileUtils.setPermissions(state.mFilename, 0644, -1, -1);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
        File f = new File(state.mFilename);
        if (null != f && f.exists()) {
            f.setReadable(true, false);
        }
    }
    syncDestination(state);
}

From source file:com.comcast.cdn.traffic_control.traffic_router.core.loc.AbstractServiceUpdater.java

protected boolean copyDatabaseIfDifferent(final File existingDB, final File newDB) throws IOException {
    if (filesEqual(existingDB, newDB)) {
        LOGGER.info("[" + getClass().getSimpleName() + "] database unchanged.");
        return false;
    }//  w  ww.j  a v a  2  s.  c  o  m

    if (existingDB.isDirectory() && newDB.isDirectory()) {
        moveDirectory(existingDB, newDB);
        LOGGER.info("[" + getClass().getSimpleName() + "] Successfully updated database " + existingDB);
        return true;
    }

    if (existingDB != null && existingDB.exists()) {
        deleteDatabase(existingDB);
    }

    newDB.setReadable(true, true);
    newDB.setWritable(true, false);
    final boolean renamed = newDB.renameTo(existingDB);

    if (!renamed) {
        LOGGER.fatal("[" + getClass().getSimpleName() + "] Unable to rename " + newDB + " to "
                + existingDB.getAbsolutePath() + "; current working directory is "
                + System.getProperty("user.dir"));
        return false;
    }

    LOGGER.info("[" + getClass().getSimpleName() + "] Successfully updated database " + existingDB);
    return true;
}