List of usage examples for java.io File getParentFile
public File getParentFile()
null
if this pathname does not name a parent directory. From source file:Main.java
public static void openNamedFile(String filename) { try {/* w ww.j a v a 2s . c o m*/ File f = new File(filename); // Log.e("kuinfa", "filename= " + filename); FileInputStream fis = new FileInputStream(f); long size = f.length(); name = f.getName(); patch = f.getParentFile().toString(); DataInputStream dis = new DataInputStream(fis); byte[] b = new byte[(int) size]; int length = dis.read(b, 0, (int) size); dis.close(); fis.close(); String ttt = new String(b, 0, length, "UTF-8"); try { ttt = new String(ttt.getBytes(), "UTF-8"); } catch (UnsupportedEncodingException e) { } } catch (FileNotFoundException e) { } catch (IOException e) { } }
From source file:Main.java
public static void unzip(URL _url, URL _dest, boolean _remove_top) { int BUFFER_SZ = 2048; File file = new File(_url.getPath()); String dest = _dest.getPath(); new File(dest).mkdir(); try {// ww w . ja v a 2s .c om ZipFile zip = new ZipFile(file); Enumeration<? extends ZipEntry> zipFileEntries = zip.entries(); // Process each entry while (zipFileEntries.hasMoreElements()) { // grab a zip file entry ZipEntry entry = (ZipEntry) zipFileEntries.nextElement(); String currentEntry = entry.getName(); if (_remove_top) currentEntry = currentEntry.substring(currentEntry.indexOf('/'), currentEntry.length()); File destFile = new File(dest, currentEntry); //destFile = new File(newPath, destFile.getName()); File destinationParent = destFile.getParentFile(); // create the parent directory structure if needed destinationParent.mkdirs(); if (!entry.isDirectory()) { BufferedInputStream is; is = new BufferedInputStream(zip.getInputStream(entry)); int currentByte; // establish buffer for writing file byte data[] = new byte[BUFFER_SZ]; // write the current file to disk FileOutputStream fos = new FileOutputStream(destFile); BufferedOutputStream dst = new BufferedOutputStream(fos, BUFFER_SZ); // read and write until last byte is encountered while ((currentByte = is.read(data, 0, BUFFER_SZ)) != -1) { dst.write(data, 0, currentByte); } dst.flush(); dst.close(); is.close(); } /* if (currentEntry.endsWith(".zip")) { // found a zip file, try to open extractFolder(destFile.getAbsolutePath()); }*/ } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:Main.java
public static boolean saveObjectToFile(String filePath, Object object) { if (!TextUtils.isEmpty(filePath)) { File cacheFile = null; try {/* w w w . j a v a2 s. c om*/ cacheFile = new File(filePath); if (cacheFile.exists()) { cacheFile.delete(); } if (!cacheFile.getParentFile().exists()) { cacheFile.getParentFile().mkdirs(); } cacheFile.createNewFile(); } catch (Throwable var6) { var6.printStackTrace(); cacheFile = null; } if (cacheFile != null) { try { FileOutputStream t = new FileOutputStream(cacheFile); GZIPOutputStream gzos = new GZIPOutputStream(t); ObjectOutputStream oos = new ObjectOutputStream(gzos); oos.writeObject(object); oos.flush(); oos.close(); return true; } catch (Throwable var7) { var7.printStackTrace(); } } } return false; }
From source file:com.tealcube.minecraft.bukkit.facecore.utilities.IOUtils.java
/** * Create a {@link java.io.File} from the given {@link java.io.File}. * * @param file File to create//from w w w. j a va 2 s .c o m * @return if creation was successful */ public static boolean createFile(File file) { Validate.notNull(file, "file cannot be null"); boolean succeeded = file.exists(); if (!succeeded) { try { if (!createDirectory(file.getParentFile())) { return false; } succeeded = file.createNewFile(); } catch (IOException ignored) { // do nothing here } } return succeeded; }
From source file:au.org.ala.delta.util.FileUtils.java
public static File findFileIgnoreCase(File file) { if (file.exists()) { return file; }// w ww . ja v a2s .co m File parent = file.getParentFile(); if (parent == null) { parent = new File(System.getProperty("user.dir")); } if (parent.exists()) { String[] matches = parent.list(new CaseInsenstiveFilenameMatcher(file.getName())); if (matches.length > 0) { String newFilename = String.format("%s%s%s", parent.getAbsolutePath(), File.separator, matches[0]); File candidateFile = new File(newFilename); if (candidateFile.exists()) { return candidateFile; } } } return null; }
From source file:com.aliyun.odps.local.common.utils.ArchiveUtils.java
/** * Given a File input it will unzip the file in a the unzip directory passed * as the second parameter//w w w . j ava 2s.c o m * * @param inFile * The zip file as input * @param unzipDir * The unzip directory where to unzip the zip file. * @throws IOException */ public static void unZip(File inFile, File unzipDir) throws IOException { Enumeration<? extends ZipEntry> entries; ZipFile zipFile = new ZipFile(inFile); try { entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); if (!entry.isDirectory()) { InputStream in = zipFile.getInputStream(entry); try { File file = new File(unzipDir, entry.getName()); if (!file.getParentFile().mkdirs()) { if (!file.getParentFile().isDirectory()) { throw new IOException("Mkdirs failed to create " + file.getParentFile().toString()); } } OutputStream out = new FileOutputStream(file); try { byte[] buffer = new byte[8192]; int i; while ((i = in.read(buffer)) != -1) { out.write(buffer, 0, i); } } finally { out.close(); } } finally { in.close(); } } } } finally { zipFile.close(); } }
From source file:com.jaeksoft.searchlib.util.FileUtils.java
public final static boolean isSubDirectory(File base, File child) throws IOException { base = base.getCanonicalFile();/* w w w . j a va 2 s . co m*/ child = child.getCanonicalFile(); File parent = child; while (parent != null) { if (base.equals(parent)) return true; parent = parent.getParentFile(); } return false; }
From source file:mobile.service.core.FileService.java
/** * ?//from w ww .j ava 2s . com * * @param sourceFile ? * @param destFile */ public static void copyFile(String sourceFile, String destFile) { File dest = new File(destFile); if (!dest.exists()) {// ? dest.getParentFile().mkdirs(); } try (FileInputStream in = new FileInputStream(sourceFile); FileOutputStream out = new FileOutputStream(destFile)) { FileUtils.copy(in, out); } catch (Exception e) { LOGGER.error("fail to copyFile. sourceFile = " + sourceFile + " ,destFile = " + destFile, e); } }
From source file:de.fmaul.android.cmis.utils.StorageUtils.java
private static void ensureOrCreatePathAndFile(File contentFile) { try {//from www. ja v a 2 s.com contentFile.getParentFile().mkdirs(); contentFile.createNewFile(); } catch (IOException iox) { throw new RuntimeException(iox); } }
From source file:Main.java
public static void copyFile(File fromFile, File toFile, Boolean rewrite) { if (!fromFile.exists()) { return;//from w w w .java 2s . c om } if (!fromFile.isFile()) { return; } if (!fromFile.canRead()) { return; } if (!toFile.getParentFile().exists()) { toFile.getParentFile().mkdirs(); } if (toFile.exists() && rewrite) { toFile.delete(); } try { java.io.FileInputStream fosfrom = new java.io.FileInputStream(fromFile); java.io.FileOutputStream fosto = new FileOutputStream(toFile); byte bt[] = new byte[1024]; int c; while ((c = fosfrom.read(bt)) > 0) { fosto.write(bt, 0, c); } fosfrom.close(); fosto.close(); } catch (Exception ex) { ex.printStackTrace(); } }