Example usage for java.io File listFiles

List of usage examples for java.io File listFiles

Introduction

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

Prototype

public File[] listFiles() 

Source Link

Document

Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname.

Usage

From source file:gobblin.compaction.hive.CompactionRunner.java

public static void main(String[] args) throws IOException, ConfigurationException {

    properties = CliOptions.parseArgs(MRCompactionRunner.class, args);

    File compactionConfigDir = new File(properties.getProperty(COMPACTION_CONFIG_DIR));
    File[] listOfFiles = compactionConfigDir.listFiles();
    if (listOfFiles == null || listOfFiles.length == 0) {
        System.err.println("No compaction configuration files found under " + compactionConfigDir);
        System.exit(1);//  ww  w  .java 2 s.c  o  m
    }

    int numOfJobs = 0;
    for (File file : listOfFiles) {
        if (file.isFile() && !file.getName().startsWith(".")) {
            numOfJobs++;
        }
    }
    LOG.info("Found " + numOfJobs + " compaction tasks.");
    try (PrintWriter pw = new PrintWriter(new OutputStreamWriter(
            new FileOutputStream(properties.getProperty(TIMING_FILE, TIMING_FILE_DEFAULT)),
            Charset.forName("UTF-8")))) {

        for (File file : listOfFiles) {
            if (file.isFile() && !file.getName().startsWith(".")) {
                Configuration jobConfig = new PropertiesConfiguration(file.getAbsolutePath());
                jobProperties = ConfigurationConverter.getProperties(jobConfig);
                long startTime = System.nanoTime();
                compact();
                long endTime = System.nanoTime();
                long elapsedTime = endTime - startTime;
                double seconds = TimeUnit.NANOSECONDS.toSeconds(elapsedTime);
                pw.printf("%s: %f%n", file.getAbsolutePath(), seconds);
            }
        }
    }
}

From source file:gobblin.compaction.CompactionRunner.java

public static void main(String[] args) throws ConfigurationException, IOException, SQLException {

    if (args.length != 1) {
        LOG.info("Proper usage: java -jar compaction.jar <global-config-file>\n" + "or\n"
                + "hadoop jar compaction.jar <global-config-file>\n" + "or\n"
                + "yarn jar compaction.jar <global-config-file>\n");
        System.exit(1);/*from  ww w . j  a va 2  s.  c  o m*/
    }

    Configuration globalConfig = new PropertiesConfiguration(args[0]);
    properties = ConfigurationConverter.getProperties(globalConfig);

    File compactionConfigDir = new File(properties.getProperty(COMPACTION_CONFIG_DIR));
    File[] listOfFiles = compactionConfigDir.listFiles();
    if (listOfFiles == null || listOfFiles.length == 0) {
        System.err.println("No compaction configuration files found under " + compactionConfigDir);
        System.exit(1);
    }

    int numOfJobs = 0;
    for (File file : listOfFiles) {
        if (file.isFile() && !file.getName().startsWith(".")) {
            numOfJobs++;
        }
    }
    LOG.info("Found " + numOfJobs + " compaction tasks.");
    PrintWriter pw = new PrintWriter(new OutputStreamWriter(
            new FileOutputStream(properties.getProperty(TIMING_FILE, TIMING_FILE_DEFAULT)),
            Charset.forName("UTF-8")));

    for (File file : listOfFiles) {
        if (file.isFile() && !file.getName().startsWith(".")) {
            Configuration jobConfig = new PropertiesConfiguration(file.getAbsolutePath());
            jobProperties = ConfigurationConverter.getProperties(jobConfig);
            long startTime = System.nanoTime();
            compact();
            long endTime = System.nanoTime();
            long elapsedTime = endTime - startTime;
            double seconds = TimeUnit.NANOSECONDS.toSeconds(elapsedTime);
            pw.printf("%s: %f%n", file.getAbsolutePath(), seconds);
        }
    }

    pw.close();
}

From source file:com.sangupta.nutz.PerformanceTestSuite.java

