Example usage for java.io File exists

List of usage examples for java.io File exists

Introduction

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

Prototype

public boolean exists() 

Source Link

Document

Tests whether the file or directory denoted by this abstract pathname exists.

Usage

From source file:com.quangphuong.crawler.util.HighlightsOfflineCrawler.java

public static void main(String[] args) {
    webClient.getOptions().setJavaScriptEnabled(false);
    webClient.getOptions().setCssEnabled(false);
    webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
    //        webClient.setJavaScriptTimeout(2 * 1000);
    int y = startY;
    int m = startM;
    int d = startD;
    int date = 0;
    File f = new File(cachePath);
    if (f.exists() && !f.isDirectory()) {
        date = (Integer) ObjectIO.read(cachePath);
        y = date / 10000;/*from   ww w .  j a va 2  s  . co  m*/
        m = date % 10000;
        d = m % 100;
        System.out.println("D: " + d + "-M: " + m + "-Y: " + y);
    }
    boolean goNext = true;
    while (date != stopDate) {
        date = y + m + d;
        // Crawler
        String dateStr = String.valueOf(date);
        String link = AppConstant.videoPrefix + dateStr;
        try {
            System.out.println("Link: " + link);
            HtmlPage page = webClient.getPage(link);
            goNext = page.getWebResponse().getStatusCode() != 503;
            List<HtmlElement> tables = (List<HtmlElement>) page.getByXPath(AppConstant.hightlightTables);
            int count = 0;
            String kind = "";
            String tournament = "";
            for (HtmlElement table : tables) {
                count++;

                //Get kind
                HtmlElement span = table.getFirstByXPath(AppConstant.highlightKind);
                if (span != null && span.getAttribute("class").equals("whitetitle")) {
                    kind = span.getTextContent();
                    System.out.println(kind);
                }

                // Get Tournament
                if (table.getAttribute("background") != null && !"".equals(table.getAttribute("background"))) {
                    HtmlElement el = table.getFirstByXPath(AppConstant.highlightTournament);
                    tournament = el.getTextContent();
                    System.out.println(tournament);
                } else // Get matches
                {
                    if (count != 1 && (table.getAttribute("bgcolor").equals(""))) {
                        List<HtmlElement> matches = (List<HtmlElement>) table
                                .getByXPath(AppConstant.highlightMatches);
                        for (HtmlElement el : matches) {
                            //                            System.out.println(el.asXml());
                            HtmlElement tmp = el.getFirstByXPath(AppConstant.highlightMatch);
                            String match = tmp.getTextContent().trim();

                            tmp = el.getFirstByXPath(AppConstant.highlightMatchTime);
                            String time = tmp.getTextContent().trim();
                            tmp = el.getFirstByXPath(AppConstant.highlightMatchLogoTeam1);
                            String logoTeam1;
                            try {
                                logoTeam1 = tmp.getAttribute("src");
                            } catch (Exception e) {
                                logoTeam1 = "";
                            }
                            tmp = el.getFirstByXPath(AppConstant.highlightMatchScore);
                            String score = tmp.getTextContent().trim();
                            tmp = el.getFirstByXPath(AppConstant.highlightMatchLogoTeam2);
                            String logoTeam2;
                            try {
                                logoTeam2 = tmp.getAttribute("src");
                            } catch (Exception e) {
                                logoTeam2 = "";
                            }
                            //                            webClient.waitForBackgroundJavaScript(10 * 1000);
                            tmp = el.getFirstByXPath(AppConstant.highlightMatchLink);
                            String highlightLink;
                            try {
                                highlightLink = tmp.getAttribute("href");
                            } catch (Exception e) {
                                highlightLink = "";
                            }
                            tmp = el.getFirstByXPath(AppConstant.highlightMatchFullLink);
                            String fullMatchLink;
                            try {
                                fullMatchLink = tmp.getAttribute("href");
                            } catch (Exception e) {
                                fullMatchLink = "";
                            }
                            tmp = el.getFirstByXPath(AppConstant.highlightMatchLongLink);
                            String longHighlightLink;
                            try {
                                longHighlightLink = tmp.getAttribute("href");
                            } catch (Exception e) {
                                longHighlightLink = "";
                            }
                            System.out.println("----" + match + "-" + time + "-" + logoTeam1 + "-" + score + "-"
                                    + logoTeam2 + "-" + highlightLink + "-" + fullMatchLink + "-"
                                    + longHighlightLink);
                            //                            System.out.println("kindddddddddddddddd: " + kind);
                            Highlight highlight = new Highlight(0, kind, tournament, match, logoTeam1,
                                    logoTeam2, highlightLink, longHighlightLink, fullMatchLink, score, dateStr,
                                    time);

                            DBWrapper dBWrapper = new DBWrapper(false);
                            dBWrapper.updateEntity(highlight);
                        }
                    }
                }
            }
            //                System.out.println("-------------------");
            //                System.out.println("Page memory: " + Agent.sizeOf(page));
        } catch (Exception ex) {
            Logger.getLogger(HighlightsOfflineCrawler.class.getName()).log(Level.SEVERE, null, ex);
        }
        ObjectIO.write(cachePath, date);
        if (goNext) {
            if (m == 1200 && isLastDayOfMonth(d, m, y)) {
                y += 10000;
                m = 100;
                d = 1;
            } else if (isLastDayOfMonth(d, m, y)) {
                m += 100;
                d = 1;
            } else {
                d += 1;
            }
        }
    }
}

