List of usage examples for java.io File canWrite
public boolean canWrite()
From source file:FileCopy.java
/** * The static method that actually performs the file copy. Before copying the * file, however, it performs a lot of tests to make sure everything is as it * should be.//from w w w .j a v a2 s . c o m */ public static void copy(String from_name, String to_name) throws IOException { File from_file = new File(from_name); // Get File objects from Strings File to_file = new File(to_name); // First make sure the source file exists, is a file, and is readable. // These tests are also performed by the FileInputStream constructor // which throws a FileNotFoundException if they fail. if (!from_file.exists()) abort("no such source file: " + from_name); if (!from_file.isFile()) abort("can't copy directory: " + from_name); if (!from_file.canRead()) abort("source file is unreadable: " + from_name); // If the destination is a directory, use the source file name // as the destination file name if (to_file.isDirectory()) to_file = new File(to_file, from_file.getName()); // If the destination exists, make sure it is a writeable file // and ask before overwriting it. If the destination doesn't // exist, make sure the directory exists and is writeable. if (to_file.exists()) { if (!to_file.canWrite()) abort("destination file is unwriteable: " + to_name); // Ask whether to overwrite it System.out.print("Overwrite existing file " + to_file.getName() + "? (Y/N): "); System.out.flush(); // Get the user's response. BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String response = in.readLine(); // Check the response. If not a Yes, abort the copy. if (!response.equals("Y") && !response.equals("y")) abort("existing file was not overwritten."); } else { // If file doesn't exist, check if directory exists and is // writeable. If getParent() returns null, then the directory is // the current dir. so look up the user.dir system property to // find out what that is. String parent = to_file.getParent(); // The destination directory if (parent == null) // If none, use the current directory parent = System.getProperty("user.dir"); File dir = new File(parent); // Convert it to a file. if (!dir.exists()) abort("destination directory doesn't exist: " + parent); if (dir.isFile()) abort("destination is not a directory: " + parent); if (!dir.canWrite()) abort("destination directory is unwriteable: " + parent); } // If we've gotten this far, then everything is okay. // So we copy the file, a buffer of bytes at a time. FileInputStream from = null; // Stream to read from source FileOutputStream to = null; // Stream to write to destination try { from = new FileInputStream(from_file); // Create input stream to = new FileOutputStream(to_file); // Create output stream byte[] buffer = new byte[4096]; // To hold file contents int bytes_read; // How many bytes in buffer // Read a chunk of bytes into the buffer, then write them out, // looping until we reach the end of the file (when read() returns // -1). Note the combination of assignment and comparison in this // while loop. This is a common I/O programming idiom. while ((bytes_read = from.read(buffer)) != -1) // Read until EOF to.write(buffer, 0, bytes_read); // write } // Always close the streams, even if exceptions were thrown finally { if (from != null) try { from.close(); } catch (IOException e) { ; } if (to != null) try { to.close(); } catch (IOException e) { ; } } }
From source file:com.frostwire.platform.DefaultFileSystem.java
@Override public boolean canWrite(File file) { return file.canWrite(); }
From source file:com.orange.ocara.model.export.docx.DocxWriter.java
/** * To check a file. File must exists, be writeable * * @param file file to check//w ww .ja va2s.co m * @return true if ok, false otherwise */ private boolean checkFile(File file) { return file != null && file.exists() && file.canWrite() && file.isFile(); }
From source file:org.eclipse.gemini.blueprint.test.internal.util.FileSystemStorageTest.java
public void testResourceForTempFile() throws Exception { Resource res = storage.getResource(); assertTrue(res.exists());/*from www. j a va2 s . c o m*/ File tempFile = res.getFile(); assertTrue(tempFile.exists()); assertTrue(tempFile.canRead()); assertTrue(tempFile.canWrite()); }
From source file:com.orange.ocara.model.export.docx.DocxWriter.java
/** * To check a directory. Directory must exists, be writeable. * * @param directory Directory to check// ww w . j a va2 s .co m * @return true if ok, false otherwise */ private boolean checkDirectory(File directory) { return directory != null && directory.exists() && directory.canWrite() && directory.isDirectory(); }
From source file:com.qcadoo.plugin.internal.filemanager.DefaultPluginFileManager.java
private boolean checkFileRightsToWrite(final String pluginsPath) { File file = new File(pluginsPath); if (!file.exists() || !file.canWrite()) { return false; }//www . j a v a2 s. c om return true; }
From source file:com.erudika.para.storage.LocalFileStore.java
@Override public boolean delete(String path) { if (StringUtils.startsWith(path, File.separator)) { path = path.substring(1);// w ww. j a va 2 s. c o m } if (!StringUtils.isBlank(path)) { File f = new File(folder + File.separator + path); return f.canWrite() && f.delete(); } return false; }
From source file:com.github.rnewson.couchdb.lucene.ConfigTest.java
@Test public void testGetDir() { try {//from w w w .j av a 2 s. com final Config config = new Config(); File dir = config.getDir(); assertTrue(dir.exists()); assertTrue(dir.canRead()); assertTrue(dir.canWrite()); assertEquals(new File("target", "indexes"), dir); } catch (ConfigurationException ce) { fail("ConfigurationException shouldn't have been thrown." + ce.getMessage()); } catch (IOException ioe) { fail("IOException shouldn't have been thrown." + ioe.getMessage()); } }
From source file:com.jbrisbin.vpc.jobsched.patch.PatchMessageHandler.java
public void handleMessage(final PatchMessage msg) { if (null != msg.getFile()) { // Read original file List<String> original = new ArrayList<String>(); File originalFile = new File(msg.getFile()); try {//from w w w. ja v a 2 s . co m BufferedReader reader = new BufferedReader(new FileReader(originalFile)); String line = null; while (null != (line = reader.readLine())) { original.add(line); } reader.close(); } catch (FileNotFoundException e) { log.error(e.getMessage(), e); } catch (IOException e) { log.error(e.getMessage(), e); } if (log.isDebugEnabled()) { log.debug("Patching original file: " + original); } // Patch it Patch patch = DiffUtils.parseUnifiedDiff(msg.getDiff()); try { List<String> updated = (List<String>) DiffUtils.patch(original, patch); File tmp = File.createTempFile("patcher_", "patched"); BufferedWriter writer = new BufferedWriter(new FileWriter(tmp)); for (String line : updated) { writer.write(line + "\n"); } writer.flush(); writer.close(); // Backup existing file File basedir = originalFile.getParentFile(); if (basedir.canWrite()) { File backup = new File(originalFile.getParentFile(), originalFile.getName() + ".bkp." + fmt.format(Calendar.getInstance().getTime())); originalFile.renameTo(backup); // Replace with patched version tmp.renameTo(originalFile); } } catch (PatchFailedException e) { log.error(e.getMessage(), e); } catch (IOException e) { log.error(e.getMessage(), e); } } }
From source file:com.samir.commons.java.jarinstallerkit.jarinstaller.JarInstaller.java
public void starter(final String[] args) throws Exception { LOGGER.log(Level.INFO, "shutdown Previous"); shutdownPrevious();// w w w .j av a 2s .c om LOGGER.log(Level.INFO, "start disconnector Service"); disconnectorService(); File finalDir = new File(Constants.getInstallDirectorySeparator()); if (finalDir.canWrite()) { doInstallLifeCycle(args); } //Remove the old tmp file try { File appTmp = new File(Constants.FINAL_APP_TMP); if (appTmp.exists()) { appTmp.delete(); } } catch (Exception e) { } }