Example usage for java.io File mkdirs

List of usage examples for java.io File mkdirs

Introduction

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

Prototype

public boolean mkdirs() 

Source Link

Document

Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories.

Usage

From source file:de.tudarmstadt.ukp.experiments.argumentation.convincingness.sampling.Step2ArgumentPairsSampling.java

public static void main(String[] args) throws Exception {
    String inputDir = args[0];//from  w  ww  .  j a va2 s . c om

    // /tmp
    File outputDir = new File(args[1]);
    if (!outputDir.exists()) {
        outputDir.mkdirs();
    }

    // pseudo-random
    final Random random = new Random(1);

    int totalPairsCount = 0;

    // read all debates
    for (File file : IOHelper.listXmlFiles(new File(inputDir))) {
        Debate debate = DebateSerializer.deserializeFromXML(FileUtils.readFileToString(file, "utf-8"));

        // get two stances
        SortedSet<String> originalStances = debate.getStances();

        // cleaning: some debate has three or more stances (data are inconsistent)
        // remove those with only one argument
        SortedSet<String> stances = new TreeSet<>();
        for (String stance : originalStances) {
            if (debate.getArgumentsForStance(stance).size() > 1) {
                stances.add(stance);
            }
        }

        if (stances.size() != 2) {
            throw new IllegalStateException(
                    "2 stances per debate expected, was " + stances.size() + ", " + stances);
        }

        // for each stance, get pseudo-random N arguments
        for (String stance : stances) {
            List<Argument> argumentsForStance = debate.getArgumentsForStance(stance);

            // shuffle
            Collections.shuffle(argumentsForStance, random);

            // and get max first N arguments
            List<Argument> selectedArguments = argumentsForStance.subList(0,
                    argumentsForStance.size() < MAX_SELECTED_ARGUMENTS_PRO_SIDE ? argumentsForStance.size()
                            : MAX_SELECTED_ARGUMENTS_PRO_SIDE);

            List<ArgumentPair> argumentPairs = new ArrayList<>();

            // now create pairs
            for (int i = 0; i < selectedArguments.size(); i++) {
                for (int j = (i + 1); j < selectedArguments.size(); j++) {
                    Argument arg1 = selectedArguments.get(i);
                    Argument arg2 = selectedArguments.get(j);

                    ArgumentPair argumentPair = new ArgumentPair();
                    argumentPair.setDebateMetaData(debate.getDebateMetaData());

                    // assign arg1 and arg2 pseudo-randomly
                    // (not to have the same argument as arg1 all the time)
                    if (random.nextBoolean()) {
                        argumentPair.setArg1(arg1);
                        argumentPair.setArg2(arg2);
                    } else {
                        argumentPair.setArg1(arg2);
                        argumentPair.setArg2(arg1);
                    }

                    // set unique id
                    argumentPair.setId(argumentPair.getArg1().getId() + "_" + argumentPair.getArg2().getId());

                    argumentPairs.add(argumentPair);
                }
            }

            String fileName = IOHelper.createFileName(debate.getDebateMetaData(), stance);

            File outputFile = new File(outputDir, fileName);

            // and save all sampled pairs into a XML file
            XStreamTools.toXML(argumentPairs, outputFile);

            System.out.println("Saved " + argumentPairs.size() + " pairs to " + outputFile);

            totalPairsCount += argumentPairs.size();
        }

    }

    System.out.println("Total pairs generated: " + totalPairsCount);
}

From source file:at.tuwien.ifs.feature.evaluation.SimilarityRetrievalWriter.java