From source file:mase.MaseEvolve.java

public static void main(String[] args) throws Exception {
    File outDir = getOutDir(args);
    boolean force = Arrays.asList(args).contains(FORCE);
    if (!outDir.exists()) {
        outDir.mkdirs();/*from   ww  w  . j  ava  2  s. c  o  m*/
    } else if (!force) {
        System.out.println("Folder already exists: " + outDir.getAbsolutePath() + ". Waiting 5 sec.");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException ex) {
        }
    }

    // Get config file
    Map<String, String> pars = readParams(args);

    // Copy config to outdir
    try {
        File rawConfig = writeConfig(args, pars, outDir, false);
        File destiny = new File(outDir, DEFAULT_CONFIG);
        destiny.delete();
        FileUtils.moveFile(rawConfig, new File(outDir, DEFAULT_CONFIG));
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    // JBOT INTEGRATION: copy jbot config file to the outdir
    // Does nothing when jbot is not used
    if (pars.containsKey("problem.jbot-config")) {
        File jbot = new File(pars.get("problem.jbot-config"));
        FileUtils.copyFile(jbot, new File(outDir, jbot.getName()));
    }

    // Write config to system temp file
    File config = writeConfig(args, pars, outDir, true);
    // Launch
    launchExperiment(config);
}

From source file:com.nextdoor.bender.S3SnsNotifier.java

public static void main(String[] args) throws ParseException, InterruptedException, IOException {
    formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").withZoneUTC();

    /*//from   ww  w. j a  v  a2 s  .c  o  m
     * Parse cli arguments
     */
    Options options = new Options();
    options.addOption(Option.builder().longOpt("bucket").hasArg().required()
            .desc("Name of S3 bucket to list s3 objects from").build());
    options.addOption(Option.builder().longOpt("key-file").hasArg().required()
            .desc("Local file of S3 keys to process").build());
    options.addOption(
            Option.builder().longOpt("sns-arn").hasArg().required().desc("SNS arn to publish to").build());
    options.addOption(Option.builder().longOpt("throttle-ms").hasArg()
            .desc("Amount of ms to wait between publishing to SNS").build());
    options.addOption(Option.builder().longOpt("processed-file").hasArg()
            .desc("Local file to use to store procssed S3 object names").build());
    options.addOption(Option.builder().longOpt("skip-processed").hasArg(false)
            .desc("Whether to skip S3 objects that have been processed").build());
    options.addOption(
            Option.builder().longOpt("dry-run").hasArg(false).desc("If set do not publish to SNS").build());

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

    String bucket = cmd.getOptionValue("bucket");
    String keyFile = cmd.getOptionValue("key-file");
    String snsArn = cmd.getOptionValue("sns-arn");
    String processedFile = cmd.getOptionValue("processed-file", null);
    boolean skipProcessed = cmd.hasOption("skip-processed");
    dryRun = cmd.hasOption("dry-run");
    long throttle = Long.parseLong(cmd.getOptionValue("throttle-ms", "-1"));

    if (processedFile != null) {
        File file = new File(processedFile);

        if (!file.exists()) {
            logger.debug("creating local file to store processed s3 object names: " + processedFile);
            file.createNewFile();
        }
    }

    /*
     * Import S3 keys that have been processed
     */
    if (skipProcessed && processedFile != null) {
        try (BufferedReader br = new BufferedReader(new FileReader(processedFile))) {
            String line;
            while ((line = br.readLine()) != null) {
                alreadyPublished.add(line.trim());
            }
        }
    }

    /*
     * Setup writer for file containing processed S3 keys
     */
    FileWriter fw = null;
    BufferedWriter bw = null;
    if (processedFile != null) {
        fw = new FileWriter(processedFile, true);
        bw = new BufferedWriter(fw);
    }

    /*
     * Create clients
     */
    AmazonS3Client s3Client = new AmazonS3Client();
    AmazonSNSClient snsClient = new AmazonSNSClient();

    /*
     * Get S3 object list
     */
    try (BufferedReader br = new BufferedReader(new FileReader(keyFile))) {
        String line;
        while ((line = br.readLine()) != null) {
            String key = line.trim();

            if (alreadyPublished.contains(key)) {
                logger.info("skipping " + key);
            }

            ObjectMetadata om = s3Client.getObjectMetadata(bucket, key);

            S3EventNotification s3Notification = getS3Notification(key, bucket, om.getContentLength());

            String json = s3Notification.toJson();

            /*
             * Publish to SNS
             */
            if (publish(snsArn, json, snsClient, key) && processedFile != null) {
                bw.write(key + "\n");
                bw.flush();
            }

            if (throttle != -1) {
                Thread.sleep(throttle);
            }

        }
    }

    if (processedFile != null) {
        bw.close();
        fw.close();
    }
}