public static void main(String[] args) throws Exception {
    File dir = new File("src/test/resources/markdown");
    File[] files = dir.listFiles();

    for (File file : files) {
        if (file.getName().endsWith(".text")) {
            final String markup = FileUtils.readFileToString(file);

            String html = file.getAbsolutePath();
            html = html.replace(".text", ".html");
            html = FileUtils.readFileToString(new File(html));

            TestData testData = new TestData(markup, html);
            tests.add(testData);//from w w w .  j  av  a 2 s. c  o m
        }
    }

    TestResults nutz = runTests(new TestExecutor() {

        private MarkdownProcessor processor = new MarkdownProcessor();

        @Override
        public String convertMarkup(String markup) throws Exception {
            return processor.toHtml(markup);
        }

    });

    TestResults txtmark = runTests(new TestExecutor() {

        @Override
        public String convertMarkup(String markup) throws Exception {
            return Processor.process(markup);
        }

    });

    TestResults pegdown = runTests(new TestExecutor() {

        private PegDownProcessor processor = new PegDownProcessor();

        @Override
        public String convertMarkup(String markup) throws Exception {
            return processor.markdownToHtml(markup);
        }

    });

    System.out.println("\n\n\n\n\n");
    System.out.println("Nutz: " + nutz);
    System.out.println("Pegdown: " + pegdown);
    System.out.println("TextMark: " + txtmark);
}

From source file:com.osrdata.etltoolbox.fileloader.Main.java

/**
 * Main entry point into the application.
 * @param args command line argunemtns/*from   w ww.  j ava 2s  .  co m*/
 */
