Example usage for java.io File toString

List of usage examples for java.io File toString

Introduction

In this page you can find the example usage for java.io File toString.

Prototype

public String toString() 

Source Link

Document

Returns the pathname string of this abstract pathname.

Usage

From source file:com.microsoft.applicationinsights.internal.perfcounter.JniPCConnector.java

/**
 * The method will try to extract the dll for the Windows performance counters to a local
 * folder and then will try to load it. The method will do all that by doing the following things:
 * 1. Find the OS type (64/32) currently supports only 64 bit.
 * 2. Will find the path to extract to, which is %temp%/AI_BASE_FOLDER/AI_NATIVE_FOLDER/sdk_version_number
 * 3. Find out whether or not the file already exists in that directory
 * 4. If the dll is not there, the method will extract it from the jar to that directory
 * 5. The method will call System.load to load the dll and by doing so we are ready to use it
 * @return true on success, otherwise false
 * @throws IOException If there are errors in opening/writing/reading/closing etc.
 *         Note that the method might throw RuntimeExceptions due to critical issues
 */// w ww .  j  a  va 2 s .co  m
private static void loadNativeLibrary() throws IOException {
    String model = System.getProperty("sun.arch.data.model");
    String libraryToLoad = BITS_MODEL_64.equals(model) ? NATIVE_LIBRARY_64 : NATIVE_LIBRARY_32;

    File dllPath = buildDllLocalPath();

    File dllOnDisk = new File(dllPath, libraryToLoad);

    if (!dllOnDisk.exists()) {
        extractToLocalFolder(dllOnDisk, libraryToLoad);
    }

    System.load(dllOnDisk.toString());

    initNativeCode();

    InternalLogger.INSTANCE.trace("Successfully loaded library '%s'", libraryToLoad);
}

From source file:ch.vorburger.mariadb4j.Util.java

public static void forceExecutable(File executableFile) throws IOException {
    if (executableFile.exists() && !executableFile.canExecute()) {
        boolean succeeded = executableFile.setExecutable(true);
        if (succeeded) {
            logger.info("chmod +x " + executableFile.toString() + " (using java.io.File.setExecutable)");
        } else {//w w  w.  java 2  s  .c o  m
            throw new IOException("Failed to do chmod +x " + executableFile.toString()
                    + " using java.io.File.setExecutable, which will be a problem on *NIX...");
        }
    }
}

From source file:gov.usgs.anss.query.MultiplexedMSOutputer.java

/**
 * This does the hard work of sorting - called as a shutdown hook.
 * TODO: consider recursion./*  w w  w . jav a  2s.c  o  m*/
 * @param outputName name for the output file.
 * @param files list of MiniSEED files to multiplex.
 * @param cleanup flag indicating whether to cleanup after ourselves or not.
 * @throws IOException
 */
public static void multiplexFiles(String outputName, List<File> files, boolean cleanup, boolean allowEmpty)
        throws IOException {
    ArrayList<File> cleanupFiles = new ArrayList<File>(files);
    ArrayList<File> moreFiles = new ArrayList<File>();

    File outputFile = new File(outputName);
    File tempOutputFile = new File(outputName + ".tmp");

    do {
        // This checks if we're in a subsequent (i.e. not the first) iteration and if there are any more files to process...?
        if (!moreFiles.isEmpty()) {
            logger.info("more files left to multiplex...");
            FileUtils.deleteQuietly(tempOutputFile);
            FileUtils.moveFile(outputFile, tempOutputFile);

            cleanupFiles.add(tempOutputFile);
            moreFiles.add(tempOutputFile);
            files = moreFiles;
            moreFiles = new ArrayList<File>();
        }

        logger.log(Level.FINE, "Multiplexing blocks from {0} temp files to {1}",
                new Object[] { files.size(), outputName });
        BufferedOutputStream out = new BufferedOutputStream(FileUtils.openOutputStream(outputFile));

        // The hard part, sorting the temp files...
        TreeMap<MiniSeed, FileInputStream> blks = new TreeMap<MiniSeed, FileInputStream>(
                new MiniSeedTimeOnlyComparator());
        // Prime the TreeMap
        logger.log(Level.FINEST, "Priming the TreeMap with files: {0}", files);
        for (File file : files) {
            logger.log(Level.INFO, "Reading first block from {0}", file.toString());
            try {
                FileInputStream fs = FileUtils.openInputStream(file);
                MiniSeed ms = getNextValidMiniSeed(fs, allowEmpty);
                if (ms != null) {
                    blks.put(ms, fs);
                } else {
                    logger.log(Level.WARNING, "Failed to read valid MiniSEED block from {0}", file.toString());
                }
            } catch (IOException ex) {
                // Catch "Too many open files" i.e. hitting ulimit, throw anything else.
                if (ex.getMessage().contains("Too many open files")) {
                    logger.log(Level.INFO, "Too many open files - {0} deferred.", file.toString());
                    moreFiles.add(file);
                } else
                    throw ex;
            }
        }

        while (!blks.isEmpty()) {
            MiniSeed next = blks.firstKey();
            out.write(next.getBuf(), 0, next.getBlockSize());

            FileInputStream fs = blks.remove(next);
            next = getNextValidMiniSeed(fs, allowEmpty);
            if (next != null) {
                blks.put(next, fs);
            } else {
                fs.close();
            }
        }

        out.close();
    } while (!moreFiles.isEmpty());

    if (cleanup) {
        logger.log(Level.INFO, "Cleaning up...");
        for (File file : cleanupFiles) {
            FileUtils.deleteQuietly(file);
        }
    }
}