From source file:edu.illinois.cs.cogcomp.ner.BenchmarkOutputParser.java

/**
 * This main method will take one required argument, idenfitying the file containing 
 * the results. Optionally, "-single" may also be passed indicating it will extract
 * the F1 value for single token values only.
 * @param args/*from  w  w w .ja va  2  s  .  c o m*/
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    parseArgs(args);
    System.out.println("L1lr,L1t,L2lr,L2t,L1 token,L2 token,F1,F2");
    for (File file : resultsfile.listFiles()) {
        if (file.getName().startsWith("L1r")) {
            File resultsfile = new File(file, "ner/results.out");
            if (resultsfile.exists()) {
                try {
                    Parameters p = parseFilename(file);
                    String lines = FileUtils.readFileToString(resultsfile);

                    // get the token level score.
                    String tokenL2 = null, tokenL1 = null;
                    Matcher matcher = l2tokenlevelpattern.matcher(lines);
                    if (matcher.find())
                        tokenL2 = matcher.group(1);
                    else {
                        matcher = ol2tokenlevelpattern.matcher(lines);
                        if (matcher.find())
                            tokenL2 = matcher.group(1);
                        else
                            System.err.println("No token level match");
                    }

                    matcher = l1tokenlevelpattern.matcher(lines);
                    if (matcher.find())
                        tokenL1 = matcher.group(1);
                    else {
                        matcher = ol1tokenlevelpattern.matcher(lines);
                        if (matcher.find())
                            tokenL1 = matcher.group(1);
                        else
                            System.err.println("No token level match");
                    }

                    matcher = phraselevelpattern.matcher(lines);
                    matcher.find();
                    String phraseL1 = matcher.group(1);
                    String phraseL2 = matcher.group(2);
                    System.out.println(
                            p.toString() + "," + tokenL1 + "," + tokenL2 + "," + phraseL1 + "," + phraseL2);
                } catch (java.lang.IllegalStateException ise) {
                    System.err.println("The results file could not be parsed : \"" + resultsfile + "\"");
                }
            } else {
                System.err.println("no results in " + resultsfile);
            }

        }
    }
}

From source file:de.unirostock.sems.caro.CaRo.java

/**
 * The main method to be called by the command line.
 * /* ww w.  j  a  v  a 2s .c o  m*/
 * @param args
 *          the arguments
 */
