List of usage examples for java.io File canWrite
public boolean canWrite()
From source file:Main.java
/** * Writes out the contents.//from ww w . j a va 2s . co m * @param outFile outFile * @param contents contents * @throws FileNotFoundException FileNotFoundException * @throws IOException IOException */ public static void setContents(final File outFile, final String contents) throws FileNotFoundException, IOException { if (outFile == null) { throw new IllegalArgumentException("File should not be null."); //$NON-NLS-1$ } if (outFile.exists()) { if (!outFile.isFile()) { throw new IllegalArgumentException("Should not be a directory: " + outFile); //$NON-NLS-1$ } if (!outFile.canWrite()) { throw new IllegalArgumentException("File cannot be written: " + outFile); //$NON-NLS-1$ } } Writer output = null; try { output = new BufferedWriter(new FileWriter(outFile)); if (contents != null) { output.write(contents); output.flush(); } } finally { if (output != null) { output.close(); } } }
From source file:it.geosolutions.geobatch.figis.intersection.Utilities.java
public static void writeOnShapeFile(File shapeFile, SimpleFeatureCollection collection) { if (shapeFile == null || collection == null || !shapeFile.canWrite()) { throw new IllegalArgumentException( "One or more input parameters are null or the file provided couldn't be read"); }/*from w w w . j ava 2 s. c om*/ ShapefileDataStore newDataStore = null; Transaction transaction = null; SimpleFeatureStore featureStore = null; try { newDataStore = new ShapefileDataStore(shapeFile.toURI().toURL()); newDataStore.createSchema(collection.getSchema()); transaction = new DefaultTransaction("create"); String typeName = newDataStore.getTypeNames()[0]; SimpleFeatureSource featureSource = newDataStore.getFeatureSource(typeName); featureStore = (SimpleFeatureStore) featureSource; featureStore.setTransaction(transaction); featureStore.addFeatures(collection); transaction.commit(); } catch (IOException e) { LOGGER.error(e.getMessage(), e); } finally { if (newDataStore != null) { newDataStore.dispose(); } if (transaction != null) { try { transaction.close(); } catch (IOException e) { LOGGER.error(e.getMessage(), e); } } if (featureStore != null) { newDataStore.dispose(); } } }
From source file:net.rim.ejde.internal.legacy.Util.java
static public IStatus save(Workspace workspace, boolean prompt) { if (workspace == null) { log.error("Workspace could not be saved. NULL workspace."); return new Status(IStatus.ERROR, ContextManager.PLUGIN_ID, 1, "", null); }//ww w . j a v a 2s .c om if (!workspace.getDirty()) { log.warn("Tried to save an unchanged workspace"); return Status.OK_STATUS; } File jdwFile = workspace.getFile(); if (jdwFile == null || !jdwFile.exists()) { log.error("Workspace could not be saved. JDW file does not exist or is null"); return new Status(IStatus.ERROR, ContextManager.PLUGIN_ID, 1, "", null); } boolean canChange = jdwFile.canWrite(); if (!canChange && prompt) { IStatus lStatus = FileUtils.canChange(jdwFile); if (lStatus.isOK()) { try { workspace.save(); } catch (IDEError e) { log.error("Workspace could not be saved.", e); return new Status(IStatus.ERROR, ContextManager.PLUGIN_ID, 1, "", e); } } else { log.error("Workspace could not be saved. User disallowed operation."); return new Status(IStatus.ERROR, ContextManager.PLUGIN_ID, 1, "", null); } } else { try { workspace.save(); } catch (IDEError e) { log.error("Workspace could not be saved.", e); return new Status(IStatus.ERROR, ContextManager.PLUGIN_ID, 1, "", e); } } return Status.OK_STATUS; }
From source file:com.binomed.showtime.android.util.CineShowtimeRequestManage.java
public static void completeMovieDetailStream(MovieBean movie) throws Exception { try {// w ww .j av a2 s.c o m File root = Environment.getExternalStorageDirectory(); if (root.canWrite()) { File posterFile = new File(root, new StringBuilder(CineShowtimeCst.FOLDER_POSTER) .append(movie.getId()).append(".jpg").toString()); posterFile.getParentFile().mkdirs(); if (posterFile.exists()) { Log.i(TAG, "img existe"); movie.setImgStream(new FileInputStream(posterFile)); } else { Log.i(TAG, "img existe pas : lancement de la requete"); HttpGet getMethod = CineShowtimeFactory.getHttpGet(); getMethod.setURI(new URI(movie.getUrlImg())); HttpResponse res = CineShowtimeFactory.getHttpClient().execute(getMethod); FileOutputStream fileOutPut = new FileOutputStream(posterFile); InputStream inputStream = res.getEntity().getContent(); byte[] tempon = new byte[10240]; while (true) { int nRead = inputStream.read(tempon, 0, tempon.length); if (nRead <= 0) { break; } fileOutPut.write(tempon, 0, nRead); } fileOutPut.close(); movie.setImgStream(new FileInputStream(posterFile)); } } else { Log.i(TAG, "SD not accessible : "); HttpGet getMethod = CineShowtimeFactory.getHttpGet(); getMethod.setURI(new URI(movie.getUrlImg())); HttpResponse res = CineShowtimeFactory.getHttpClient().execute(getMethod); InputStream inputStream = res.getEntity().getContent(); movie.setImgStream(inputStream); } } catch (IOException e) { Log.e(TAG, "Could not write file " + e.getMessage()); //$NON-NLS-1$ } }
From source file:de.cebitec.readXplorer.util.GeneralUtils.java
/** * Deletes the given file and if existent also the corresponding ".bai" * index file./* w ww . j a v a 2 s . co m*/ * @param lastWorkFile the file to delete * @return true, if the file could be deleted, false otherwise * @throws IOException */ public static boolean deleteOldWorkFile(File lastWorkFile) throws IOException { boolean deleted = false; if (lastWorkFile.canWrite()) { try { Files.delete(lastWorkFile.toPath()); deleted = true; File indexFile = new File(lastWorkFile.getAbsolutePath().concat(Properties.BAM_INDEX_EXT)); if (indexFile.canWrite()) { Files.delete(indexFile.toPath()); } } catch (IOException ex) { throw new IOException(NbBundle.getMessage(GeneralUtils.class, "MSG_GeneralUtils.FileDeletionError", lastWorkFile.getAbsolutePath())); } } return deleted; }
From source file:it.geosolutions.geobatch.figis.intersection.Utilities.java
/************** * This method create the tmpdirName dir in the temporary dir * /* w w w. ja va2 s. co m*/ * @param tmpDirName * @return * @throws IOException */ static File createTmpDir(String tmpDirName) throws IOException { // creates the temporary $tmp/figis and $tmpfigis/$layername LOGGER.trace("Creating the temp dir " + tmpDirName); final File sysTempDir = new File(System.getProperty("java.io.tmpdir")); String figisTmpDir = sysTempDir + "/" + tmpDirName; File tmpDir = new File(figisTmpDir); if (!tmpDir.exists()) { tmpDir.mkdirs(); } if (tmpDir.exists() && tmpDir.isDirectory() && tmpDir.canWrite()) { LOGGER.trace("Temp dir successfully created : " + tmpDir.getAbsolutePath()); return tmpDir; } else { LOGGER.error("Could not create 'figisTmpDir' (" + figisTmpDir + ")"); throw new IOException("Could not create 'figisTmpDir' (" + figisTmpDir + ")"); } }
From source file:com.zotoh.core.util.FileUte.java
private static void copyOneFile(File srcFile, File destFile) throws IOException { if (destFile.exists()) { if (!destFile.isFile()) { throw new IOException("\"" + destFile + "\" exists but is not a valid file"); }/* ww w . jav a2 s . c om*/ if (!destFile.canWrite()) { throw new IOException("Cannot overwrite \"" + destFile + "\""); } } InputStream src = new FileInputStream(srcFile); OutputStream out = null; try { streamToStream(src, out = new FileOutputStream(destFile)); } finally { StreamUte.close(out); StreamUte.close(src); } if (srcFile.length() != destFile.length()) { throw new IOException("Failed to copy full contents from '" + srcFile + "' to '" + destFile + "'"); } // preserve the file datetime destFile.setLastModified(srcFile.lastModified()); }
From source file:com.manning.androidhacks.hack040.util.DiskLruCache.java
/** * Used to fetch an instance of DiskLruCache. * //from w w w.j a va2s .com * @param context * @param cacheDir * @param maxByteSize * @return */ public static DiskLruCache openCache(Context context, File cacheDir, long maxByteSize) { if (!cacheDir.exists()) { cacheDir.mkdirs(); } if (cacheDir.isDirectory() && cacheDir.canWrite() && Utils.getUsableSpace(cacheDir) > maxByteSize) { return new DiskLruCache(cacheDir, maxByteSize); } return null; }
From source file:com.meltmedia.cadmium.core.FileSystemManager.java
public static String getNextDirInSequence(String lastDirName) { File lastDir = new File(lastDirName); String newDir = lastDirName;/*from w w w. j ava 2 s . c om*/ if (lastDir.exists()) { File parentDir = lastDir.getParentFile(); String dirName = lastDir.getName(); if (parentDir.canWrite()) { int nextNum = 0; Matcher fnameMatcher = FNAME_PATTERN.matcher(dirName); if (fnameMatcher.matches()) { nextNum = Integer.parseInt(fnameMatcher.group(2)); dirName = fnameMatcher.group(1); } nextNum++; dirName += "_" + nextNum; File newDirFile = new File(parentDir, dirName); if (newDirFile.exists()) { deleteDeep(newDirFile.getAbsolutePath()); } newDir = newDirFile.getAbsolutePath(); } else { newDir = null; } } return newDir; }
From source file:com.silverpeas.util.FileUtil.java
/** * Copies the specified source file to the specified destination. If the destination exists, it is * then replaced by the source. If the destination can be overwritten, its write property is set * before the copy./*from w ww . ja va 2 s .c om*/ * * @param source the file to copy. * @param destination the destination file of the move. * @throws IOException if the source or the destination is invalid or if an error occurs while * copying the file. */ public static void copyFile(File source, File destination) throws IOException { if (destination.exists() && !destination.canWrite()) { destination.setWritable(true); } FileUtils.copyFile(source, destination); }