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:dk.netarkivet.harvester.harvesting.metadata.MetadataFileWriterArc.java
/** * Create a <code>MetadataFileWriter</code> for ARC output. * * @param metadataARCFile The metadata ARC <code>File</code> * @return <code>MetadataFileWriter</code> for writing metadata files in ARC *//*from www . j a v a 2 s. c o m*/ public static MetadataFileWriter createWriter(File metadataARCFile) { MetadataFileWriterArc mtfw = new MetadataFileWriterArc(); ArcFileNaming naming = new ArcFileNamingSingleFile(metadataARCFile); ArcFileWriterConfig config = new ArcFileWriterConfig(metadataARCFile.getParentFile(), false, Long.MAX_VALUE, true); mtfw.writer = ArcFileWriter.getArcWriterInstance(naming, config); mtfw.open(); return mtfw; }
From source file:org.apache.hadoop.gateway.AmbariServiceDefinitionTest.java
public static void setupGateway() throws Exception { File targetDir = new File(System.getProperty("user.dir"), "target"); File gatewayDir = new File(targetDir, "gateway-home-" + UUID.randomUUID()); gatewayDir.mkdirs();/*from ww w .j av a2 s. c o m*/ config = new GatewayTestConfig(); config.setGatewayHomeDir(gatewayDir.getAbsolutePath()); URL svcsFileUrl = TestUtils.getResourceUrl(DAT, "test-svcs/readme.txt"); File svcsFile = new File(svcsFileUrl.getFile()); File svcsDir = svcsFile.getParentFile(); config.setGatewayServicesDir(svcsDir.getAbsolutePath()); String pathToStacksSource = "gateway-service-definitions/src/main/resources/services"; File stacksSourceDir = new File(targetDir.getParent(), pathToStacksSource); if (!stacksSourceDir.exists()) { stacksSourceDir = new File(targetDir.getParentFile().getParent(), pathToStacksSource); } if (stacksSourceDir.exists()) { FileUtils.copyDirectoryToDirectory(stacksSourceDir, svcsDir); } File topoDir = new File(config.getGatewayTopologyDir()); topoDir.mkdirs(); File deployDir = new File(config.getGatewayDeploymentDir()); deployDir.mkdirs(); setupMockServers(); startGatewayServer(); }
From source file:com.adguard.compiler.FileUtil.java
private static void processHtmlFile(File file, Browser browser) throws Exception { Configuration templateConfiguration = new Configuration(); FileTemplateLoader fileTemplateLoader = new FileTemplateLoader(file.getParentFile()); templateConfiguration.setTemplateLoader(fileTemplateLoader); String result;/*from w w w . j a v a 2 s . c om*/ Writer out = null; try { out = new StringWriter(); Map<String, Object> params = new HashMap<String, Object>(); params.put("browser", browser); Template template = templateConfiguration.getTemplate(file.getName(), "utf-8"); template.process(params, out); result = out.toString(); } finally { IOUtils.closeQuietly(out); } FileUtils.writeStringToFile(file, result); }
From source file:com.attilax.zip.FileUtil.java
/** /*from ww w . j av a 2 s . c o m*/ * ?? * * @param fileName * ?? * @param isDirectory ? * @return * @author yayagepei * @date 2008-8-27 */ public static File buildFile(String fileName, boolean isDirectory) { File target = new File(fileName); if (isDirectory) { target.mkdirs(); } else { if (!target.getParentFile().exists()) { target.getParentFile().mkdirs(); target = new File(target.getAbsolutePath()); } } return target; }
From source file:com.cedarsoft.io.LinkUtils.java
/** * Deletes the symbolic link/* w ww .j a v a 2s. c om*/ * * @param linkFile the link file * @throws IOException if any. */ public static void deleteSymbolicLink(@Nonnull File linkFile) throws IOException { if (!linkFile.exists()) { throw new FileNotFoundException("No such symlink: " + linkFile); } // find the resource of the existing link: File canonicalFile = linkFile.getCanonicalFile(); // rename the resource, thus breaking the link: File temp = createTempFile("symlink", ".tmp", canonicalFile.getParentFile()); try { try { FileUtils.moveFile(canonicalFile, temp); } catch (IOException e) { throw new IOException("Couldn't rename resource when attempting to delete " + linkFile); } // delete the (now) broken link: if (!linkFile.delete()) { throw new IOException("Couldn't delete symlink: " + linkFile + " (was it a real file? is this not a UNIX system?)"); } } finally { // return the resource to its original name: try { FileUtils.moveFile(temp, canonicalFile); } catch (IOException e) { throw new IOException("Couldn't return resource " + temp + " to its original name: " + canonicalFile.getAbsolutePath() + "\n THE RESOURCE'S NAME ON DISK HAS " + "BEEN CHANGED BY THIS ERROR!\n"); } } }
From source file:eu.irreality.age.SwingAetheriaGameLoaderInterface.java
public static void redirectStandardError(String file) { File f = new File(file); if (!f.exists()) { if (!f.getParentFile().exists()) { if (!f.getParentFile().mkdirs()) { System.err.println( "Could not redirect standard error to " + file + ": unable to create directories."); return; }// ww w .ja v a 2s . c o m } } //{f.getParentFile().exists() try { System.setErr(new PrintStream(new FileOutputStream(f, true))); System.err.println("[" + new Date() + "]"); } catch (FileNotFoundException e) { System.err.println("Could not redirect standard error to " + file + ":"); e.printStackTrace(); } }
From source file:org.jamwiki.utils.ResourceUtil.java
/** * Attempt to get the class loader root directory. This method works * by searching for a file that MUST exist in the class loader root * and then returning its parent directory. * * @return Returns a file indicating the directory of the class loader. * @throws IOException Thrown if the class loader can not be found, * which may occur if this class is deployed without the jamwiki-war package. */// w w w .j av a 2s .c o m public static File getClassLoaderRoot() throws IOException { // The file hard-coded here MUST exist in the class loader directory. File file = ResourceUtil.getClassLoaderFile("sql/sql.ansi.properties"); if (!file.exists()) { throw new IOException("Unable to find class loader root"); } return file.getParentFile().getParentFile(); }
From source file:JarUtil.java
/** * Extracts the given jar-file to the specified directory. The target * directory will be cleaned before the jar-file will be extracted. * //from w ww . ja v a 2 s. com * @param jarFile * The jar file which should be unpacked * @param targetDir * The directory to which the jar-content should be extracted. * @throws FileNotFoundException * when the jarFile does not exist * @throws IOException * when a file could not be written or the jar-file could not * read. */ public static void unjar(File jarFile, File targetDir) throws FileNotFoundException, IOException { // clear target directory: if (targetDir.exists()) { targetDir.delete(); } // create new target directory: targetDir.mkdirs(); // read jar-file: String targetPath = targetDir.getAbsolutePath() + File.separatorChar; byte[] buffer = new byte[1024 * 1024]; JarFile input = new JarFile(jarFile, false, ZipFile.OPEN_READ); Enumeration<JarEntry> enumeration = input.entries(); for (; enumeration.hasMoreElements();) { JarEntry entry = enumeration.nextElement(); if (!entry.isDirectory()) { // do not copy anything from the package cache: if (entry.getName().indexOf("package cache") == -1) { String path = targetPath + entry.getName(); File file = new File(path); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } FileOutputStream out = new FileOutputStream(file); InputStream in = input.getInputStream(entry); int read; while ((read = in.read(buffer)) != -1) { out.write(buffer, 0, read); } in.close(); out.close(); } } } }
From source file:JarUtil.java
/** * Extracts the given resource from a jar-file to the specified directory. * // w w w . j ava2 s . c o m * @param jarFile * The jar file which should be unpacked * @param resource * The name of a resource in the jar * @param targetDir * The directory to which the jar-content should be extracted. * @throws FileNotFoundException * when the jarFile does not exist * @throws IOException * when a file could not be written or the jar-file could not * read. */ public static void unjar(File jarFile, String resource, File targetDir) throws FileNotFoundException, IOException { // clear target directory: if (targetDir.exists()) { targetDir.delete(); } // create new target directory: targetDir.mkdirs(); // read jar-file: String targetPath = targetDir.getAbsolutePath() + File.separatorChar; byte[] buffer = new byte[1024 * 1024]; JarFile input = new JarFile(jarFile, false, ZipFile.OPEN_READ); Enumeration<JarEntry> enumeration = input.entries(); for (; enumeration.hasMoreElements();) { JarEntry entry = enumeration.nextElement(); if (!entry.isDirectory()) { // do not copy anything from the package cache: if (entry.getName().equals(resource)) { String path = targetPath + entry.getName(); File file = new File(path); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } FileOutputStream out = new FileOutputStream(file); InputStream in = input.getInputStream(entry); int read; while ((read = in.read(buffer)) != -1) { out.write(buffer, 0, read); } in.close(); out.close(); } } } }
From source file:eu.delving.metadata.Hasher.java
public static File ensureFileHashed(File file) throws IOException { if (file.getName().contains(SEPARATOR)) { return file; } else {/*ww w.j a v a2s .c om*/ Hasher hasher = new Hasher(); hasher.update(file); File hashedFile = new File(file.getParentFile(), hasher.prefixFileName(file.getName())); if (hashedFile.exists()) FileUtils.deleteQuietly(hashedFile); FileUtils.moveFile(file, hashedFile); return hashedFile; } }