public static void main(String[] args) {
    Options options = new Options();

    options.addOption(new Option("h", "help", false, "print the help message"));
    options.addOption(
            Option.builder().longOpt("roca").desc("convert a research object into a combine archive").build());
    options.addOption(
            Option.builder().longOpt("caro").desc("convert a combine archive into a research object").build());
    options.addOption(Option.builder("i").longOpt("in").required().argName("FILE").hasArg()
            .desc("source container to be converted").build());
    options.addOption(Option.builder("o").longOpt("out").required().argName("FILE").hasArg()
            .desc("target container to be created").build());

    CommandLineParser parser = new DefaultParser();
    CommandLine line = null;
    try {
        line = parser.parse(options, args);
        if (line.hasOption("help")) {
            help(options, null);
            return;
        }
    } catch (ParseException e) {
        help(options, "Parsing of command line options failed.  Reason: " + e.getMessage());
        return;
    }

    File in = new File(line.getOptionValue("in"));
    File out = new File(line.getOptionValue("out"));

    if (!in.exists()) {
        help(options, "file " + in + " does not exist");
        return;
    }

    if (out.exists()) {
        help(options, "file " + out + " already exist");
        return;
    }

    if (line.hasOption("caro") && line.hasOption("roca")) {
        help(options, "only one of --roca and --caro is allowed");
        return;
    }

    CaRoConverter conv = null;

    if (line.hasOption("caro"))
        conv = new CaToRo(in);
    else if (line.hasOption("roca"))
        conv = new RoToCa(in);
    else {
        help(options, "you need to either supply --roca or --caro");
        return;
    }
    conv.convertTo(out);

    if (conv.hasErrors())
        System.err.println("There were errors!");

    if (conv.hasWarnings())
        System.err.println("There were warnings!");

    List<CaRoNotification> notifications = conv.getNotifications();
    for (CaRoNotification note : notifications)
        System.out.println(note);

}

From source file:Base64.java

public static void main(String[] args) {
    boolean decode = false;

    if (args.length == 0) {
        System.out.println("usage:  java Base64 [-d[ecode]] filename");
        System.exit(0);/*  w w  w .  j a va 2  s . c  om*/
    }
    for (int i = 0; i < args.length; i++) {
        if ("-decode".equalsIgnoreCase(args[i]))
            decode = true;
        else if ("-d".equalsIgnoreCase(args[i]))
            decode = true;
    }

    String filename = args[args.length - 1];
    File file = new File(filename);
    if (!file.exists()) {
        System.out.println("Error:  file '" + filename + "' doesn't exist!");
        System.exit(0);
    }

    if (decode) {
        char[] encoded = readChars(file);
        byte[] decoded = decode(encoded);
        writeBytes(file, decoded);
    } else {
        byte[] decoded = readBytes(file);
        char[] encoded = encode(decoded);
        writeChars(file, encoded);
    }
}

From source file:com.gsma.iariauth.validator.util.IARIValidatorMain.java

public static void main(String[] args) {
    String formatstr = "IARIValidator [-d <authorization document>] [-n <package name>] [-ps <package signer fingerprint>] [-pk <package signer keystore>] [-pa <package signer certificate alias>] [-pp <package signer keystore password>] -k <keystore> -p <password> [-v]";

    HelpFormatter formatter = new HelpFormatter();
    GnuParser parser = new GnuParser();
    Options opts = new Options();

    opts.addOption(new ArgOption("d", "document", "IARI Authorization document"));
    opts.addOption(new ArgOption("pkgname", "package-name", "package name"));
    opts.addOption(new ArgOption("pkgsigner", "package-signer", "package signer fingerprint"));
    opts.addOption(new ArgOption("pkgkeystore", "package-keystore", "package signing keystore"));
    opts.addOption(new ArgOption("pkgalias", "package-key-alias", "package signing certificate alias"));
    opts.addOption(new ArgOption("pkgstorepass", "package-keystore-pass", "package signing keystore password"));
    opts.addOption(new Option("v", "verbose", false, "verbose output"));

    CommandLine cli = null;/*from   ww  w  .  ja  v  a 2 s. co  m*/
    try {
        cli = parser.parse(opts, args);
    } catch (ParseException e) {
        formatter.printHelp(formatstr, opts);
        return;
    }

    boolean verbose = cli.hasOption("v");

    String packageName = cli.getOptionValue("pkgname");
    String packageSigner = cli.getOptionValue("pkgsigner");
    if (packageSigner == null) {
        String packageSignerKeystore = cli.getOptionValue("pkgkeystore");
        String packageSignerKeystoreAlias = cli.getOptionValue("pkgalias");
        String packageSignerKeystorePasswd = cli.getOptionValue("pkgstorepass");
        if (packageSignerKeystore != null) {
            if (packageSignerKeystoreAlias == null) {
                System.err.println("No alias given for package signing certificate");
                System.exit(1);
            }
            if (packageSignerKeystorePasswd == null) {
                System.err.println("No password given for package signing keystore");
                System.exit(1);
            }
            KeyStore packageKeystore = loadKeyStore(packageSignerKeystore, packageSignerKeystorePasswd);
            if (packageKeystore == null) {
                System.err.println("Unable to read package keystore");
                System.exit(1);
            }
            try {
                X509Certificate c = (X509Certificate) packageKeystore
                        .getCertificate(packageSignerKeystoreAlias);
                if (c == null) {
                    System.err.println("Unable to access package signing certificate");
                    System.exit(1);
                }
                packageSigner = getFingerprint(c);
            } catch (KeyStoreException e) {
                System.err.println("Unable to access package signing certificate");
                System.exit(1);
            } catch (CertificateEncodingException e) {
                e.printStackTrace();
                System.err.println("Unable to read package signing certificate");
                System.exit(1);
            }
        }
    }

    String authDocumentPath = cli.getOptionValue("d");
    if (authDocumentPath == null) {
        System.err.println("No auth document specified");
        System.exit(1);
    }
    File authDocument = new File(authDocumentPath);
    if (!authDocument.exists() || !authDocument.isFile()) {
        System.err.println("Unable to read specified auth document");
        System.exit(1);
    }

    PackageProcessor processor = new PackageProcessor(packageName, packageSigner);
    ProcessingResult result = processor.processIARIauthorization(authDocument);
    if (result.getStatus() != ProcessingResult.STATUS_OK) {
        System.err.println("Error validating authDocument:");
        System.err.println(result.getError().toString());
        System.exit(1);
    }

    if (verbose) {
        System.out.println(result.getAuthDocument().toString());
    }
    System.exit(0);
}

