Example usage for java.io File getParentFile

List of usage examples for java.io File getParentFile

Introduction

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

Prototype

public File getParentFile() 

Source Link

Document

Returns the abstract pathname of this abstract pathname's parent, or null if this pathname does not name a parent directory.

Usage

From source file:cognition.pipeline.Main.java

private static String getCurrentFolder() {
    CodeSource codeSource = Main.class.getProtectionDomain().getCodeSource();
    File jarFile;
    try {//from   w  w  w.j  a  va 2  s  .  c  o  m
        jarFile = new File(codeSource.getLocation().toURI().getPath());
    } catch (URISyntaxException e) {
        e.printStackTrace();
        return "";
    }
    return jarFile.getParentFile().getPath();
}

From source file:org.dkpro.similarity.experiments.sts2013baseline.util.Features2Arff.java

private static String toArffString(Collection<File> csvFiles, InputStream goldStandardInputStream)
        throws IOException {
    // Create the Arff header
    StringBuilder arff = new StringBuilder();
    arff.append("@relation temp-relation" + LF);
    arff.append(LF);/*from   www .j ava2  s.  c om*/

    // Init data object
    Map<Integer, List<Double>> data = new HashMap<Integer, List<Double>>();

    for (File file : csvFiles) {
        String feature = file.getParentFile().getName() + "/"
                + file.getName().substring(0, file.getName().length() - 4);
        // feature = feature.replaceAll(",", "");

        // Add the attribute to the Arff header
        arff.append("@attribute " + feature + " numeric" + LF);

        // Read data
        List<String> lines = FileUtils.readLines(file);

        if (lines.size() == 0) {
            System.err.println("Empty feature file for " + feature + ". Experiment will probably fail.");
        }

        for (int doc = 1; doc <= lines.size(); doc++) {
            String line = lines.get(doc - 1);

            if (line.length() > 0) // Ignore empty lines
            {
                double value = Double.parseDouble(line); // There's just the score on the line, nothing else.

                // Limit to [0;5] interval
                if (value > 5.0) {
                    value = 5.0;
                }
                if (value < 0.0) {
                    value = 0.0;
                }

                // Get doc object in data list
                List<Double> docObj;
                if (data.containsKey(doc)) {
                    docObj = data.get(doc);
                } else {
                    docObj = new ArrayList<Double>();
                }

                // Put data
                docObj.add(value);
                data.put(doc, docObj);
            }
        }
    }

    // Add gold attribute to attribute list in header
    // We also need to do this for unlabeled data
    arff.append("@attribute gold real" + LF);

    // Add gold similarity score 
    List<String> lines = new ArrayList<String>();
    if (goldStandardInputStream != null) {
        String line;
        BufferedReader br = new BufferedReader(new InputStreamReader(goldStandardInputStream, "UTF-8"));
        while ((line = br.readLine()) != null) {
            lines.add(line);
        }
    } else {
        for (int i = 0; i < FileUtils.readLines(csvFiles.iterator().next()).size(); i++) {
            lines.add("0.0");
        }
    }

    for (int doc = 1; doc <= lines.size(); doc++) {
        //         System.out.println(lines.get(doc - 1).length());
        //         System.out.println(lines.get(doc - 1));
        if (lines.get(doc - 1).length() == 0) {
            System.out.println("here2");
            break;
        }

        double value = Double.parseDouble(lines.get(doc - 1));

        //         System.out.println(doc);

        List<Double> docObj = data.get(doc);
        docObj.add(value);
        data.put(doc, docObj);
    }

    // Finalize header
    arff.append(LF);
    arff.append("@data" + LF);

    // Write data
    for (int i = 1; i <= data.keySet().size(); i++) {
        String dataItem = StringUtils.join(data.get(i), ",");

        arff.append(dataItem + LF);
    }

    return arff.toString();
}

From source file:de.shadowhunt.maven.plugins.packageinfo.PackageInfoPlugin.java

static void createNecessaryDirectories(final File file) throws IOException {
    final File parent = file.getParentFile();
    if (parent.isDirectory()) {
        return;//w  ww. j av  a 2  s  . com
    }

    final boolean createdDirs = parent.mkdirs();
    if (!createdDirs) {
        throw new IOException("could not create all necessary but nonexistent parent directories for " + file);
    }
}

From source file:com.linkedin.databus2.test.TestUtil.java

public static void setupLogging(boolean logToConsole, String fileLogPath, Level logLevel) {
    PatternLayout defaultLayout = new PatternLayout("%d{ISO8601} [%t] (%p) {%c{1}} %m%n");
    Logger.getRootLogger().removeAllAppenders();

    if (logToConsole) {
        ConsoleAppender defaultAppender = new ConsoleAppender(defaultLayout);
        Logger.getRootLogger().addAppender(defaultAppender);
    }/*from w  ww .ja  va 2  s . c om*/
    if (null != fileLogPath && 0 < fileLogPath.length()) {
        File logFile = new File(fileLogPath);
        if (null != logFile.getParentFile() && !logFile.getParentFile().exists()) {
            if (!logFile.getParentFile().mkdirs()) {
                Logger.getRootLogger().error("unable to create parent directory for log file: " + logFile);
            }
        }
        FileAppender fileAppender = null;
        try {
            fileAppender = new FileAppender(defaultLayout, fileLogPath);
        } catch (IOException io) {
            Logger.getRootLogger().error(io);
        }
        if (null != fileAppender)
            Logger.getRootLogger().addAppender(fileAppender);
    }

    Logger.getRootLogger().setLevel(logLevel);
}