public static void main(String[] args) throws SOMToolboxException, IOException {
    // register and parse all options
    JSAPResult config = OptionFactory.parseResults(args, OPTIONS);
    File inputVectorFile = config.getFile("inputVectorFile");
    String outputDirStr = AbstractOptionFactory.getFilePath(config, "outputDirectory");
    File outputDirBase = new File(outputDirStr);
    outputDirBase.mkdirs();
    String metricName = config.getString("metric");
    DistanceMetric metric = AbstractMetric.instantiateNice(metricName);

    int neighbours = config.getInt("numberNeighbours");
    int startIndex = config.getInt("startIndex");
    int numberItems = config.getInt("numberItems", -1);

    try {/*  ww w . j  a va 2  s .co m*/
        SOMLibSparseInputData data = new SOMLibSparseInputData(inputVectorFile.getAbsolutePath());
        int endIndex = data.numVectors();
        if (numberItems != -1) {
            if (startIndex + numberItems > endIndex) {
                System.out.println("Specified number of items (" + numberItems + ") exceeds maximum ("
                        + data.numVectors() + "), limiting to " + (endIndex - startIndex) + ".");
            } else {
                endIndex = startIndex + numberItems;
            }
        }
        StdErrProgressWriter progress = new StdErrProgressWriter(endIndex - startIndex, "processing vector ");
        // SortedSet<InputDistance> distances;
        for (int inputDatumIndex = startIndex; inputDatumIndex < endIndex; inputDatumIndex++) {

            InputDatum inputDatum = data.getInputDatum(inputDatumIndex);
            String inputLabel = inputDatum.getLabel();
            if (inputDatumIndex == -1) {
                throw new IllegalArgumentException(
                        "Input with label '" + inputLabel + "' not found in vector file '" + inputVectorFile
                                + "'; possible labels are: " + StringUtils.toString(data.getLabels(), 15));
            }

            File outputDir = new File(outputDirBase,
                    inputLabel.charAt(2) + "/" + inputLabel.charAt(3) + "/" + inputLabel.charAt(4));
            outputDir.mkdirs();
            File outputFile = new File(outputDir, inputLabel + ".txt");

            boolean fileExistsAndValid = false;
            if (outputFile.exists()) {
                // check if it the valid data
                String linesInvalid = "";
                int validLineCount = 0;
                ArrayList<String> lines = FileUtils.readLinesAsList(outputFile.getAbsolutePath());
                for (String string : lines) {
                    if (string.trim().length() == 0) {
                        continue;
                    }
                    String[] parts = string.split("\t");
                    if (parts.length != 2) {
                        linesInvalid += "Line '" + string + "' invalid - contains " + parts.length
                                + " elements.\n";
                    } else if (!NumberUtils.isNumber(parts[1])) {
                        linesInvalid = "Line '" + string + "' invalid - 2nd part is not a number.\n";
                    } else {
                        validLineCount++;
                    }
                }
                if (validLineCount != neighbours) {
                    linesInvalid = "Not enough valid lines; expected " + neighbours + ", found "
                            + validLineCount + ".\n";
                }
                fileExistsAndValid = true;
                if (org.apache.commons.lang.StringUtils.isNotBlank(linesInvalid)) {
                    System.out.println("File " + outputFile.getAbsolutePath() + " exists, but is not valid:\n"
                            + linesInvalid);
                }
            }

            if (fileExistsAndValid) {
                Logger.getLogger("at.tuwien.ifs.feature.evaluation").finer(
                        "File " + outputFile.getAbsolutePath() + " exists and is valid; not recomputing");
            } else {
                PrintWriter p = new PrintWriter(outputFile);
                SmallestElementSet<InputDistance> distances = data.getNearestDistances(inputDatumIndex,
                        neighbours, metric);
                for (InputDistance inputDistance : distances) {
                    p.println(inputDistance.getInput().getLabel() + "\t" + inputDistance.getDistance());
                }
                p.close();
            }
            progress.progress();
        }

    } catch (IllegalArgumentException e) {
        System.out.println(e.getMessage() + ". Aborting.");
        System.exit(-1);
    }
}

From source file:com.github.megatronking.svg.cli.Main.java

public static void main(String[] args) {
    Options opt = new Options();
    opt.addOption("d", "dir", true, "the target svg directory");
    opt.addOption("f", "file", true, "the target svg file");
    opt.addOption("o", "output", true, "the output vector file or directory");
    opt.addOption("w", "width", true, "the width size of target vector image");
    opt.addOption("h", "height", true, "the height size of target vector image");

    HelpFormatter formatter = new HelpFormatter();
    CommandLineParser parser = new PosixParser();

    CommandLine cl;//from  w  w  w .  j  av  a2s.com
    try {
        cl = parser.parse(opt, args);
    } catch (ParseException e) {
        formatter.printHelp(HELPER_INFO, opt);
        return;
    }

    if (cl == null) {
        formatter.printHelp(HELPER_INFO, opt);
        return;
    }

    int width = 0;
    int height = 0;
    if (cl.hasOption("w")) {
        width = SCU.parseInt(cl.getOptionValue("w"));
    }
    if (cl.hasOption("h")) {
        height = SCU.parseInt(cl.getOptionValue("h"));
    }

    String dir = null;
    String file = null;
    if (cl.hasOption("d")) {
        dir = cl.getOptionValue("d");
    } else if (cl.hasOption("f")) {
        file = cl.getOptionValue("f");
    }

    String output = null;
    if (cl.hasOption("o")) {
        output = cl.getOptionValue("o");
    }

    if (output == null) {
        if (dir != null) {
            output = dir;
        }
        if (file != null) {
            output = FileUtils.noExtensionName(file) + ".xml";
        }
    }

    if (dir == null && file == null) {
        formatter.printHelp(HELPER_INFO, opt);
        throw new RuntimeException("You must input the target svg file or directory");
    }

    if (dir != null) {
        File inputDir = new File(dir);
        if (!inputDir.exists() || !inputDir.isDirectory()) {
            throw new RuntimeException("The path [" + dir + "] is not exist or valid directory");
        }
        File outputDir = new File(output);
        if (outputDir.exists() || outputDir.mkdirs()) {
            svg2vectorForDirectory(inputDir, outputDir, width, height);
        } else {
            throw new RuntimeException("The path [" + outputDir + "] is not a valid directory");
        }
    }

    if (file != null) {
        File inputFile = new File(file);
        if (!inputFile.exists() || !inputFile.isFile()) {
            throw new RuntimeException("The path [" + file + "] is not exist or valid file");
        }
        svg2vectorForFile(inputFile, new File(output), width, height);
    }
}