From source file:iaj.linkit.App.java

/**
 * Application entry method./*from ww  w  .ja  v a 2 s  .  c o m*/
 *
 * @param args Command line arguments.
 */
public static void main(final String[] args) {
    final Configuration config = new Configuration();
    final CmdLineParser parser = new CmdLineParser(config);

    try {
        parser.parseArgument(args);

        final File f = (config.getPaths().size() > 0) ? new File(config.getPaths().get(0)).getCanonicalFile()
                : new File(DIR_CURRENT).getCanonicalFile();
        if (!f.exists()) {
            System.err.println("No such path: " + f.getCanonicalFile());
            System.exit(1);
        } else if (!f.canRead()) {
            System.err.println("Can't read path.");
            System.exit(1);
        } else if (f.isDirectory()) {
            handleFiles(config.isRecursive(), f.listFiles());
        } else {
            handleFiles(config.isRecursive(), f);
        }

    } catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
    } catch (final CmdLineException e) {
        System.err.println(e.getMessage());
        System.err.println("lu [options...] [paths...]"); //$NON-NLS-1$
        parser.printUsage(System.err);
        System.exit(1);
    }
}

From source file:image.writer.ImageWriterExample1.java

/**
 * Command-line interface/*  w  w w .jav  a  2s . c om*/
 * @param args command-line arguments
 */
public static void main(String[] args) {
    try {
        File targetDir;
        if (args.length >= 1) {
            targetDir = new File(args[0]);
        } else {
            targetDir = new File(".");
        }
        if (!targetDir.exists()) {
            System.err.println("Target Directory does not exist: " + targetDir);
        }
        File outputFile = new File(targetDir, "eps-example1.tif");
        ImageWriterExample1 app = new ImageWriterExample1();
        app.generateBitmapUsingJava2D(outputFile, "image/tiff");
        System.out.println("File written: " + outputFile.getCanonicalPath());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:mil.tatrc.physiology.biogears.verification.ScenarioPlotTool.java

public static void main(String[] args) {
    if (args.length < 1) {
        Log.fatal("Expected inputs : [results file path] [result in results file NOT to plot] ");
        return;/*from  w w w .j  a v  a  2  s .co  m*/
    }
    File f = new File(args[0]);
    if (!f.exists()) {
        Log.fatal("Input file cannot be found");
        return;
    }
    String reportDir = "./graph_results/" + f.getName();
    reportDir = reportDir.substring(0, reportDir.lastIndexOf(".")) + "/";
    ScenarioPlotTool t = new ScenarioPlotTool();
    t.graphResults(args[0], reportDir);
}