List of usage examples for java.io File getParent
public String getParent()
null
if this pathname does not name a parent directory. From source file:com.uas.Files.FilesDAO.java
@Override public File getUniqueFilename(File file) { String baseName = FilenameUtils.getBaseName(file.getName()); String extension = FilenameUtils.getExtension(file.getName()); int counter = 1; while (file.exists()) { file = new File(file.getParent(), baseName + "-" + (counter++) + "." + extension); }// ww w . j a v a 2 s . co m return file; }
From source file:cc.recommenders.io.Directory.java
public Directory getParentDirectory(String relativeFileName) { File f = new File(rootDir, relativeFileName); Asserts.assertTrue(f.exists());/*w w w. j a va 2 s. co m*/ Asserts.assertFalse(f.getAbsolutePath().contains("..")); return new Directory(f.getParent()); }
From source file:com.aurel.track.dbase.HandleHome.java
/** * Checks if the file with this name has been modified in the meantime. For this we compute the files * hash value and look if it exists in the hash.txt file where we keep them. * @param existingFile//from w w w. j av a2 s . c o m * @param targetDirAbsolutePath * @return */ public static boolean fileIsTheOriginal(File existingFile, String hash) { BufferedReader br = null; try { File hashFile = new File(existingFile.getParent() + File.separator + "hash.txt"); if (hashFile.exists()) { br = new BufferedReader(new FileReader(hashFile)); String line; String fname = ""; while ((line = br.readLine()) != null) { String[] keyv = line.split("="); if (keyv != null && keyv.length > 1 && keyv[1] != null) { fname = keyv[0]; if (fname.equals(existingFile.getName()) && hash.equals(keyv[1])) { br.close(); return true; } } } br.close(); } } catch (Exception e) { LOGGER.warn(e.getMessage()); } return false; }
From source file:au.edu.unsw.cse.soc.federatedcloud.community.driven.cloudbase.connectors.juju.JujuConnector.java
private int packageFinalResourceDescription(File charmDir, File deploymentShellScript) { Random rand = new Random(); //referred from http://stackoverflow.com/questions/363681/generating-random-integers-in-a-range-with-java int randomNum = rand.nextInt((1000 - 1) + 1) + 1; File outputFile = new File(charmDir.getParent() + File.separator + randomNum + ".zip"); ZipUtil.pack(charmDir, outputFile);//from www . j a v a 2 s . c o m return randomNum; }
From source file:com.sun.portal.portletcontainer.driver.admin.UploadServlet.java
private String processFileItem(FileItem fi) throws FileUploadException { // On some browsers fi.getName() will return the full path to the file // the client select this can cause problems // so the following is a workaround. try {/*from w ww .j a v a2 s .c om*/ String fileName = fi.getName(); if (fileName == null || fileName.trim().length() == 0) { return null; } fileName = FilenameUtils.getName(fileName); File fNew = File.createTempFile("opc", ".tmp"); fNew.deleteOnExit(); fi.write(fNew); File finalFileName = new File(fNew.getParent() + File.separator + fileName); if (fNew.renameTo(finalFileName)) { return finalFileName.getAbsolutePath(); } else { // unable to rename, copy the contents of the file instead PortletWarUpdaterUtil.copyFile(fNew, finalFileName, true, false); return finalFileName.getAbsolutePath(); } } catch (Exception e) { throw new FileUploadException(e.getMessage()); } }
From source file:com.emc.ecs.sync.test.TestObjectTarget.java
private void mkdirs(File path) { File parent = path.getParentFile(); if (parent == null) return;/*from ww w.ja va2s. c o m*/ mkdirs(parent); if (getObject(parent.getPath()) == null) { // add directory String parentParent = parent.getParent(); if (parentParent == null) parentParent = ""; addChild(parentParent, new TestSyncObject(this, parent.getPath(), parent.getPath(), null, new ArrayList<TestSyncObject>())); } }
From source file:ch.kostceco.bento.sipval.service.impl.ConfigurationServiceImpl.java
private XMLConfiguration getConfig() { if (this.config == null) { try {//w w w.j a v a2 s. com String path = "configuration/sipvalidator.conf.xml"; URL locationOfJar = SipValidator.class.getProtectionDomain().getCodeSource().getLocation(); String locationOfJarPath = locationOfJar.getPath(); if (locationOfJarPath.endsWith(".jar")) { File file = new File(locationOfJarPath); String fileParent = file.getParent(); path = fileParent + "/" + path; } config = new XMLConfiguration(path); } catch (ConfigurationException e) { System.out.print( "\r "); System.out.flush(); System.out.print("\r"); System.out.flush(); LOGGER.logInfo(getTextResourceService().getText(MESSAGE_CONFIGURATION_ERROR_1)); LOGGER.logInfo(getTextResourceService().getText(MESSAGE_CONFIGURATION_ERROR_2)); LOGGER.logInfo(getTextResourceService().getText(MESSAGE_CONFIGURATION_ERROR_3)); System.exit(1); } } return config; }
From source file:com.twosigma.beakerx.kernel.magic.command.MavenJarResolver.java
private void deletePomFolder(File finalPom) { if (finalPom != null) { File parentFile = new File(finalPom.getParent()); try {/*ww w . j ava 2 s. co m*/ FileUtils.deleteDirectory(parentFile); } catch (IOException e) { } } }
From source file:com.mattc.argus2.concurrent.ZipProcess.java
@Override public void run() { this.running.set(true); // Zip Archive Entry Input Stream InputStream zis = null;//from w ww .ja v a2 s .c om try { this.zip = new ZipFile(this.zipFile); ZipArchiveEntry entry; Enumeration<ZipArchiveEntry> entries; entries = this.zip.getEntriesInPhysicalOrder(); // While there are still Entries to process, iterate while (entries.hasMoreElements()) { entry = entries.nextElement(); // Create the Empty File to Extract to and its Parent Directory // Notify Console and ProgressLog that a File is Being Extracted final File destination = new File(this.destDir, entry.getName()); final File dirs = new File(destination.getParent()); dirs.mkdirs(); Console.info("EXT: " + Utility.relativizePath(destination, this.destDir.getParentFile())); /* * IMPORTANT * * Ensures that All Files have a Directory to go to. * * If a Folder is for some reason created as a File, this will detect * that. It will delete the file and re-create it as a Directory so * that installation can continue. * * If all else fails, print debug information and stop extracting. It * is unlikely the installed application will work without all files * being extracted. */ try { if (!destination.getParentFile().isDirectory()) { if (!destination.getParentFile().delete()) throw new IOException( "Failure to Delete Directory! - " + destination.getCanonicalPath()); destination.getParentFile().mkdirs(); } if (!destination.createNewFile() && !destination.exists()) throw new IOException("Failure to create Destination! - " + destination.getPath()); } catch (final IOException e) { final String errMsg = "Failure to Extract " + this.zipFile.getName(); final String errPath = "PATH = " + destination.getCanonicalPath(); final String errParent = "PARENT = " + destination.getParentFile().getCanonicalPath(); final String errIsDir = "PARENT_IS_DIRECTORY = " + destination.getParentFile().isDirectory(); // Standard IO Error Information e.printStackTrace(System.err); System.err.println(errMsg); System.err.println(errPath); System.err.println(errParent); System.err.println(errIsDir); // Log File Error Information (Possibly Standard IO if DEBUG // = True) Console.exception(e); Console.error(errMsg); Console.error(errPath); Console.error(errParent); Console.error(errIsDir); // Attempt to Delete the Partially Unzipped and // Non-functional install Directory if (!IOUtils.deleteDirectory(this.destDir)) { Console.error("Although extraction failed, the erroneous directory could not be removed!"); } else { Console.info("Erroneous Directory Deleted Successfully!"); } } // Establish a Stream to output data to the destination file zis = this.zip.getInputStream(entry); this.fos = new FileOutputStream(destination); // Read Zip Entry data into buffer, output buffer to // installation file for (int c = zis.read(this.buffer); c > 0; c = zis.read(this.buffer)) { this.fos.write(this.buffer, 0, c); } // Close Current Entry and Destination OutputStream zis.close(); this.fos.close(); } } catch (final ZipException e) { Console.exception(e); } catch (final IOException e) { Console.exception(e); } finally { // Ensure that All Streams Are Closed to prevent Memory // Leaks IOUtils.closeSilently(zis); IOUtils.closeSilently(this.fos); IOUtils.closeSilently(this.zip); this.running.set(false); } }