public static void main(String[] args) {
    Options options = new Options();
    options.addOption(new Option("s", "spec", true, "Source-to-target specification file"));
    options.addOption(new Option("d", "directory", true, "Source directory to load"));
    options.addOption(new Option("f", "file", true, "File to perform operation on"));
    options.addOption(new Option("r", "replace", false, "Replace previously loaded data"));
    options.addOption(new Option("t", "trace", true, "Trace records processed at specified interval"));

    CommandLineParser parser = new BasicParser();

    try {
        CommandLine line = parser.parse(options, args);

        if (line.hasOption("spec") && (line.hasOption("directory") || line.hasOption("file"))) {
            FileLoader loader = new FileLoader(line);

            loader.init();

            if (line.hasOption("file")) {
                loader.load(new File(line.getOptionValue("file")));
            } else if (line.hasOption("directory")) {
                File directory = new File(line.getOptionValue("directory"));

                if (directory.isDirectory()) {
                    File[] files = directory.listFiles();

                    for (File file : files) {
                        loader.load(file);
                    }
                } else {
                    log.fatal(directory.getAbsolutePath() + " does not appear to be a directory.");
                }
            }
        } else {
            usage();
        }
    } catch (ParseException e) {
        usage();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:MainGeneratePicasaIniFile.java

public static void main(String[] args) {
    try {/*from w w w. java 2 s. c o  m*/

        Calendar start = Calendar.getInstance();

        start.set(1899, 11, 30, 0, 0);

        PicasawebService myService = new PicasawebService("My Application");
        myService.setUserCredentials(args[0], args[1]);

        // Get a list of all entries
        URL metafeedUrl = new URL("http://picasaweb.google.com/data/feed/api/user/" + args[0] + "?kind=album");
        System.out.println("Getting Picasa Web Albums entries...\n");
        UserFeed resultFeed = myService.getFeed(metafeedUrl, UserFeed.class);

        // resultFeed.

        File root = new File(args[2]);
        File[] albuns = root.listFiles();

        int j = 0;
        List<GphotoEntry> entries = resultFeed.getEntries();
        for (int i = 0; i < entries.size(); i++) {
            GphotoEntry entry = entries.get(i);
            String href = entry.getHtmlLink().getHref();

            String name = entry.getTitle().getPlainText();

            for (File album : albuns) {
                if (album.getName().equals(name) && !href.contains("02?")) {
                    File picasaini = new File(album, "Picasa.ini");

                    if (!picasaini.exists()) {
                        StringBuilder builder = new StringBuilder();

                        builder.append("\n");
                        builder.append("[Picasa]\n");
                        builder.append("name=");
                        builder.append(name);
                        builder.append("\n");
                        builder.append("location=");
                        Collection<Extension> extensions = entry.getExtensions();

                        for (Extension extension : extensions) {

                            if (extension instanceof GphotoLocation) {
                                GphotoLocation location = (GphotoLocation) extension;
                                if (location.getValue() != null) {
                                    builder.append(location.getValue());
                                }
                            }
                        }
                        builder.append("\n");
                        builder.append("category=Folders on Disk");
                        builder.append("\n");
                        builder.append("date=");
                        String source = name.substring(0, 10);

                        DateFormat formater = new SimpleDateFormat("yyyy-MM-dd");

                        Date date = formater.parse(source);

                        Calendar end = Calendar.getInstance();

                        end.setTime(date);

                        builder.append(daysBetween(start, end));
                        builder.append(".000000");
                        builder.append("\n");
                        builder.append(args[0]);
                        builder.append("_lh=");
                        builder.append(entry.getGphotoId());
                        builder.append("\n");
                        builder.append("P2category=Folders on Disk");
                        builder.append("\n");

                        URL feedUrl = new URL("https://picasaweb.google.com/data/feed/api/user/" + args[0]
                                + "/albumid/" + entry.getGphotoId());

                        AlbumFeed feed = myService.getFeed(feedUrl, AlbumFeed.class);

                        for (GphotoEntry photo : feed.getEntries()) {
                            builder.append("\n");
                            builder.append("[");
                            builder.append(photo.getTitle().getPlainText());
                            builder.append("]");
                            builder.append("\n");
                            long id = Long.parseLong(photo.getGphotoId());

                            builder.append("IIDLIST_");
                            builder.append(args[0]);
                            builder.append("_lh=");
                            builder.append(Long.toHexString(id));
                            builder.append("\n");
                        }

                        System.out.println(builder.toString());
                        IOUtils.write(builder.toString(), new FileOutputStream(picasaini));
                        j++;
                    }
                }

            }

        }
        System.out.println(j);
        System.out.println("\nTotal Entries: " + entries.size());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:edu.cuhk.hccl.TripRealRatingsApp.java

public static void main(String[] args) throws IOException {
    File dir = new File(args[0]);
    File outFile = new File(args[1]);
    outFile.delete();//from www .  j av  a  2  s .  co  m

    StringBuilder buffer = new StringBuilder();

    for (File file : dir.listFiles()) {
        List<String> lines = FileUtils.readLines(file, "UTF-8");
        String hotelID = file.getName().split("_")[1];
        String author = null;
        boolean noContent = false;
        for (String line : lines) {
            if (line.startsWith("<Author>")) {
                try {
                    author = line.split(">")[1].trim();
                } catch (ArrayIndexOutOfBoundsException e) {
                    System.out.println("[ERROR] An error occured on this line:");
                    System.out.println(line);
                    continue;
                }
            } else if (line.startsWith("<Content>")) { // ignore records if they have no content
                String content = line.split(">")[1].trim();
                if (content == null || content.equals(""))
                    noContent = true;
            } else if (line.startsWith("<Rating>")) {
                String[] rates = line.split(">")[1].trim().split("\t");

                if (noContent || rates.length != 8)
                    continue;

                // Change missing rating from -1 to 0
                for (int i = 0; i < rates.length; i++) {
                    if (rates[i].equals("-1"))
                        rates[i] = "0";
                }

                buffer.append(author + "\t");
                buffer.append(hotelID + "\t");

                // overall
                buffer.append(rates[0] + "\t");
                // location
                buffer.append(rates[3] + "\t");
                // room
                buffer.append(rates[2] + "\t");
                // service
                buffer.append(rates[6] + "\t");
                // value
                buffer.append(rates[1] + "\t");
                // cleanliness
                buffer.append(rates[4] + "\t");

                buffer.append("\n");
            }
        }

        // Write once for each file
        FileUtils.writeStringToFile(outFile, buffer.toString(), true);

        // Clear buffer
        buffer.setLength(0);
        System.out.printf("[INFO] Finished processing %s\n", file.getName());
    }
    System.out.println("[INFO] All processinig are finished!");
}

From source file:com.carteblanche.kwd.driver.KeywordDrivenDriver.java

public static void main(String[] args) {
    File[] listOfFiles;/*from   w w  w.  jav  a2  s .c  o m*/

    File folder = new File(testDirectory + testSuiteName + "/");
    String testSuiteName = folder.getName();
    testSuiteName = StringUtils
            .capitalize(StringUtils.join(StringUtils.splitByCharacterTypeCamelCase(testSuiteName), ' '));
    listOfFiles = folder.listFiles();
    TestNGDriver testNGDriver = new TestNGDriver();

    int i = 0;
    for (File csv : listOfFiles) {
        KWDTestCase testCase = TestCaseParser.parse(csv, cvsSplitBy);
        testNGDriver.runTests(testCase, testSuiteName + i, folder.getName());
        i++;
    }

}

From source file:edu.gslis.ts.ThriftToTREC.java

public static void main(String[] args) {
    try {//from   w ww.ja v  a  2 s. c o  m
        // Get the commandline options
        Options options = createOptions();
        CommandLineParser parser = new GnuParser();
        CommandLine cmd = parser.parse(options, args);

        String in = cmd.getOptionValue("i");
        String outfile = cmd.getOptionValue("o");
        String sentenceParser = cmd.getOptionValue("p");

        // Setup the filter
        ThriftToTREC f = new ThriftToTREC();

        if (in != null && outfile != null) {
            File infile = new File(in);
            if (infile.isDirectory()) {
                for (File file : infile.listFiles()) {
                    if (file.isDirectory()) {
                        for (File filefile : file.listFiles()) {
                            f.filter(filefile, new File(outfile), sentenceParser);
                        }
                    } else {
                        f.filter(file, new File(outfile), sentenceParser);
                    }
                }
            } else
                f.filter(infile, new File(outfile), sentenceParser);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:fi.helsinki.cs.iot.kahvihub.KahviHub.java

public static void main(String[] args) throws InterruptedException {
    // create Options object
    Options options = new Options();
    // add conf file option
    options.addOption("c", true, "config file");
    CommandLineParser parser = new BasicParser();
    CommandLine cmd;//from w w  w.  j av a 2 s.com
    try {
        cmd = parser.parse(options, args);
        String configFile = cmd.getOptionValue("c");
        if (configFile == null) {
            Log.e(TAG, "The config file option was not provided");
            // automatically generate the help statement
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("d", options);
            System.exit(-1);
        } else {
            try {
                HubConfig hubConfig = ConfigurationFileParser.parseConfigurationFile(configFile);
                Path libdir = Paths.get(hubConfig.getLibdir());
                if (hubConfig.isDebugMode()) {
                    File dir = libdir.toFile();
                    if (dir.exists() && dir.isDirectory())
                        for (File file : dir.listFiles())
                            file.delete();
                }
                final IotHubHTTPD server = new IotHubHTTPD(hubConfig.getPort(), libdir, hubConfig.getHost());
                init(hubConfig);
                try {
                    server.start();
                } catch (IOException ioe) {
                    Log.e(TAG, "Couldn't start server:\n" + ioe);
                    System.exit(-1);
                }
                Runtime.getRuntime().addShutdownHook(new Thread() {
                    @Override
                    public void run() {
                        server.stop();
                        Log.i(TAG, "Server stopped");
                    }
                });

                while (true) {
                    Thread.sleep(1000);
                }
            } catch (ConfigurationParsingException | IOException e) {
                System.out.println("1:" + e.getMessage());
                Log.e(TAG, e.getMessage());
                System.exit(-1);
            }
        }

    } catch (ParseException e) {
        System.out.println(e.getMessage());
        Log.e(TAG, e.getMessage());
        System.exit(-1);
    }
}

From source file:name.martingeisse.miner.server.tools.RegionConverter.java

/**
 * The main method//www. j  a va 2  s.co m
 * @param args command-line arguments
 * @throws Exception on errors
 */
public static void main(String[] args) throws Exception {
    if (args.length != 1) {
        System.out.println("usage: RegionConverter <region-folder>");
        return;
    }
    File regionFolder = new File(args[0]);
    RegionConverter converter = new RegionConverter();
    for (File regionFile : regionFolder.listFiles()) {
        converter.handleRegionFile(regionFile);
    }
}