From source file:com.linkedin.python.importer.ImporterCLI.java

public static void main(String[] args) throws Exception {

    Options options = createOptions();//  ww w  .  j  av  a  2 s .c om

    CommandLineParser parser = new DefaultParser();
    CommandLine line = parser.parse(options, args);

    if (line.hasOption("quite")) {
        Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
        root.setLevel(Level.WARN);
    }

    final File repoPath = new File(line.getOptionValue("repo"));
    final DependencySubstitution replacements = new DependencySubstitution(buildSubstitutionMap(line));

    repoPath.mkdirs();

    if (!repoPath.exists() || !repoPath.isDirectory()) {
        throw new RuntimeException(
                "Unable to continue, " + repoPath.getAbsolutePath() + " does not exist, or is not a directory");
    }

    for (String dependency : line.getArgList()) {
        new DependencyDownloader(dependency, repoPath, replacements).download();
    }

    logger.info("Execution Finished!");
}

From source file:embedding.ExampleJava2D2PDF.java

/**
 * Main method.//from   ww  w .j a  v a  2s.co m
 * @param args command-line arguments
 */
public static void main(String[] args) {
    try {
        System.out.println("FOP " + ExampleJava2D2PDF.class.getSimpleName() + "\n");
        System.out.println("Preparing...");

        //Setup directories
        File baseDir = new File(".");
        File outDir = new File(baseDir, "out");
        if (!outDir.isDirectory()) {
            if (!outDir.mkdirs()) {
                throw new IOException("Could not create output directory: " + outDir);
            }
        }

        //Setup output file
        File pdffile = new File(outDir, "ResultJava2D2PDF.pdf");

        System.out.println("Output: PDF (" + pdffile + ")");
        System.out.println();
        System.out.println("Generating...");

        ExampleJava2D2PDF app = new ExampleJava2D2PDF();
        app.generatePDF(pdffile);

        System.out.println("Success!");
    } catch (Throwable t) {
        t.printStackTrace(System.err);
        System.exit(-1);
    }
}

From source file:de.tudarmstadt.ukp.experiments.dip.wp1.documents.Step9AgreementCollector.java

public static void main(String[] args) throws Exception {
    // input dir - list of xml query containers
    // /home/user-ukp/research/data/dip/wp1-documents/step3-filled-raw-html
    File inputDir = new File(args[0]);

    // output dir
    File outputDir = new File(args[1]);
    if (!outputDir.exists()) {
        outputDir.mkdirs();
    }//from  w w  w.j  a  v  a2  s  . c  o  m

    computeObservedAgreement(inputDir, outputDir);
}

From source file:de.tudarmstadt.ukp.experiments.argumentation.convincingness.sampling.Step7aLearningDataProducer.java