From source file:cn.fql.utility.FileUtility.java

/**
 * do delete all files under specified file's dir
 *
 * @param root specified file root//from   w  w w  .  j  av a2s  .  c om
 */
public static void deleteDirs(File root) throws Exception {
    System.setSecurityManager(SECURITYMANAGER);
    if (root.isDirectory()) {
        List allFiles = deleteAll(root);
        if (allFiles != null) {
            for (int i = allFiles.size() - 1; i >= 0; i--) {
                java.io.File f = (java.io.File) allFiles.remove(i);
                String fileName = f.toString();
                if (!f.delete()) {
                    throw new Exception("Exception: delete file " + fileName + " false!");
                }
            }
        }
    }
    System.setSecurityManager(SYSSECURITYMANAGER);
}

From source file:net.metanotion.sqlc.SqlcPhp.java

public static String mkPath(final String outPath, final String[] packageName, final String name) {
    final File f = getFilePath(outPath, packageName);
    f.mkdirs();/*from   w ww  . j av  a  2s . com*/
    return (f.toString() + File.separator + name + ".php");
}

From source file:Fasta.java

public static String Download(String name) {
    String path;/*from  w  w w . jav  a2s . c o m*/
    if (name.equals("BrownCNA")) {
        path = "http://phagesdb.org/media/fastas/Browncna.fasta";
    } else if (name.equals("GUmbie")) {
        path = "http://phagesdb.org/media/fastas/Gumbie.fasta";
    } else if (name.equals("Numberten")) {
        path = "http://phagesdb.org/media/fastas/NumberTen.fasta";
    } else if (name.equals("Seabiscuit")) {
        path = "http://phagesdb.org/media/fastas/SeaBiscuit.fasta";
    } else if (name.equals("Caliburn")) {
        path = "http://phagesdb.org/media/fastas/Excalibur.fasta";
    } else if (name.equals("Godpower")) {
        path = "http://phagesdb.org/media/fastas/GodPower.fasta";
    } else if (name.equals("Romney")) {
        path = "http://phagesdb.org/media/fastas/Romney2012.fasta";
    } else {
        path = "http://phagesdb.org/media/fastas/" + name + ".fasta";
    }
    String base = new File("").getAbsolutePath();
    name = base + "/Fastas/" + name + ".fasta";
    File file = new File(name);
    try {
        if (!file.exists()) {
            URL netPath = new URL(path);
            FileUtils.copyURLToFile(netPath, file);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return file.toString();
}

From source file:info.dolezel.fatrat.plugins.helpers.NativeHelpers.java

private static Set<Class> findClasses(File directory, String packageName, Class annotation)
        throws ClassNotFoundException, IOException {
    Set<Class> classes = new HashSet<Class>();
    if (!directory.exists()) {

        String fullPath = directory.toString();
        String jarPath = fullPath.replaceFirst("[.]jar[!].*", ".jar").replaceFirst("file:", "");
        JarFile jarFile = new JarFile(jarPath);
        Enumeration<JarEntry> entries = jarFile.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = entries.nextElement();
            String entryName = entry.getName();
            if (entryName.endsWith(".class") && !entryName.contains("$")) {
                String className = entryName.replace('/', '.').replace('\\', '.').replace(".class", "");
                Class cls = loader.loadClass(className);

                if (annotation == null || cls.isAnnotationPresent(annotation))
                    classes.add(cls);/*from w w w .  j  av a  2s . c  o  m*/
            }
        }
    } else {
        File[] files = directory.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                assert !file.getName().contains(".");
                classes.addAll(findClasses(file, packageName + "." + file.getName(), annotation));
            } else if (file.getName().endsWith(".class")) {
                Class cls = loader.loadClass(
                        packageName + '.' + file.getName().substring(0, file.getName().length() - 6));

                if (annotation == null || cls.isAnnotationPresent(annotation))
                    classes.add(cls);
            }
        }
    }
    return classes;
}

