Example usage for java.io File canWrite

List of usage examples for java.io File canWrite

Introduction

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

Prototype

public boolean canWrite() 

Source Link

Document

Tests whether the application can modify the file denoted by this abstract pathname.

Usage

From source file:net.idlesoft.android.apps.github.activities.BranchesList.java

@Override
public boolean onOptionsItemSelected(final MenuItem item) {
    switch (item.getItemId()) {
    case 0:/*from w  w w.j  a v a 2  s  .  co m*/
        final Intent i1 = new Intent(this, Hubroid.class);
        startActivity(i1);
        return true;
    case 1:
        mEditor.clear().commit();
        final Intent intent = new Intent(this, Hubroid.class);
        startActivity(intent);
        return true;
    case 2:
        final File root = Environment.getExternalStorageDirectory();
        if (root.canWrite()) {
            final File hubroid = new File(root, "hubroid");
            if (!hubroid.exists() && !hubroid.isDirectory()) {
                return true;
            } else {
                hubroid.delete();
                return true;
            }
        }
    }
    return false;
}

From source file:net.sf.jabref.external.push.PushToLyx.java

License:asdf

@Override
public void pushEntries(BibDatabase database, final List<BibEntry> entries, final String keyString,
        MetaData metaData) {//from  ww  w .  ja va 2  s.c  om

    couldNotConnect = false;
    couldNotCall = false;
    notDefined = false;

    initParameters();
    commandPath = Globals.prefs.get(commandPathPreferenceKey);

    if ((commandPath == null) || commandPath.trim().isEmpty()) {
        notDefined = true;
        return;
    }

    if (!commandPath.endsWith(".in")) {
        commandPath = commandPath + ".in";
    }
    File lp = new File(commandPath); // this needs to fixed because it gives "asdf" when going prefs.get("lyxpipe")
    if (!lp.exists() || !lp.canWrite()) {
        // See if it helps to append ".in":
        lp = new File(commandPath + ".in");
        if (!lp.exists() || !lp.canWrite()) {
            couldNotConnect = true;
            return;
        }
    }

    final File lyxpipe = lp;

    JabRefExecutorService.INSTANCE.executeAndWait(() -> {
        try (FileWriter fw = new FileWriter(lyxpipe); BufferedWriter lyxOut = new BufferedWriter(fw)) {
            String citeStr;

            citeStr = "LYXCMD:sampleclient:citation-insert:" + keyString;
            lyxOut.write(citeStr + "\n");

            lyxOut.close();
            fw.close();
        } catch (IOException excep) {
            couldNotCall = true;
            LOGGER.warn("Problem pushing to LyX/Kile.", excep);
        }
    });
}

From source file:net.longfalcon.web.InstallController.java

@RequestMapping(value = "/install2", method = RequestMethod.POST)
public String installStepTwo(@ModelAttribute("installerForm") InstallerVO installerForm, Model model) {

    String nzbFileLocation = installerForm.getNzbFileLocation();
    Set<String> errorSet = new HashSet<>();

    File file = new File(nzbFileLocation);
    if (file.exists()) {
        if (!file.canRead() || !file.canWrite()) {
            errorSet.add(String.format("Location %s is not readable and/or writeable.", nzbFileLocation));
        }/* w w  w .  j ava2  s  .c o  m*/
        if (!file.isDirectory()) {
            errorSet.add(String.format("Location %s is not a directory.", nzbFileLocation));
        }
    } else {
        errorSet.add(String.format("Location %s does not exist.", nzbFileLocation));
    }

    if (errorSet.size() > 0) {
        model.addAttribute("title", INSTALL_STEP_2_TITLE);
        model.addAttribute("nzbPath", nzbFileLocation);
        model.addAttribute("installerForm", installerForm);
        model.addAttribute("errorSet", errorSet);
        return "install2";
    }

    try {
        installerService.initializeNzbStorage(nzbFileLocation);
    } catch (Exception e) {
        _log.error(e, e);
        errorSet.add(e.toString());
        model.addAttribute("title", INSTALL_STEP_2_TITLE);
        model.addAttribute("nzbPath", nzbFileLocation);
        model.addAttribute("installerForm", installerForm);
        model.addAttribute("errorSet", errorSet);
        return "install2";
    }

    model.asMap().clear();
    return "redirect:/";
}

From source file:com.tecapro.inventory.common.util.FileTempUtil.java

/**
 * check file deleteable/*from  ww w . ja  v a 2s  . c o  m*/
 */
public boolean isDeletable(String name, int index) {
    File file = new File(baseDir, getTempName(name, index));
    return file.isFile() && file.canWrite();
}

From source file:it.geosolutions.geoserver.sira.security.IrideSiraSecurityTest.java

