List of usage examples for java.io File getCanonicalPath
public String getCanonicalPath() throws IOException
From source file:hdfs.FileUtil.java
static String execCommand(File f, String... cmd) throws IOException { String[] args = new String[cmd.length + 1]; System.arraycopy(cmd, 0, args, 0, cmd.length); args[cmd.length] = f.getCanonicalPath(); String output = Shell.execCommand(args); return output; }
From source file:com.twitter.elephanttwin.util.HdfsUtils.java
/** * Concatenate the content of all HDFS files matching {@code hdfsGlob} into a * file {@code localFilename}.//from w w w.j av a2 s. co m * * @param hdfsNameNode The name of the Hadoop name node. * @param hdfsGlob Files matching this pattern will be fetched. * @param localFilename Name of local file to store concatenated content. * @return The newly created file. * @throws IOException when the file cannot be created/written. */ public static File getHdfsFiles(String hdfsNameNode, String hdfsGlob, String localFilename) throws IOException { Preconditions.checkNotNull(localFilename); Preconditions.checkNotNull(hdfsGlob); Preconditions.checkNotNull(hdfsNameNode); // init the FS connection and the local file. Configuration config = new Configuration(); config.set(CommonConfigurationKeys.FS_DEFAULT_NAME_KEY, hdfsNameNode); FileSystem dfs = FileSystem.get(config); File localFile = new File(localFilename); FileOutputStream localStream = new FileOutputStream(localFile); // get files that need downloading. FileStatus[] statuses = dfs.globStatus(new Path(hdfsGlob)); LOG.info("Pattern " + hdfsGlob + " matched " + statuses.length + " HDFS files, " + "fetching to " + localFile.getCanonicalPath() + "..."); // append each file. int copiedChars = 0; FSDataInputStream remoteStream = null; for (FileStatus status : statuses) { Path src = status.getPath(); try { remoteStream = dfs.open(src); copiedChars += IOUtils.copy(remoteStream, localStream); } catch (IOException e) { LOG.severe("Failed to open/copy " + src); } finally { IOUtils.closeQuietly(remoteStream); } } LOG.info("Fetch " + copiedChars + " bytes to local FS"); return localFile; }
From source file:com.aoyetech.fee.commons.utils.FileUtils.java
public static void copyFile(final File srcFile, final File destFile, final boolean preserveFileDate) throws IOException { if (srcFile == null) { throw new NullPointerException("Source must not be null"); }/* w w w . j a v a 2 s. c om*/ if (destFile == null) { throw new NullPointerException("Destination must not be null"); } if (srcFile.exists() == false) { throw new FileNotFoundException("Source '" + srcFile + "' does not exist"); } if (srcFile.isDirectory()) { throw new IOException("Source '" + srcFile + "' exists but is a directory"); } if (srcFile.getCanonicalPath().equals(destFile.getCanonicalPath())) { throw new IOException("Source '" + srcFile + "' and destination '" + destFile + "' are the same"); } if ((destFile.getParentFile() != null) && (destFile.getParentFile().exists() == false)) { if (destFile.getParentFile().mkdirs() == false) { throw new IOException("Destination '" + destFile + "' directory cannot be created"); } } if (destFile.exists() && (destFile.canWrite() == false)) { throw new IOException("Destination '" + destFile + "' exists but is read-only"); } doCopyFile(srcFile, destFile, preserveFileDate); }
From source file:com.adito.applications.server.FileReplacement.java
static File getTempFile(File near) throws IOException { String path = null;/*from w w w . jav a 2 s . c om*/ if (near != null) if (near.isFile()) path = near.getParent(); else if (near.isDirectory()) path = near.getPath(); Random wheel = new Random(); // seeded from the clock File tempFile = null; do { // generate random a number 10,000,000 .. 99,999,999 int unique = (wheel.nextInt() & Integer.MAX_VALUE) % 90000000 + 10000000; tempFile = new File(path, Integer.toString(unique) + ".tmp"); } while (tempFile.exists()); // We "finally" found a name not already used. Nearly always the first // time. // Quickly stake our claim to it by opening/closing it to create it. // In theory somebody could have grabbed it in that tiny window since // we checked if it exists, but that is highly unlikely. new FileOutputStream(tempFile).close(); // debugging peek at the name generated. if (false) { System.out.println(tempFile.getCanonicalPath()); } return tempFile; }
From source file:com.github.fritaly.dualcommander.Utils.java
public static void copyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException { if (srcFile == null) { throw new NullPointerException("Source must not be null"); }/*from ww w . ja va 2 s. c o m*/ if (destFile == null) { throw new NullPointerException("Destination must not be null"); } if (srcFile.exists() == false) { throw new FileNotFoundException("Source '" + srcFile + "' does not exist"); } if (srcFile.isDirectory()) { throw new IOException("Source '" + srcFile + "' exists but is a directory"); } if (srcFile.getCanonicalPath().equals(destFile.getCanonicalPath())) { throw new IOException("Source '" + srcFile + "' and destination '" + destFile + "' are the same"); } File parentFile = destFile.getParentFile(); if (parentFile != null) { if (!parentFile.mkdirs() && !parentFile.isDirectory()) { throw new IOException("Destination '" + parentFile + "' directory cannot be created"); } } if (destFile.exists() && destFile.canWrite() == false) { throw new IOException("Destination '" + destFile + "' exists but is read-only"); } doCopyFile(srcFile, destFile, preserveFileDate); }
From source file:gov.nih.nci.ncicb.tcga.dcc.common.util.FileUtil.java
/** * Expands a tar or tar.gz archive into the given directory * * @param expandDir the target directory * @param tarOrTarGzFilename the name of the archive to expand (must be tar or tar.gz) * @throws IOException//from ww w . ja va 2 s .c o m */ public static void explodeTarOrTarGz(final File expandDir, final String tarOrTarGzFilename) throws IOException { final File file = new File(tarOrTarGzFilename); final FileInputStream fStream = new FileInputStream(file); String filenameWithoutExtension; if (tarOrTarGzFilename.endsWith(ConstantValues.COMPRESSED_ARCHIVE_EXTENSION)) { filenameWithoutExtension = getFilenameWithoutExtension(tarOrTarGzFilename, ConstantValues.COMPRESSED_ARCHIVE_EXTENSION); } else { filenameWithoutExtension = getFilenameWithoutExtension(tarOrTarGzFilename, ConstantValues.UNCOMPRESSED_ARCHIVE_EXTENSION); } GZIPInputStream gzipStream = null; TarInputStream tin = null; TarEntry tarEntry; final StringBuffer errorMsg = new StringBuffer(); try { //noinspection IOResourceOpenedButNotSafelyClosed if (tarOrTarGzFilename.endsWith(ConstantValues.COMPRESSED_ARCHIVE_EXTENSION)) { gzipStream = new GZIPInputStream(fStream); tin = new TarInputStream(gzipStream); } else { tin = new TarInputStream(fStream); } while ((tarEntry = tin.getNextEntry()) != null) { String entryName = tarEntry.getName(); final File entryFile = new File(entryName); if (!tarEntry.isDirectory()) { if (!entryName.startsWith(filenameWithoutExtension)) { errorMsg.append( "Archive files should be contained inside a single directory with the same name as the archive.\n"); } // extract out just the filename of the entry if (entryFile.getCanonicalPath().lastIndexOf(File.separator) != -1) { entryName = entryFile.getCanonicalPath() .substring(entryFile.getCanonicalPath().lastIndexOf(File.separator)); } final File destPath = new File(expandDir, entryName); FileOutputStream fout = new FileOutputStream(destPath); try { tin.copyEntryContents(fout); } finally { fout.flush(); fout.close(); fout = null; } } else { if (!entryName.equals(filenameWithoutExtension) && !entryName.equals(filenameWithoutExtension + File.separator) && !entryName.equals(filenameWithoutExtension + "/")) { String t = filenameWithoutExtension + File.separator; errorMsg.append("Archive contains a non-standard directory '").append(entryName).append( "'. Archive files should be contained inside a single directory with the same name as the archive.\n"); } } } } finally { IOUtils.closeQuietly(tin); IOUtils.closeQuietly(gzipStream); IOUtils.closeQuietly(fStream); if (errorMsg.length() > 0) throw new IOException(errorMsg.toString()); } }
From source file:com.twinsoft.convertigo.engine.localbuild.BuildLocally.java
/*** * /*from ww w. j a v a 2 s . com*/ * @param paths * @param command * @return * @throws IOException */ private static String getFullPath(String paths, String command) throws IOException { for (String path : paths.split(Pattern.quote(File.pathSeparator))) { File candidate = new File(path, command); if (candidate.exists()) { return candidate.getCanonicalPath(); } } return null; }
From source file:com.googlecode.dex2jar.test.TestUtils.java
public static File dex(List<File> files, File distFile) throws Exception { String dxJar = "src/test/resources/dx.jar"; File dxFile = new File(dxJar); if (!dxFile.exists()) { throw new RuntimeException("dx.jar?"); }//w w w .j av a2 s. c o m URLClassLoader cl = new URLClassLoader(new URL[] { dxFile.toURI().toURL() }); Class<?> c = cl.loadClass("com.android.dx.command.Main"); Method m = c.getMethod("main", String[].class); if (distFile == null) { distFile = File.createTempFile("dex", ".dex"); } List<String> args = new ArrayList<String>(); args.addAll(Arrays.asList("--dex", "--no-strict", "--output=" + distFile.getCanonicalPath())); for (File f : files) { args.add(f.getCanonicalPath()); } m.invoke(null, new Object[] { args.toArray(new String[0]) }); return distFile; }
From source file:com.symbian.driver.core.ResourceLoader.java
/** * Loads TDv1 XML files.//ww w . j ava 2 s . c o m * * @param aXmlRoot The location of the XML root for TDv1. * @param aRootAddress The URI to the Execute to load. * @return The task corresponding to the URI specfied in the XML Root directory. * @throws ParseException If there was a problem with getting the configuration. * @throws FileNotFoundException IF the XML root directory is invalid. * @throws IOException If the URI or XML root directory is invalid. */ public static Task loadOldXml(File aXmlRoot, URI aRootAddress) throws ParseException, FileNotFoundException, IOException { LOGGER.info("Importing Old TestDriver v1 XML at: " + aXmlRoot.getAbsolutePath()); // Create Document Root DocumentRoot lDocumentRoot = DriverFactory.eINSTANCE.createDocumentRoot(); // Register the factory Driver lDriver = DriverFactory.eINSTANCE.createDriver(); // Import Old XML to new XML lDriver.setTask(new FrameworkGenerator(aXmlRoot).buildEmfModel()); lDocumentRoot.setDriver(lDriver); File lSaveFile = null; if (aXmlRoot.isDirectory()) { lSaveFile = new File(aXmlRoot.getCanonicalPath() + File.separator + aXmlRoot.getName() + ".driver"); } else if (aXmlRoot.isFile()) { lSaveFile = new File(aXmlRoot.toString().replaceAll("\\.xml", ".driver")); } DriverResourceImpl lResource = (DriverResourceImpl) iResourceSet .createResource(URI.createFileURI(lSaveFile.getAbsolutePath())); lResource.getContents().add(lDocumentRoot); Map lSaveOptions = new HashMap(); lSaveOptions.put(XMLResource.OPTION_ENCODING, "UTF-8"); lResource.save(new FileOutputStream(lSaveFile), lSaveOptions); LOGGER.info("Created TestDriver v2 XML file at: " + lSaveFile.getCanonicalPath()); if (aRootAddress != null) { String lTempFragement = aRootAddress.toString(); aRootAddress = URI.createFileURI(lSaveFile.getCanonicalPath()); aRootAddress = aRootAddress .appendFragment(lTempFragement.charAt(0) == '#' ? lTempFragement.substring(1) : lTempFragement); TDConfig.getInstance().setPreferenceURI(TDConfig.ENTRY_POINT_ADDRESS, aRootAddress); ExtendedDriverValidator.validator(lResource); return lResource.getTask(aRootAddress.fragment()); } return ((DocumentRoot) lResource.getContents().get(0)).getDriver().getTask(); }
From source file:com.nary.io.FileUtils.java
/** * resolveNativeLibPath/*from ww w . j a va2s. c o m*/ */ public static String resolveNativeLibPath(String nativeLibPath) throws IOException { if (new cfmlURI(nativeLibPath).isRealFile()) { File nativeLib = cfEngine.getResolvedFile(nativeLibPath); if (!nativeLib.exists()) { throw new IOException("Native library does not exist: " + nativeLibPath); } return nativeLib.getCanonicalPath(); } // copy native lib to temporary file, and return path to temp file InputStream is = cfEngine.thisServletContext.getResourceAsStream(nativeLibPath); if (is == null) { throw new IOException("Could not load native library: " + nativeLibPath); } File tempFile = File.createTempFile("LIB", (cfEngine.WINDOWS ? ".dll" : ".so"), cfEngine.thisPlatform.getFileIO().getTempDirectory()); OutputStream fos = cfEngine.thisPlatform.getFileIO().getFileOutputStream(tempFile); StreamUtil.copyTo(is, fos); return tempFile.getCanonicalPath(); }