From source file:FileHelper.java

/**
 * Move a file from one location to another.  An attempt is made to rename
 * the file and if that fails, the file is copied and the old file deleted.
 *
 * @param from file which should be moved.
 * @param to desired destination of the file.
 * @param overwrite If false, an exception will be thrown rather than overwrite a file.
 * @throws IOException if an error occurs.
 *
 * @since ostermillerutils 1.00.00// w w w. ja v a 2  s  .c om
 */
public static void move(File from, File to, boolean overwrite) throws IOException {
    if (to.exists()) {
        if (overwrite) {
            if (!to.delete()) {
                throw new IOException(MessageFormat.format(labels.getString("deleteerror"),
                        (Object[]) new String[] { to.toString() }));
            }
        } else {
            throw new IOException(MessageFormat.format(labels.getString("alreadyexistserror"),
                    (Object[]) new String[] { to.toString() }));
        }
    }

    if (from.renameTo(to))
        return;

    InputStream in = null;
    OutputStream out = null;
    try {
        in = new FileInputStream(from);
        out = new FileOutputStream(to);
        copy(in, out);
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;
        if (!from.delete()) {
            throw new IOException(MessageFormat.format(labels.getString("deleteoriginalerror"),
                    (Object[]) new String[] { from.toString(), to.toString() }));
        }
    } finally {
        if (in != null) {
            in.close();
            in = null;
        }
        if (out != null) {
            out.flush();
            out.close();
            out = null;
        }
    }
}

From source file:org.hyperic.hq.hqapi1.tools.Shell.java

static private Properties getClientProperties(String file) {
    Properties props = new Properties();

    File clientProperties = null;

    if (file != null) {
        clientProperties = new File(file);
        if (!clientProperties.exists()) {
            System.err.println("Error: " + clientProperties.toString() + " does not exist");
            System.exit(-1);//from w  w  w  .j av  a 2s.com
        }
    } else {
        InputStream is = Shell.class.getResourceAsStream("/client.properties");
        try {
            if (is != null) {
                props.load(is);
            }
        } catch (IOException e) {
            // System.err..etc..
            System.exit(-1);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException ignore) {
                }
            }
        }

        if (is == null) {
            // Default to ~/.hq/client.properties
            String home = System.getProperty("user.home");
            File hq = new File(home, ".hq");
            clientProperties = new File(hq, "client.properties");
        }
    }

    if (clientProperties != null && clientProperties.exists()) {
        FileInputStream fis = null;
        props = new Properties();
        try {
            fis = new FileInputStream(clientProperties);
            props.load(fis);
        } catch (IOException e) {
            return props;
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException ioe) {
                // Ignore
            }
        }
    }

    // Trim property values
    for (Enumeration e = props.propertyNames(); e.hasMoreElements();) {
        String prop = (String) e.nextElement();
        props.setProperty(prop, props.getProperty(prop).trim());
    }

    return props;
}

From source file:com.symbian.driver.core.processors.EmulatorPostProcessor.java

/**
 * @param lNewFile//  www  . ja v  a2  s  .  co m
 */
public static final void restoreFile(File lNewFile) {
    LOGGER.info("Restoring old file: " + lNewFile);
    if (lNewFile.isFile()) {
        try {
            if (lNewFile.delete() && !new File(lNewFile.getCanonicalPath() + BACKUP).renameTo(lNewFile)) {
                LOGGER.log(Level.SEVERE, "Could not restore file: " + lNewFile.toString());
            }
        } catch (IOException lIOException) {
            LOGGER.log(Level.SEVERE, "Could not restore file: " + lNewFile.toString(), lIOException);
        }
    } else {
        LOGGER.log(Level.WARNING, "Could not find file to restore: " + lNewFile.toString());
    }
}