@SuppressWarnings("unchecked")
public static void main(String[] args) throws IOException {
    String inputDir = args[0];//from   w  w  w  . ja  v a2  s . c  o  m
    File outputDir = new File(args[1]);

    if (!outputDir.exists()) {
        outputDir.mkdirs();
    }

    Collection<File> files = IOHelper.listXmlFiles(new File(inputDir));

    // for generating ConvArgStrict use this
    String prefix = "no-eq_DescendingScoreArgumentPairListSorter";

    // for oversampling using graph transitivity properties use this
    //        String prefix = "generated_no-eq_AscendingScoreArgumentPairListSorter";

    Iterator<File> iterator = files.iterator();
    while (iterator.hasNext()) {
        File file = iterator.next();

        if (!file.getName().startsWith(prefix)) {
            iterator.remove();
        }
    }

    int totalGoldPairsCounter = 0;

    Map<String, Integer> goldDataDistribution = new HashMap<>();

    DescriptiveStatistics statsPerTopic = new DescriptiveStatistics();

    int totalPairsWithReasonSameAsGold = 0;

    DescriptiveStatistics ds = new DescriptiveStatistics();

    for (File file : files) {
        List<AnnotatedArgumentPair> argumentPairs = (List<AnnotatedArgumentPair>) XStreamTools.getXStream()
                .fromXML(file);

        int pairsPerTopicCounter = 0;

        String name = file.getName().replaceAll(prefix, "").replaceAll("\\.xml", "");

        PrintWriter pw = new PrintWriter(new File(outputDir, name + ".csv"), "utf-8");

        pw.println("#id\tlabel\ta1\ta2");

        for (AnnotatedArgumentPair argumentPair : argumentPairs) {
            String goldLabel = argumentPair.getGoldLabel();

            if (!goldDataDistribution.containsKey(goldLabel)) {
                goldDataDistribution.put(goldLabel, 0);
            }

            goldDataDistribution.put(goldLabel, goldDataDistribution.get(goldLabel) + 1);

            pw.printf(Locale.ENGLISH, "%s\t%s\t%s\t%s%n", argumentPair.getId(), goldLabel,
                    multipleParagraphsToSingleLine(argumentPair.getArg1().getText()),
                    multipleParagraphsToSingleLine(argumentPair.getArg2().getText()));

            pairsPerTopicCounter++;

            int sameInOnePair = 0;

            // get gold reason statistics
            for (AnnotatedArgumentPair.MTurkAssignment assignment : argumentPair.mTurkAssignments) {
                String label = assignment.getValue();

                if (goldLabel.equals(label)) {
                    sameInOnePair++;
                }
            }

            ds.addValue(sameInOnePair);
            totalPairsWithReasonSameAsGold += sameInOnePair;
        }

        totalGoldPairsCounter += pairsPerTopicCounter;
        statsPerTopic.addValue(pairsPerTopicCounter);

        pw.close();
    }

    System.out.println("Total reasons matching gold " + totalPairsWithReasonSameAsGold);
    System.out.println(ds);

    System.out.println("Total gold pairs: " + totalGoldPairsCounter);
    System.out.println(statsPerTopic);

    int totalPairs = 0;
    for (Integer pairs : goldDataDistribution.values()) {
        totalPairs += pairs;
    }
    System.out.println("Total pairs: " + totalPairs);
    System.out.println(goldDataDistribution);

}

From source file:embedding.events.ExampleEvents.java

/**
 * Main method.//w  w w  . ja  v a2  s  . com
 * @param args command-line arguments
 */
public static void main(String[] args) {
    try {
        System.out.println("FOP ExampleEvents\n");
        System.out.println("Preparing...");

        //Setup directories
        File baseDir = new File(".");
        File outDir = new File(baseDir, "out");
        outDir.mkdirs();

        //Setup input and output files
        URL fo = ExampleEvents.class.getResource("missing-image.fo");
        File pdffile = new File(outDir, "out.pdf");

        System.out.println("Input: XSL-FO (" + fo.toExternalForm() + ")");
        System.out.println("Output: PDF (" + pdffile + ")");
        System.out.println();
        System.out.println("Transforming...");

        ExampleEvents app = new ExampleEvents();

        try {
            app.convertFO2PDF(fo, pdffile);
        } catch (TransformerException te) {
            //Note: We don't get the original exception here!
            //FOP needs to embed the exception in a SAXException and the TraX transformer
            //again wraps the SAXException in a TransformerException. Even our own
            //RuntimeException just wraps the original FileNotFoundException.
            //So we need to unpack to get the original exception (about three layers deep).
            Throwable originalThrowable = getOriginalThrowable(te);
            originalThrowable.printStackTrace(System.err);
            System.out.println("Aborted!");
            System.exit(-1);
        }

        System.out.println("Success!");
    } catch (Exception e) {
        //Some other error (shouldn't happen in this example)
        e.printStackTrace(System.err);
        System.exit(-1);
    }
}

From source file:DruidResponseTime.java