From source file:com.bluexml.tools.miscellaneous.ConvertFileContentIntoJSString.java

protected static void parseFile(File input, String fileNameout) {

    if (input.isDirectory()) {
        File[] listFiles = input.listFiles();
        for (File file2 : listFiles) {
            parseFile(file2, fileNameout);
        }/*  ww w  . ja v a 2s . co  m*/
    } else {
        File output = new File(input.getParentFile(), input.getName() + "-" + fileNameout);
        writeFiles(input, output);
    }

}

From source file:fm.last.commons.io.LastFileUtils.java

/**
 * Writes the contents of the passed input stream to the passed file and closes the InputStream when done.
 * /*from   w  ww . jav  a2 s . c o  m*/
 * @param inputStream Input stream to write to file.
 * @param file The file to write to.
 * @param makeDirs Whether to create the files parent dir(s) or not.
 * @throws IOException If an error occurs writing the InputStream to the file.
 */
public static void writeToFile(InputStream inputStream, File file, boolean makeDirs) throws IOException {
    if (makeDirs && file.getParentFile() != null && !file.getParentFile().exists()) {
        if (!file.getParentFile().mkdirs()) {
            throw new IOException("Error creating '" + file.getParentFile().getAbsolutePath() + "'");
        }
    }
    BufferedOutputStream outputStream = null;
    try {
        outputStream = new BufferedOutputStream(new FileOutputStream(file));
        byte[] inBuffer = new byte[32 * 1024];
        int bytesRead = 0;
        while ((bytesRead = inputStream.read(inBuffer)) != -1) {
            outputStream.write(inBuffer, 0, bytesRead);
        }
    } finally {
        IOUtils.closeQuietly(inputStream);
        IOUtils.closeQuietly(outputStream);
    }
}

From source file:Main.java

/**
 * Stores document into specified file/*w  ww  .ja v a 2  s .co m*/
 * @param document XML document
 * @param transformer Transformer object to store document
 * @param file File where this method stores XML document
 * @param encoding Encoding in with document should be stored
 * @throws IOException If cannot store because of IO error
 * @throws TransformerException If cannot store because transformer error
 */
public static void storeDocument(Document document, Transformer transformer, File file, String encoding)
        throws IOException, TransformerException {
    FileOutputStream stream = null;
    OutputStreamWriter writer = null;
    try {
        if (!file.exists()) {
            file.getParentFile().mkdirs();
            file.createNewFile();
        }
        stream = new FileOutputStream(file);
        writer = new OutputStreamWriter(stream, encoding);
        storeDocument(document, transformer, writer);
    } finally {
        if (stream != null) {
            stream.close();
        }
    }
}

From source file:eu.stratosphere.library.clustering.DistributedOnePassKMeans.Util.java

private static File createAndRegisterTempFile(String fileName) throws IOException {
    File baseDir = new File(System.getProperty("java.io.tmpdir"));
    File f = new File(baseDir, fileName);

    if (f.exists()) {
        deleteRecursively(f);//from   ww  w. j av a  2s  .  c  om
    }

    File parentToDelete = f;
    while (true) {
        File parent = parentToDelete.getParentFile();
        if (parent == null) {
            throw new IOException("Missed temp dir while traversing parents of a temp file.");
        }
        if (parent.equals(baseDir)) {
            break;
        }
        parentToDelete = parent;
    }

    Files.createParentDirs(f);
    tempFiles.add(parentToDelete);
    return f;
}

From source file:Main.java

/**
 * The formPath is relative to the framework directory and is passed into
 * the WebKit to specify the form to display.
 *
 * @param appName//  w w  w  .  jav  a 2  s . com
 * @param formDefFile
 * @return
 */
public static String getRelativeFormPath(String appName, File formDefFile) {

    // compute FORM_PATH...
    // we need to do this relative to the AppFolder, as the
    // common index.html is under the ./system folder.

    String relativePath = asRelativePath(appName, formDefFile.getParentFile());
    // adjust for relative path from ./system...
    relativePath = ".." + File.separator + relativePath + File.separator;
    return relativePath;
}

From source file:es.emergya.tools.JarSearcher.java

public static File getJarDirectory(Class<?> clazz) {
    File jar = JarSearcher.getJar(clazz);
    if (jar != null) {
        // .jar found
        if (JarSearcher.LOG.isTraceEnabled())
            JarSearcher.LOG.trace("getJarDirectory: " + jar.getParentFile().getAbsolutePath());
        return jar.getParentFile();
    }/*  w w w  .  j a v  a2  s .c  o  m*/
    // ... not found
    return null;
}