private void copyConfigurationFile(File dataDirRoot, String configResource) throws IOException {
    // copy configuration to data directory
    assertTrue(dataDirRoot.canWrite());

    final File securityDir = new File(dataDirRoot, "security");
    assertNotNull(securityDir);//w ww .j av a  2  s .c  om
    if (!securityDir.exists()) {
        assertTrue(securityDir.mkdir());
    }
    assertTrue(securityDir.canWrite());
    final File configFile = new File(securityDir, CONFIG_FILE_DEST);
    try (final FileOutputStream fos = new FileOutputStream(configFile)) {
        IOUtils.copy(this.getClass().getResourceAsStream(configResource), fos);
    }
    assertTrue(configFile.exists());
}

From source file:com.tecapro.inventory.common.util.FileTempUtil.java

/**
 * check file writable//w  w w  . j a v a2 s. c  om
 */
public boolean isWritable(String name, int index) {
    if (name == null) {
        return true;
    }
    File file = new File(baseDir, getTempName(name, index));
    return !file.exists() || file.canWrite();
}

From source file:net.hillsdon.reviki.configuration.DataDirImpl.java

private File getWritableChildDir(final File dir, final String child) {
    File indexDir = new File(dir, child);
    if (!indexDir.exists()) {
        if (!indexDir.mkdir()) {
            return null;
        }/*w w  w  .  j a  v a 2  s  . co m*/
    }
    if (indexDir.isDirectory() && indexDir.canWrite()) {
        return indexDir;
    }
    return null;
}

From source file:io.vertigo.struts2.FileDownloader4Tests.java

/**
 * Perform the file/image download./*ww  w.  jav a2 s  . c  om*/
 *
 * @param element
 * @param attribute
 * @return downloadedFileAbsolutePath
 * @throws IOException
 * @throws NullPointerException
 */
private String downloader(final WebElement element, final String attribute)
        throws IOException, NullPointerException, URISyntaxException {
    final String fileToDownloadLocation = element.getAttribute(attribute);
    if (fileToDownloadLocation.trim().equals("")) {
        throw new NullPointerException("The element you have specified does not link to anything!");
    }

    final URL fileToDownload = new URL(fileToDownloadLocation);
    final File downloadedFile = new TempFile(localDownloadPath, "test");
    if (downloadedFile.canWrite() == false) {
        downloadedFile.setWritable(true);
    }

    try (final CloseableHttpClient client = HttpClientBuilder.create().build()) {
        final BasicHttpContext localContext = new BasicHttpContext();

        LOG.info("Mimic WebDriver cookie state: " + mimicWebDriverCookieState);
        if (mimicWebDriverCookieState) {
            localContext.setAttribute(ClientContext.COOKIE_STORE,
                    mimicCookieState(driver.manage().getCookies()));
        }

        final HttpGet httpget = new HttpGet(fileToDownload.toURI());
        final HttpParams httpRequestParameters = httpget.getParams();
        httpRequestParameters.setParameter(ClientPNames.HANDLE_REDIRECTS, followRedirects);
        httpget.setParams(httpRequestParameters);

        LOG.info("Sending GET request for: " + httpget.getURI());
        try (final CloseableHttpResponse response = client.execute(httpget, localContext)) {
            httpStatusOfLastDownloadAttempt = response.getStatusLine().getStatusCode();
            LOG.info("HTTP GET request status: " + httpStatusOfLastDownloadAttempt);
            LOG.info("Downloading file: " + downloadedFile.getName());
            FileUtils.copyInputStreamToFile(response.getEntity().getContent(), downloadedFile);
            //response.getEntity().getContent().close();
        }
    }

    final String downloadedFileAbsolutePath = downloadedFile.getAbsolutePath();
    LOG.info("File downloaded to '" + downloadedFileAbsolutePath + "'");

    return downloadedFileAbsolutePath;
}

From source file:net.ontopia.infoset.content.FileContentStore.java

public FileContentStore(File store_root) throws ContentStoreException {
    if (!store_root.canWrite())
        throw new ContentStoreException(
                "Content store root directory '" + store_root.getAbsoluteFile() + "' not writable.");

    // FIXME: should use java.nio.FileLock to ensure that only this
    // JVM accesses this directory. should also use some mechanism to
    // ensure that we are the only FileContentStore on this directory
    // within this JVM.
    ////from  ww  w  .j  a v  a2s  . co m
    // UPDATE: key file is now being protected using file locks. Still
    // need to add locks when deleting entries.

    // set up members
    this.store_root = store_root;
    this.files_per_directory = FILES_PER_DIRECTORY;
    this.open = true;
    this.key_file = new File(store_root, "keyfile.txt");

    // initialize
    allocateNewBlock();
}

From source file:FileTable2.java

public void setFileStats(File dir) {
    String files[] = dir.list();//www.j a  v a  2s  .c o m
    data = new Object[files.length][titles.length];

    for (int i = 0; i < files.length; i++) {
        File tmp = new File(files[i]);
        data[i][0] = new Boolean(tmp.isDirectory());
        data[i][1] = tmp.getName();
        data[i][2] = new Boolean(tmp.canRead());
        data[i][3] = new Boolean(tmp.canWrite());
        data[i][4] = new Long(tmp.length());
        data[i][5] = new Date(tmp.lastModified());
    }

    // Just in case anyone's listening...
    fireTableDataChanged();
}