List of usage examples for java.io File canWrite
public boolean canWrite()
From source file:eu.stratosphere.nephele.taskmanager.TaskManager.java
/** * Checks, whether the given strings describe existing directories that are writable. If that is not * the case, an exception is raised.//from www .j a va2 s. co m * * @param tempDirs * An array of strings which are checked to be paths to writable directories. * @throws Exception * Thrown, if any of the mentioned checks fails. */ private static final void checkTempDirs(final String[] tempDirs) throws Exception { for (int i = 0; i < tempDirs.length; ++i) { final String dir = tempDirs[i]; if (dir == null) { throw new Exception("Temporary file directory #" + (i + 1) + " is null."); } final File f = new File(dir); if (!f.exists()) { throw new Exception("Temporary file directory '" + f.getAbsolutePath() + "' does not exist."); } if (!f.isDirectory()) { throw new Exception("Temporary file directory '" + f.getAbsolutePath() + "' is not a directory."); } if (!f.canWrite()) { throw new Exception("Temporary file directory '" + f.getAbsolutePath() + "' is not writable."); } } }
From source file:RolloverFileOutputStream.java
private synchronized void setFile() throws IOException { // Check directory File file = new File(_filename); _filename = file.getCanonicalPath(); file = new File(_filename); File dir = new File(file.getParent()); if (!dir.isDirectory() || !dir.canWrite()) throw new IOException("Cannot write log directory " + dir); Date now = new Date(); // Is this a rollover file? String filename = file.getName(); int i = filename.toLowerCase().indexOf(YYYY_MM_DD); if (i >= 0) { file = new File(dir, filename.substring(0, i) + _fileDateFormat.format(now) + filename.substring(i + YYYY_MM_DD.length())); }//from w w w. j ava 2 s . co m if (file.exists() && !file.canWrite()) throw new IOException("Cannot write log file " + file); // Do we need to change the output stream? if (out == null || !file.equals(_file)) { // Yep _file = file; if (!_append && file.exists()) file.renameTo(new File(file.toString() + "." + _fileBackupFormat.format(now))); OutputStream oldOut = out; out = new FileOutputStream(file.toString(), _append); if (oldOut != null) oldOut.close(); // if(log.isDebugEnabled())log.debug("Opened "+_file); } }
From source file:com.amazonaws.mturk.cmd.MakeTemplate.java
private void copyTemplateFile(String sourceRoot, String targetRoot, String extension) throws Exception { String inputFileName = sourceRoot + extension; String outputFileName = targetRoot + extension; System.out.println("Copying resource file: " + outputFileName); File inputFile = new File(inputFileName); if (!inputFile.exists() || !inputFile.canRead()) { throw new Exception("Could not read from the file " + inputFileName); }/*from w ww . j a va 2 s . co m*/ File outputFile = new File(outputFileName); if (!outputFile.exists()) { if (!outputFile.createNewFile() || !outputFile.canWrite()) throw new Exception("Could not write to the file " + outputFileName); } // copy file FileReader in = new FileReader(inputFile); FileWriter out = new FileWriter(outputFile); try { char[] buffer = new char[1024]; int nread = 0; while ((nread = in.read(buffer)) != -1) { out.write(buffer, 0, nread); } } finally { in.close(); out.close(); } }
From source file:com.piaoyou.util.FileUtil.java
public static void copyFile(File org, File des, boolean overwrite) throws IOException { if (!org.exists()) { throw new FileNotFoundException("Cannot find the source file: " + org.getAbsolutePath()); }//from w w w .j ava2 s . c o m if (!org.canRead()) { throw new IOException("Cannot read the source file: " + org.getAbsolutePath()); } if (overwrite == false) { if (des.exists()) { return; } } else { if (des.exists()) { if (!des.canWrite()) { throw new IOException("Cannot write the destination file: " + des.getAbsolutePath()); } } else { if (!des.createNewFile()) { throw new IOException("Cannot write the destination file: " + des.getAbsolutePath()); } } } BufferedInputStream inputStream = null; BufferedOutputStream outputStream = null; byte[] block = new byte[1024]; try { inputStream = new BufferedInputStream(new FileInputStream(org)); outputStream = new BufferedOutputStream(new FileOutputStream(des)); while (true) { int readLength = inputStream.read(block); if (readLength == -1) break;// end of file outputStream.write(block, 0, readLength); } } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException ex) { log.error("Cannot close stream", ex); } } if (outputStream != null) { try { outputStream.close(); } catch (IOException ex) { log.error("Cannot close stream", ex); } } } }
From source file:com.technicolor.qeo.codegen.Main.java
private File getOutputDirectory(String dirStr) { File dir = new File(dirStr); if (dir.exists()) { if (!dir.isDirectory()) { usageExit("Output argument is not a directory: " + dirStr); } else if (!dir.canWrite()) { usageExit("Output directory is not writable: " + dirStr); }//from w w w . j av a 2s .com } else { if (!dir.mkdirs()) { usageExit("Unable to create output directory: " + dirStr); } } return dir; }
From source file:ddf.catalog.backup.CatalogBackupPlugin.java
private void validateDirectory(File directory) { if (!(directory.isDirectory() && directory.canWrite())) { throw new IllegalArgumentException( "Directory " + directory.getAbsolutePath() + " does not exist or is not writable"); }/*from w w w . j a v a 2 s . c om*/ }
From source file:de.uniwue.info6.database.jaxb.ScenarioExporter.java
/** * * * @param files//from w ww. j av a 2s . co m * @param zipfile * @return */ private File zip(List<File> files, File zipfile) { byte[] buf = new byte[1024]; try { ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile)); for (int i = 0; i < files.size(); i++) { FileInputStream in = new FileInputStream(files.get(i)); out.putNextEntry(new ZipEntry(files.get(i).getName())); int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.closeEntry(); in.close(); } out.close(); for (File file : files) { if (file.isFile() && file.exists() && file.canWrite()) { file.delete(); } } return zipfile; } catch (Exception e) { LOGGER.error("FAILED TO ZIP FILES", e); } return null; }
From source file:com.fseport.sftp.SftpUtil.java
/** * Should be moved to a util class that is not based on an endpoint... TODO: why * is this method synchronized?//w ww . j av a2s. c o m * * @param input * @param destination * @throws IOException */ public synchronized void copyStreamToFile(InputStream input, File destination) throws IOException { try { File folder = destination.getParentFile(); if (!folder.exists()) { throw new IOException("Destination folder does not exist: " + folder); } if (!folder.canWrite()) { throw new IOException("Destination folder is not writeable: " + folder); } FileOutputStream output = new FileOutputStream(destination); try { IOUtils.copy(input, output); } finally { if (output != null) output.close(); } } catch (IOException ex) { setErrorOccurredOnInputStream(input); throw ex; } catch (RuntimeException ex) { setErrorOccurredOnInputStream(input); throw ex; } finally { if (input != null) input.close(); } }
From source file:com.lazerycode.selenium.filedownloader.FileDownloader.java
/** * Perform the file/image download.//from www.jav a2 s.c o m * * @param element * @param attribute * @return * @throws IOException * @throws NullPointerException */ private String downloader(WebElement element, String attribute, String Filename) throws IOException, NullPointerException, URISyntaxException { String fileToDownloadLocation = element.getAttribute(attribute); if (fileToDownloadLocation.trim().equals("")) throw new NullPointerException("The element you have specified does not link to anything!"); URL fileToDownload = new URL(fileToDownloadLocation); //changed by Raul File downloadedFile = new File(Filename); //+ " fileToDownload.getFile().replaceFirst("/|\\\\", "").replace("?", "")); if (downloadedFile.canWrite() == false) downloadedFile.setWritable(true); HttpClient client = new DefaultHttpClient(); BasicHttpContext localContext = new BasicHttpContext(); //LOG.info("Mimic WebDriver cookie state: " + this.mimicWebDriverCookieState); if (this.mimicWebDriverCookieState) { localContext.setAttribute(ClientContext.COOKIE_STORE, mimicCookieState(this.driver.manage().getCookies())); } HttpGet httpget = new HttpGet(fileToDownload.toURI()); HttpParams httpRequestParameters = httpget.getParams(); httpRequestParameters.setParameter(ClientPNames.HANDLE_REDIRECTS, this.followRedirects); httpget.setParams(httpRequestParameters); // LOG.info("Sending GET request for: " + httpget.getURI()); HttpResponse response = client.execute(httpget, localContext); this.httpStatusOfLastDownloadAttempt = response.getStatusLine().getStatusCode(); //LOG.info("HTTP GET request status: " + this.httpStatusOfLastDownloadAttempt); //LOG.info("Downloading file: " + downloadedFile.getName()); FileUtils.copyInputStreamToFile(response.getEntity().getContent(), downloadedFile); response.getEntity().getContent().close(); String downloadedFileAbsolutePath = downloadedFile.getAbsolutePath(); // LOG.info("File downloaded to '" + downloadedFileAbsolutePath + "'"); return downloadedFileAbsolutePath; }
From source file:io.jenkins.blueocean.blueocean_git_pipeline.GitCacheCloneReadSaveRequest.java
@Override void save() throws IOException { invokeOnScm(new GitSCMFileSystem.FSFunction<Void>() { @Override/*from w w w .ja v a 2 s . c o m*/ public Void invoke(Repository repository) throws IOException, InterruptedException { Git activeRepo = getActiveRepository(repository); Repository repo = activeRepo.getRepository(); File repoDir = repo.getDirectory().getParentFile(); log.fine("Repo cloned to: " + repoDir.getCanonicalPath()); try { File f = new File(repoDir, filePath); if (!f.exists() || f.canWrite()) { try (Writer w = new OutputStreamWriter(new FileOutputStream(f), "utf-8")) { w.write(new String(contents, "utf-8")); } try { AddCommand add = activeRepo.add(); add.addFilepattern(filePath); add.call(); CommitCommand commit = activeRepo.commit(); commit.setMessage(commitMessage); commit.call(); // Push the changes GitUtils.push(gitSource.getRemote(), repo, getCredential(), LOCAL_REF_BASE + sourceBranch, REMOTE_REF_BASE + branch); } catch (GitAPIException ex) { throw new ServiceException.UnexpectedErrorException(ex.getMessage(), ex); } return null; } throw new ServiceException.UnexpectedErrorException("Unable to write " + filePath); } finally { FileUtils.deleteDirectory(repoDir); } } }); }