List of usage examples for java.io File getPath
public String getPath()
From source file:com.flysystem.core.adapter.local.FileCommands.java
/** * * @param local The local filesystem to check existence of files against. * @param source Must exists. Throws FileNotFoundException otherwise. * @param target Must not exist. Throws FileExistsException otherwise. * @param command The command to be executed. *//*from w w w .j av a 2 s.co m*/ public static void manipulate(Local local, File source, File target, ManipulateFileCommand command) { validateIsFileAndExists(source); if (target.exists()) throw new FileExistsException(target.getPath()); local.ensureDirectory(target); command.execute(source, target); }
From source file:edu.harvard.mcz.imagecapture.ThumbnailBuilder.java
public static File getThumbFileForFile(File file) { File thumbsDir = new File(file.getParentFile().getPath() + File.separator + "thumbs"); File result = new File(thumbsDir.getPath().concat(File.separator).concat(file.getName())); return result; }
From source file:Main.java
/** * Add a zip entry into the provided <code>ZipOutputStream</code>. The zip * entry is the part of <code>filePathToZip</code> truncated with the * <code>baseFolderPath</code>. * <p>//from w w w . j a va2s . co m * So a file or folder <code>c:\my\base\folder\my\file\to\zip.txt</code> * will be added in archive using <code>my/file/to/zip.txt</code> entry. */ public static void addZipEntry(ZipOutputStream zos, File fileToZip, File baseFolder) { if (!baseFolder.isDirectory()) { throw new IllegalArgumentException(baseFolder.getPath() + " is not a directory."); } if (fileToZip.isDirectory()) { final File[] files = fileToZip.listFiles(); for (final File file : files) { addZipEntry(zos, file, baseFolder); } } else { final String filePathToZip; final int start; try { filePathToZip = fileToZip.getCanonicalPath(); start = baseFolder.getCanonicalPath().length() + 1; } catch (final IOException e1) { throw new IllegalStateException(e1); } final int end = filePathToZip.length(); String entryName = filePathToZip.substring(start, end); entryName = entryName.replace(File.separatorChar, '/'); final FileInputStream inputStream; try { inputStream = new FileInputStream(filePathToZip); } catch (final FileNotFoundException e) { throw new IllegalStateException(e); } addEntryInputStream(zos, entryName, inputStream); } }
From source file:Main.java
public static File getLogPath(Context context) { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { File externalStorage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); if (externalStorage == null) return null; return new File(externalStorage.getPath() + File.separatorChar + "logs"); }//from w ww.j a v a 2s . c o m return null; }
From source file:Main.java
static File make_tmpdir(Context context, SQLiteDatabase db) throws Exception { File extdir = Environment.getExternalStorageDirectory(); File tmp_top = new File(extdir, "tmp_LongText"); if (!tmp_top.exists()) { if (!tmp_top.mkdir()) throw new Exception("cannot create directory: " + tmp_top.getPath()); }/*from w w w . ja v a 2 s .co m*/ if (!tmp_top.canWrite()) throw new Exception("missing permission to write to " + tmp_top.getPath()); File tmpdir; Random r = new Random(); do { tmpdir = new File(tmp_top, String.format("%d", r.nextInt())); } while (tmpdir.exists()); if (!tmpdir.mkdir()) throw new Exception("cannot create directory: " + tmp_top.getPath()); if (!tmpdir.canWrite()) throw new Exception("missing permission to write to " + tmp_top.getPath()); ContentValues v = new ContentValues(); v.put("pid", Process.myPid()); v.put("tmpdir", tmpdir.getPath()); v.put("ctime", System.currentTimeMillis()); db.insert("tmpdir", null, v); return tmpdir; }
From source file:Main.java
/** * Helper method to calculate the proper size limit of a cache instance. *//*from www . j a va 2s.c om*/ public static long getCacheSizeInBytes(File dir, float cacheSizePercent, long maxSizeInBytes) { if (dir == null || (!dir.exists() && !dir.mkdir())) { return 0; } try { StatFs stat = new StatFs(dir.getPath()); long totalBytes = stat.getBlockCountLong() * stat.getBlockSizeLong(); long freeBytes = stat.getAvailableBlocksLong() * stat.getBlockSizeLong(); long desiredBytes = Math.min((long) (totalBytes * cacheSizePercent), maxSizeInBytes); // If free space is less than desired, use half of the free disk space instead. desiredBytes = (desiredBytes > freeBytes) ? freeBytes / 2 : desiredBytes; return desiredBytes; } catch (IllegalArgumentException e) { return 0; } }
From source file:com.silverpeas.gallery.constant.MediaMimeType.java
/** * Finds from the given file the corresponding {@link MediaMimeType} * @param file//from ww w . ja v a 2 s . c o m * @return {@link MediaMimeType} */ public static MediaMimeType fromFile(File file) { return fromMimeType(file != null ? FileUtil.getMimeType(file.getPath()) : null); }
From source file:Main.java
public static Bitmap decode(File file, int targetWidth, int targetHeight) { if (file == null || !file.exists() || file.length() == 0) { return null; }//from ww w . j av a 2 s .co m String pathName = file.getPath(); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(pathName, options); /* * If set to a value > 1, requests the decoder to subsample the original * image, returning a smaller image to save memory. The sample size is * the number of pixels in either dimension that correspond to a single * pixel in the decoded bitmap. For example, inSampleSize == 4 returns * an image that is 1/4 the width/height of the original, and 1/16 the * number of pixels. Any value <= 1 is treated the same as 1. Note: the * decoder uses a final value based on powers of 2, any other value will * be rounded down to the nearest power of 2. */ options.inSampleSize = computeImageSampleSize(options, targetWidth, targetHeight); options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(pathName, options); }
From source file:io.wcm.devops.conga.plugins.aem.tooling.crypto.cli.AnsibleVault.java
private static void handleFile(File file, Function<byte[], byte[]> vaultHandler) throws IOException { if (!file.exists()) { throw new FileNotFoundException(file.getPath()); }//from www .j a va 2 s .c o m byte[] input = FileUtils.readFileToByteArray(file); byte[] output = vaultHandler.apply(input); file.delete(); FileUtils.writeByteArrayToFile(file, output); }
From source file:com.feilong.tools.net.ZClientTest.java
/** * Gets the files.// w ww .j a v a 2s . com * * @param ftp * the ftp * @param localDir * the local dir * @throws IOException * Signals that an I/O exception has occurred. */ private static void testGetFiles(FTPClient ftp, File localDir) throws IOException { String[] names = ftp.listNames(); for (String name : names) { File file = new File(localDir.getPath() + File.separator + name); if (!file.exists()) { file.createNewFile(); } long pos = file.length(); RandomAccessFile raf = new RandomAccessFile(file, "rw"); raf.seek(pos); ftp.setRestartOffset(pos); InputStream is = ftp.retrieveFileStream(name); if (is == null) { log.info("no such file:" + name); } else { log.info("start getting file:" + name); int b; while ((b = is.read()) != -1) { raf.write(b); } is.close(); if (ftp.completePendingCommand()) { log.info("done!"); } else { log.info("can't get file:" + name); } } raf.close(); } }