public static void main(String[] args) throws Exception {
    try (CloseableHttpClient client = HttpClients.createDefault()) {
        HttpPost post = new HttpPost("http://localhost:8082/druid/v2/?pretty");
        post.addHeader("content-type", "application/json");
        CloseableHttpResponse res;/*from ww w . j  a  va 2s . co  m*/

        if (STORE_RESULT) {
            File dir = new File(RESULT_DIR);
            if (!dir.exists()) {
                dir.mkdirs();
            }
        }

        int length;

        // Make sure all segments online
        System.out.println("Test if number of records is " + RECORD_NUMBER);
        post.setEntity(new StringEntity("{" + "\"queryType\":\"timeseries\","
                + "\"dataSource\":\"tpch_lineitem\"," + "\"intervals\":[\"1992-01-01/1999-01-01\"],"
                + "\"granularity\":\"all\"," + "\"aggregations\":[{\"type\":\"count\",\"name\":\"count\"}]}"));
        while (true) {
            System.out.print('*');
            res = client.execute(post);
            boolean valid;
            try (BufferedInputStream in = new BufferedInputStream(res.getEntity().getContent())) {
                length = in.read(BYTE_BUFFER);
                valid = new String(BYTE_BUFFER, 0, length, "UTF-8").contains("\"count\" : 6001215");
            }
            res.close();
            if (valid) {
                break;
            } else {
                Thread.sleep(5000);
            }
        }
        System.out.println("Number of Records Test Passed");

        for (int i = 0; i < QUERIES.length; i++) {
            System.out.println(
                    "--------------------------------------------------------------------------------");
            System.out.println("Start running query: " + QUERIES[i]);
            try (BufferedReader reader = new BufferedReader(
                    new FileReader(QUERY_FILE_DIR + File.separator + i + ".json"))) {
                length = reader.read(CHAR_BUFFER);
                post.setEntity(new StringEntity(new String(CHAR_BUFFER, 0, length)));
            }

            // Warm-up Rounds
            System.out.println("Run " + WARMUP_ROUND + " times to warm up cache...");
            for (int j = 0; j < WARMUP_ROUND; j++) {
                res = client.execute(post);
                res.close();
                System.out.print('*');
            }
            System.out.println();

            // Test Rounds
            int[] time = new int[TEST_ROUND];
            int totalTime = 0;
            System.out.println("Run " + TEST_ROUND + " times to get average time...");
            for (int j = 0; j < TEST_ROUND; j++) {
                long startTime = System.currentTimeMillis();
                res = client.execute(post);
                long endTime = System.currentTimeMillis();
                if (STORE_RESULT && j == 0) {
                    try (BufferedInputStream in = new BufferedInputStream(res.getEntity().getContent());
                            BufferedWriter writer = new BufferedWriter(
                                    new FileWriter(RESULT_DIR + File.separator + i + ".json", false))) {
                        while ((length = in.read(BYTE_BUFFER)) > 0) {
                            writer.write(new String(BYTE_BUFFER, 0, length, "UTF-8"));
                        }
                    }
                }
                res.close();
                time[j] = (int) (endTime - startTime);
                totalTime += time[j];
                System.out.print(time[j] + "ms ");
            }
            System.out.println();

            // Process Results
            double avgTime = (double) totalTime / TEST_ROUND;
            double stdDev = 0;
            for (int temp : time) {
                stdDev += (temp - avgTime) * (temp - avgTime) / TEST_ROUND;
            }
            stdDev = Math.sqrt(stdDev);
            System.out.println("The average response time for the query is: " + avgTime + "ms");
            System.out.println("The standard deviation is: " + stdDev);
        }
    }
}

From source file:boa.datagen.BoaGenerator.java

public static void main(final String[] args) throws IOException {
    final Options options = new Options();
    BoaGenerator.addOptions(options);/*ww  w .ja va  2s  .co  m*/

    final CommandLine cl;
    try {
        cl = new PosixParser().parse(options, args);
        BoaGenerator.handleCmdOptions(cl, options, args);
    } catch (final Exception e) {
        printHelp(options, e.getMessage());
        return;
    }

    /*
     * 1. if user provides local json files 2. if user provides username and
     * password in both the cases json files are going to be available
     */

    if (jsonAvailable) {
        CacheGithubJSON.main(args);
        try {
            SeqRepoImporter.main(args);
        } catch (final InterruptedException e) {
            e.printStackTrace();
        }

        // SeqProjectCombiner.main(args);
        // SeqSort.main(args);
        // SeqSortMerge.main(args);
        try {
            MapFileGen.main(args);
        } catch (final Exception e) {
            e.printStackTrace();
        }
    } else { // when user provides local repo and does not have json files
        final File output = new File(DefaultProperties.GH_JSON_CACHE_PATH);
        if (!output.exists())
            output.mkdirs();
        LocalGitSequenceGenerator.localGitSequenceGenerate(DefaultProperties.GH_GIT_PATH,
                DefaultProperties.GH_JSON_CACHE_PATH);
        try {
            MapFileGen.main(args);
        } catch (final Exception e) {
            e.printStackTrace();
        }
    }
    if (cl.hasOption("cache"))
        clear(true);
    else
        clear(false);
}