Example usage for java.io BufferedReader readLine

List of usage examples for java.io BufferedReader readLine

Introduction

In this page you can find the example usage for java.io BufferedReader readLine.

Prototype

public String readLine() throws IOException 

Source Link

Document

Reads a line of text.

Usage

From source file:Connect.java

public static void main(String[] args) {
    try { // Handle exceptions below
        // Get our command-line arguments
        String hostname = args[0];
        int port = Integer.parseInt(args[1]);
        String message = "";
        if (args.length > 2)
            for (int i = 2; i < args.length; i++)
                message += args[i] + " ";

        // Create a Socket connected to the specified host and port.
        Socket s = new Socket(hostname, port);

        // Get the socket output stream and wrap a PrintWriter around it
        PrintWriter out = new PrintWriter(s.getOutputStream());

        // Sent the specified message through the socket to the server.
        out.print(message + "\r\n");
        out.flush(); // Send it now.

        // Get an input stream from the socket and wrap a BufferedReader
        // around it, so we can read lines of text from the server.
        BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));

        // Before we start reading the server's response tell the socket
        // that we don't want to wait more than 3 seconds
        s.setSoTimeout(3000);/*from  w ww  . j  a  v a 2s. c om*/

        // Now read lines from the server until the server closes the
        // connection (and we get a null return indicating EOF) or until
        // the server is silent for 3 seconds.
        try {
            String line;
            while ((line = in.readLine()) != null)
                // If we get a line
                System.out.println(line); // print it out.
        } catch (SocketTimeoutException e) {
            // We end up here if readLine() times out.
            System.err.println("Timeout; no response from server.");
        }

        out.close(); // Close the output stream
        in.close(); // Close the input stream
        s.close(); // Close the socket
    } catch (IOException e) { // Handle IO and network exceptions here
        System.err.println(e);
    } catch (NumberFormatException e) { // Bad port number
        System.err.println("You must specify the port as a number");
    } catch (ArrayIndexOutOfBoundsException e) { // wrong # of args
        System.err.println("Usage: Connect <hostname> <port> message...");
    }
}

From source file:it.unimi.di.big.mg4j.document.TRECDocumentCollection.java

public static void main(final String[] arg) throws IOException, JSAPException, InstantiationException,
        IllegalAccessException, InvocationTargetException, NoSuchMethodException {

    SimpleJSAP jsap = new SimpleJSAP(TRECDocumentCollection.class.getName(),
            "Saves a serialised TREC document collection based on a set of file names (which will be sorted lexicographically).",
            new Parameter[] {
                    new FlaggedOption("factory", MG4JClassParser.getParser(),
                            IdentityDocumentFactory.class.getName(), JSAP.NOT_REQUIRED, 'f', "factory",
                            "A document factory with a standard constructor."),
                    new FlaggedOption("property", JSAP.STRING_PARSER, JSAP.NO_DEFAULT, JSAP.NOT_REQUIRED, 'p',
                            "property", "A 'key=value' specification, or the name of a property file")
                                    .setAllowMultipleDeclarations(true),
                    new Switch("gzipped", 'z', "gzipped", "The files are gzipped."),
                    new Switch("unsorted", 'u', "unsorted", "Keep the file list unsorted."),
                    new FlaggedOption("bufferSize", JSAP.INTSIZE_PARSER, DEFAULT_BUFFER_SIZE, JSAP.NOT_REQUIRED,
                            'b', "buffer-size", "The size of an I/O buffer."),
                    new UnflaggedOption("collection", JSAP.STRING_PARSER, JSAP.REQUIRED,
                            "The filename for the serialised collection."),
                    new UnflaggedOption("file", JSAP.STRING_PARSER, JSAP.NO_DEFAULT, JSAP.NOT_REQUIRED,
                            JSAP.GREEDY,
                            "A list of files that will be indexed. If missing, a list of files will be read from standard input.") });

    JSAPResult jsapResult = jsap.parse(arg);
    if (jsap.messagePrinted())
        return;/*from  ww w.j a  v a2 s  . c  om*/

    final DocumentFactory userFactory = PropertyBasedDocumentFactory.getInstance(jsapResult.getClass("factory"),
            jsapResult.getStringArray("property"));

    String[] file = jsapResult.getStringArray("file");
    if (file.length == 0) {
        final ObjectArrayList<String> files = new ObjectArrayList<String>();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String s;
        while ((s = bufferedReader.readLine()) != null)
            files.add(s);
        file = files.toArray(new String[0]);
    }

    // To avoid problems with find and similar utilities, we sort the file names
    if (!jsapResult.getBoolean("unsorted"))
        Arrays.sort(file);

    final DocumentFactory composite = CompositeDocumentFactory.getFactory(new TRECHeaderDocumentFactory(),
            userFactory);

    if (file.length == 0)
        System.err.println("WARNING: empty file set.");
    BinIO.storeObject(new TRECDocumentCollection(file, composite, jsapResult.getInt("bufferSize"),
            jsapResult.getBoolean("gzipped")), jsapResult.getString("collection"));
}

From source file:io.tempra.AppServer.java

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

    try {/*from w w  w  .  j ava 2s .com*/
        // String dir = getProperty("storageLocal");//
        // System.getProperty("user.home");
        String arch = "Win" + System.getProperty("sun.arch.data.model");
        System.out.println("sun.arch.data.model " + arch);

        // Sample to force java load dll or so on a filesystem
        // InputStream in = AppServer.class.getResourceAsStream("/dll/" +
        // arch + "/RechargeRPC.dll");
        //
        // File jCliSiTefI = new File(dir + "/" + "RechargeRPC.dll");
        // System.out.println("Writing dll to: " +
        // jCliSiTefI.getAbsolutePath());
        // OutputStream os = new FileOutputStream(jCliSiTefI);
        // byte[] buffer = new byte[4096];
        // int length;
        // while ((length = in.read(buffer)) > 0) {
        // os.write(buffer, 0, length);
        // }
        // os.close();
        // in.close();
        // System.load(jCliSiTefI.toString());
        // addLibraryPath(dir);

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

    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");

    Server jettyServer = new Server(Integer.parseInt(getProperty("localport")));
    // security configuration
    // FilterHolder holder = new FilterHolder(CrossOriginFilter.class);
    // holder.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM,
    // "*");
    // holder.setInitParameter("allowCredentials", "true");
    // holder.setInitParameter(
    // CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, "*");
    // holder.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM,
    // "GET,POST,HEAD");
    // holder.setInitParameter(CrossOriginFilter.ALLOWED_HEADERS_PARAM,
    // "X-Requested-With,Content-Type,Accept,Origin");
    // holder.setName("cross-origin");

    // context.addFilter(holder, fm);

    ResourceHandler resource_handler = new ResourceHandler();

    // add application on embedded server
    boolean servApp = true;
    boolean quioskMode = false;

    // if (System.getProperty("noServApp") != null )
    // if (System.getProperty("noServApp").equalsIgnoreCase("true"))
    // servApp = false;
    if (servApp) {
        //   ProtectionDomain domain = AppServer.class.getProtectionDomain();
        String webDir = AppServer.class.getResource("/webapp").toExternalForm();
        System.out.println("Jetty WEB DIR >>>>" + webDir);
        resource_handler.setDirectoriesListed(true);
        resource_handler.setWelcomeFiles(new String[] { "index.html" });
        resource_handler.setResourceBase(webDir); //
        // "C:/git/tsAgenciaVirtual/www/");
        // resource_handler.setResourceBase("C:/git/tsAgenciaVirtual/www/");

        // copyJarResourceToFolder((JarURLConnection) AppServer.class
        // .getResource("/app").openConnection(), createTempDir("app"));

        // resource_handler.setResourceBase(System
        // .getProperty("java.io.tmpdir") + "/app");
    }
    // sample to add rest services on container
    context.addServlet(FileUploadServlet.class, "/upload");
    ServletHolder jerseyServlet = context.addServlet(ServletContainer.class, "/services/*");
    jerseyServlet.setInitOrder(0);

    jerseyServlet.setInitParameter("jersey.config.server.provider.classnames",
            RestServices.class.getCanonicalName() + "," + RestServices.class.getCanonicalName());

    HandlerList handlers = new HandlerList();
    // context.addFilter(holder, "/*", EnumSet.of(DispatcherType.REQUEST));

    handlers.setHandlers(new Handler[] { resource_handler, context,

            // wscontext,
            new DefaultHandler() });
    jettyServer.setHandler(handlers);
    try {
        // Initialize javax.websocket layer
        ServerContainer wscontainer = WebSocketServerContainerInitializer.configureContext(context);
        wscontainer.setDefaultMaxSessionIdleTimeout(TimeUnit.HOURS.toMillis(1000));

        // Add WebSocket endpoint to javax.websocket layer
        wscontainer.addEndpoint(FileUpload.class);

    } catch (Throwable t) {
        t.printStackTrace(System.err);
    }

    // try {
    jettyServer.start();
    jettyServer.dump(System.err);

    if (servApp && quioskMode) {
        try {
            Process process = new ProcessBuilder(
                    "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe", "--kiosk",
                    "--kiosk-printing", "--auto", "--disable-pinch", "--incognito",
                    "--disable-session-crashed-bubble", "--overscroll-history-navigation=0",
                    "http://localhost:" + getProperty("localport")).start();
            // Process process =
            // Runtime.getRuntime().exec(" -kiosk http://localhost:8080");
            InputStream is = process.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line;

            System.out.printf("Output of running %s is:", Arrays.toString(args));
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        }

        catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
    // } finally {
    // jettyServer.destroy();
    // }//}

    jettyServer.join();
}

From source file:cmd.freebase2rdf.java

public static void main(String[] args) throws Exception {
    if (args.length != 2) {
        usage();//from   w  w  w.j a v  a2  s . c o m
    }
    File input = new File(args[0]);
    if (!input.exists())
        error("File " + input.getAbsolutePath() + " does not exist.");
    if (!input.canRead())
        error("Cannot read file " + input.getAbsolutePath());
    if (!input.isFile())
        error("Not a file " + input.getAbsolutePath());
    File output = new File(args[1]);
    if (output.exists())
        error("Output file " + output.getAbsolutePath()
                + " already exists, this program do not override existing files.");
    if (output.canWrite())
        error("Cannot write file " + output.getAbsolutePath());
    if (output.isDirectory())
        error("Not a file " + output.getAbsolutePath());
    if (!output.getName().endsWith(".nt.gz"))
        error("Output filename should end with .nt.gz, this is the only format supported.");

    BufferedReader in = new BufferedReader(
            new InputStreamReader(new BZip2CompressorInputStream(new FileInputStream(input))));
    BufferedOutputStream out = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(output)));
    String line;

    ProgressLogger progressLogger = new ProgressLogger(log, "lines", 100000, 1000000);
    progressLogger.start();
    Freebase2RDF freebase2rdf = null;
    try {
        freebase2rdf = new Freebase2RDF(out);
        while ((line = in.readLine()) != null) {
            freebase2rdf.send(line);
            progressLogger.tick();
        }
    } finally {
        if (freebase2rdf != null)
            freebase2rdf.close();
    }
    print(log, progressLogger);
}

From source file:com.gomoob.embedded.EmbeddedMongo.java

/**
 * Main entry of the program.//from w w w  .  j a  v a 2s  . c  om
 * 
 * @param args arguments used to customize starting.
 * 
 * @throws Exception If an error occured while executing the server.
 */
public static void main(String[] args) throws Exception {

    // Creates a default execution context, then the configuration of this context can be changed depending on the 
    // command line parameters received.
    context = new Context();

    // Parse the command line
    parseCommandLine(args);

    boolean terminated = false;

    System.out.println("MONGOD_HOST=" + context.getMongoContext().getNet().getServerAddress().getHostName());
    System.out.println("MONGOD_PORT=" + context.getMongoContext().getNet().getPort());
    System.out.println("SERVER_SOCKET_PORT=" + context.getSocketContext().getServerSocket().getLocalPort());

    Socket socket = null;
    BufferedReader reader = null;
    Writer writer = null;

    while (!terminated) {

        socket = context.getSocketContext().getServerSocket().accept();

        reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        writer = new OutputStreamWriter(new DataOutputStream(socket.getOutputStream()));

        System.out.println("Waiting for command...");
        String commandString = reader.readLine();
        System.out.println(commandString);

        ICommand command = parseCommandString(commandString);

        IResponse response = command.run(context);

        writer.write(response.toJSON());

        writer.close();

        // If the current socket is opened close it
        if (!socket.isClosed()) {
            socket.close();
        }

        // If stop is required
        terminated = response.isTerminationRequired();

    }

    context.getSocketContext().getServerSocket().close();
}

From source file:jackrabbit.app.App.java

public static void main(String[] args) {
    if (args.length == 0 || args.length == 1 && args[0].equals("-h")) {
        System.out.println("Usage: java -jar ackrabbit-migration-query-tool-" + VERSION
                + "-jar-with-dependencies.jar "
                + "--src src --src-conf conf [--src-repo-path path] [--src-user src_user] [--src-passwd src_pw] "
                + "[--dest-user dest_user] [--dest-passwd dest_pw] [--dest dest] [--dest-conf conf] [--dest-repo-path path] "
                + "[--cnd cnd] [--node-limit limit] " + "[--query-type type] [--query query]");
        System.out.println("\t --src source repository directory");
        System.out.println("\t --src-conf source repository configuration file");
        System.out.println("\t --src-repo-path path to source node to copy from; default is \"/\"");
        System.out.println("\t --src-user source repository login");
        System.out.println("\t --src-passwd source repository password");
        System.out.println("\t --dest destination repository directory");
        System.out.println("\t --dest-conf destination repository configuration file");
        System.out.println("\t --dest-repo-path path to destination node to copy to; default is \"/\"");
        System.out.println("\t --dest-user destination repository login");
        System.out.println("\t --dest-passwd destination repository password");
        System.out.println(//from  w ww  .j  a  v  a2 s. c  o  m
                "\t --node-limit size to partition nodes with before copying. If it is not supplied, no partitioning is performed");
        System.out.println("\t --cnd common node type definition file");
        System.out.println(
                "\t --query query to run in src. If --query is specified, then --dest, --dest-conf, --dest-repo-path and --cnd will be ignored.");
        System.out.println("\t --query-type query type (sql, xpath, JCR-SQL2); default is JCR-SQL2");
        return;
    }
    for (int i = 0; i < args.length; i = i + 2) {
        if (args[i].equals("--src") && i + 1 < args.length) {
            srcRepoDir = args[i + 1];
        } else if (args[i].equals("--dest") && i + 1 < args.length) {
            destRepoDir = args[i + 1];
        } else if (args[i].equals("--src-conf") && i + 1 < args.length) {
            srcConf = args[i + 1];
        } else if (args[i].equals("--dest-conf") && i + 1 < args.length) {
            destConf = args[i + 1];
        } else if (args[i].equals("--src-repo-path") && i + 1 < args.length) {
            srcRepoPath = args[i + 1];
        } else if (args[i].equals("--dest-repo-path") && i + 1 < args.length) {
            destRepoPath = args[i + 1];
        } else if (args[i].equals("--src-user") && i + 1 < args.length) {
            srcUser = args[i + 1];
        } else if (args[i].equals("--src-passwd") && i + 1 < args.length) {
            srcPasswd = args[i + 1];
        } else if (args[i].equals("--dest-user") && i + 1 < args.length) {
            destUser = args[i + 1];
        } else if (args[i].equals("--dest-passwd") && i + 1 < args.length) {
            destPasswd = args[i + 1];
        } else if (args[i].equals("--node-limit") && i + 1 < args.length) {
            nodeLimit = Long.parseLong(args[i + 1]);
        } else if (args[i].equals("--cnd") && i + 1 < args.length) {
            cndPath = args[i + 1];
        } else if (args[i].equals("--query") && i + 1 < args.length) {
            query = args[i + 1];
        } else if (args[i].equals("--query-type") && i + 1 < args.length) {
            queryType = args[i + 1];
        }
    }
    boolean missingArgs = false;

    if (srcRepoDir.isEmpty()) {
        missingArgs = true;
        log.error("Please specify the --src option.");
    }
    if (destRepoDir.isEmpty() && !destConf.isEmpty()) {
        missingArgs = true;
        log.error("Please specify the --dest option.");
    }
    if (srcConf.isEmpty()) {
        missingArgs = true;
        log.error("Please specify the --src-conf option.");
    }
    if (destConf.isEmpty() && !destRepoDir.isEmpty()) {
        missingArgs = true;
        log.error("Please specify the --dest-conf option.");
    }

    if (missingArgs)
        return;

    SimpleCredentials credentials = new SimpleCredentials(srcUser, srcPasswd.toCharArray());
    SimpleCredentials destCredentials = new SimpleCredentials(destUser, destPasswd.toCharArray());

    JackrabbitRepository dest = null;
    RepositoryFactory destRf = null;
    RepositoryFactory srcRf = new RepositoryFactoryImpl(srcConf, srcRepoDir);
    if (!destConf.isEmpty()) {
        destRf = new RepositoryFactoryImpl(destConf, destRepoDir);
    }

    try {
        final JackrabbitRepository src = srcRf.getRepository();
        SessionFactory srcSf = new SessionFactoryImpl(src, credentials);
        final Session srcSession = srcSf.getSession();
        Runtime.getRuntime().addShutdownHook(new Thread() {
            public void run() {
                srcSession.logout();
                src.shutdown();
            }
        });
        if (destConf.isEmpty()) {//query mode
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
            if (query.isEmpty()) {
                while (true) {
                    System.out.print(">");
                    String line = in.readLine();
                    if (line == null || line.isEmpty() || line.equalsIgnoreCase("quit")
                            || line.equalsIgnoreCase("exit")) {
                        break;
                    }
                    try {
                        runQuery(srcSession, line, queryType);
                    } catch (RepositoryException e) {
                        log.error(e.getMessage(), e);
                    }
                }
            } else {
                try {
                    runQuery(srcSession, query, queryType);
                } catch (RepositoryException e) {
                    log.error(e.getMessage(), e);
                }
            }
            return;
        }

        dest = destRf.getRepository();
        SessionFactory destSf = new SessionFactoryImpl(dest, destCredentials);
        Session destSession = destSf.getSession();

        try {
            RepositoryManager.registerCustomNodeTypes(destSession, cndPath);
            if (nodeLimit == 0)
                NodeCopier.copy(srcSession, destSession, srcRepoPath, destRepoPath, true);
            else
                NodeCopier.copy(srcSession, destSession, srcRepoPath, destRepoPath, nodeLimit, true);
            log.info("Copying " + srcSession.getWorkspace().getName() + " to "
                    + destSession.getWorkspace().getName() + " for " + srcRepoDir + " done.");
        } catch (ParseException e) {
            log.error(e.getMessage(), e);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        } catch (PathNotFoundException e) {
            log.error(e.getMessage(), e);
        } catch (RepositoryException e) {
            log.error(e.getMessage(), e);
        }

        List<String> destWkspaces = RepositoryManager.getDestinationWorkspaces(srcSession, destSession);

        for (String workspace : destWkspaces) {
            Session wsSession = srcSf.getSession(workspace);
            Session wsDestSession = destSf.getSession(workspace);
            try {
                RepositoryManager.registerCustomNodeTypes(wsDestSession, cndPath);
                if (nodeLimit == 0)
                    NodeCopier.copy(wsSession, wsDestSession, srcRepoPath, destRepoPath, true);
                else {
                    NodeCopier.copy(wsSession, wsDestSession, srcRepoPath, destRepoPath, nodeLimit, true);
                }
                log.info("Copying " + wsSession.getWorkspace().getName() + " to "
                        + wsDestSession.getWorkspace().getName() + " for " + srcRepoDir + " done.");
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            } catch (ParseException e) {
                log.error(e.getMessage(), e);
            } catch (PathNotFoundException e) {
                log.error(e.getMessage(), e);
            } catch (RepositoryException e) {
                log.error(e.getMessage(), e);
            } finally {
                wsSession.logout();
                wsDestSession.logout();
            }
        }
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    } catch (PathNotFoundException e) {
        log.error(e.getMessage(), e);
    } catch (RepositoryException e) {
        log.error(e.getMessage(), e);
    } finally {
        if (dest != null)
            dest.shutdown();
    }
}

From source file:com.twentyn.patentSearch.DocumentSearch.java

public static void main(String[] args) throws Exception {
    System.out.println("Starting up...");
    System.out.flush();//w w  w.  ja  v  a 2s .co  m
    Options opts = new Options();
    opts.addOption(Option.builder("x").longOpt("index").hasArg().required().desc("Path to index file to read")
            .build());
    opts.addOption(Option.builder("h").longOpt("help").desc("Print this help message and exit").build());
    opts.addOption(Option.builder("v").longOpt("verbose").desc("Print verbose log output").build());

    opts.addOption(Option.builder("f").longOpt("field").hasArg().desc("The indexed field to search").build());
    opts.addOption(
            Option.builder("q").longOpt("query").hasArg().desc("The query to use when searching").build());
    opts.addOption(Option.builder("l").longOpt("list-file").hasArg()
            .desc("A file containing a list of queries to run in sequence").build());
    opts.addOption(
            Option.builder("e").longOpt("enumerate").desc("Enumerate the documents in the index").build());
    opts.addOption(Option.builder("d").longOpt("dump").hasArg()
            .desc("Dump terms in the document index for a specified field").build());
    opts.addOption(
            Option.builder("o").longOpt("output").hasArg().desc("Write results JSON to this file.").build());
    opts.addOption(Option.builder("n").longOpt("inchi-field").hasArg()
            .desc("The index of the InChI field if an input TSV is specified.").build());
    opts.addOption(Option.builder("s").longOpt("synonym-field").hasArg()
            .desc("The index of the chemical synonym field if an input TSV is specified.").build());

    HelpFormatter helpFormatter = new HelpFormatter();
    CommandLineParser cmdLineParser = new DefaultParser();
    CommandLine cmdLine = null;
    try {
        cmdLine = cmdLineParser.parse(opts, args);
    } catch (ParseException e) {
        System.out.println("Caught exception when parsing command line: " + e.getMessage());
        helpFormatter.printHelp("DocumentIndexer", opts);
        System.exit(1);
    }

    if (cmdLine.hasOption("help")) {
        helpFormatter.printHelp("DocumentIndexer", opts);
        System.exit(0);
    }

    if (!(cmdLine.hasOption("enumerate") || cmdLine.hasOption("dump") || (cmdLine.hasOption("field")
            && (cmdLine.hasOption("query") || cmdLine.hasOption("list-file"))))) {
        System.out.println("Must specify one of 'enumerate', 'dump', or 'field' + {'query', 'list-file'}");
        helpFormatter.printHelp("DocumentIndexer", opts);
        System.exit(1);
    }

    if (cmdLine.hasOption("verbose")) {
        // With help from http://stackoverflow.com/questions/23434252/programmatically-change-log-level-in-log4j2
        LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
        Configuration ctxConfig = ctx.getConfiguration();
        LoggerConfig logConfig = ctxConfig.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
        logConfig.setLevel(Level.DEBUG);

        ctx.updateLoggers();
        LOGGER.debug("Verbose logging enabled");
    }

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
    objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);

    LOGGER.info("Opening index at " + cmdLine.getOptionValue("index"));

    try (Directory indexDir = FSDirectory.open(new File(cmdLine.getOptionValue("index")).toPath());
            IndexReader indexReader = DirectoryReader.open(indexDir);) {
        if (cmdLine.hasOption("enumerate")) {
            /* Enumerate all documents in the index.
             * With help from
             * http://stackoverflow.com/questions/2311845/is-it-possible-to-iterate-through-documents-stored-in-lucene-index
             */
            for (int i = 0; i < indexReader.maxDoc(); i++) {
                Document doc = indexReader.document(i);
                LOGGER.info("Doc " + i + ":");
                LOGGER.info(doc);
            }
        } else if (cmdLine.hasOption("dump")) {
            /* Dump indexed terms for a specific field.
             * With help from http://stackoverflow.com/questions/11148036/find-list-of-terms-indexed-by-lucene */
            Terms terms = SlowCompositeReaderWrapper.wrap(indexReader).terms(cmdLine.getOptionValue("dump"));
            LOGGER.info("Has positions: " + terms.hasPositions());
            LOGGER.info("Has offsets:   " + terms.hasOffsets());
            LOGGER.info("Has freqs:     " + terms.hasFreqs());
            LOGGER.info("Stats:         " + terms.getStats());
            LOGGER.info(terms);
            TermsEnum termsEnum = terms.iterator();
            BytesRef br = null;
            while ((br = termsEnum.next()) != null) {
                LOGGER.info("  " + br.utf8ToString());
            }

        } else {
            IndexSearcher searcher = new IndexSearcher(indexReader);
            String field = cmdLine.getOptionValue("field");

            List<Pair<String, String>> queries = null;
            if (cmdLine.hasOption("query")) {
                queries = Collections.singletonList(Pair.of("", cmdLine.getOptionValue("query")));
            } else if (cmdLine.hasOption("list-file")) {
                if (!(cmdLine.hasOption("inchi-field") && cmdLine.hasOption("synonym-field"))) {
                    LOGGER.error("Must specify both inchi-field and synonym-field when using list-file.");
                    System.exit(1);
                }
                Integer inchiField = Integer.parseInt(cmdLine.getOptionValue("inchi-field"));
                Integer synonymField = Integer.parseInt(cmdLine.getOptionValue("synonym-field"));

                queries = new LinkedList<>();
                BufferedReader r = new BufferedReader(new FileReader(cmdLine.getOptionValue("list-file")));
                String line;
                while ((line = r.readLine()) != null) {
                    line = line.trim();
                    if (!line.isEmpty()) {
                        // TODO: use a proper TSV reader; this is intentionally terrible as is.
                        String[] fields = line.split("\t");
                        queries.add(Pair.of(fields[inchiField].replace("\"", ""), fields[synonymField]));
                    }
                }
                r.close();
            }

            if (queries == null || queries.size() == 0) {
                LOGGER.error("Found no queries to run.");
                return;
            }

            List<SearchResult> searchResults = new ArrayList<>(queries.size());
            for (Pair<String, String> queryPair : queries) {
                String inchi = queryPair.getLeft();
                String rawQueryString = queryPair.getRight();
                /* The Lucene query parser interprets the kind of structural annotations we see in chemical entities
                 * as query directives, which is not what we want at all.  Phrase queries seem to work adequately
                 * with the analyzer we're currently using. */
                String queryString = rawQueryString.trim().toLowerCase();
                String[] parts = queryString.split("\\s+");
                PhraseQuery query = new PhraseQuery();
                for (String p : parts) {
                    query.add(new Term(field, p));
                }
                LOGGER.info("Running query: " + query.toString());

                BooleanQuery bq = new BooleanQuery();
                bq.add(query, BooleanClause.Occur.MUST);
                bq.add(new TermQuery(new Term(field, "yeast")), BooleanClause.Occur.SHOULD);
                bq.add(new TermQuery(new Term(field, "ferment")), BooleanClause.Occur.SHOULD);
                bq.add(new TermQuery(new Term(field, "fermentation")), BooleanClause.Occur.SHOULD);
                bq.add(new TermQuery(new Term(field, "fermentive")), BooleanClause.Occur.SHOULD);
                bq.add(new TermQuery(new Term(field, "saccharomyces")), BooleanClause.Occur.SHOULD);

                LOGGER.info("  Full query: " + bq.toString());

                TopDocs topDocs = searcher.search(bq, 100);
                ScoreDoc[] scoreDocs = topDocs.scoreDocs;
                if (scoreDocs.length == 0) {
                    LOGGER.info("Search returned no results.");
                }
                List<ResultDocument> results = new ArrayList<>(scoreDocs.length);
                for (int i = 0; i < scoreDocs.length; i++) {
                    ScoreDoc scoreDoc = scoreDocs[i];
                    Document doc = indexReader.document(scoreDoc.doc);
                    LOGGER.info("Doc " + i + ": " + scoreDoc.doc + ", score " + scoreDoc.score + ": "
                            + doc.get("id") + ", " + doc.get("title"));
                    results.add(new ResultDocument(scoreDoc.doc, scoreDoc.score, doc.get("title"),
                            doc.get("id"), null));
                }
                LOGGER.info("----- Done with query " + query.toString());
                // TODO: reduce memory usage when not writing results to an output file.
                searchResults.add(new SearchResult(inchi, rawQueryString, bq, results));
            }

            if (cmdLine.hasOption("output")) {
                try (FileWriter writer = new FileWriter(cmdLine.getOptionValue("output"));) {
                    writer.write(objectMapper.writeValueAsString(searchResults));
                }
            }
        }
    }
}

From source file:de.jetwick.snacktory.HtmlFetcher.java

public static void main(String[] args) throws Exception {
    BufferedReader reader = new BufferedReader(new FileReader("urls.txt"));
    String line = null;//  w  w w. j  a v  a  2  s.co  m
    Set<String> existing = new LinkedHashSet<String>();
    while ((line = reader.readLine()) != null) {
        int index1 = line.indexOf("\"");
        int index2 = line.indexOf("\"", index1 + 1);
        String url = line.substring(index1 + 1, index2);
        String domainStr = SHelper.extractDomain(url, true);
        String counterStr = "";
        // TODO more similarities
        if (existing.contains(domainStr))
            counterStr = "2";
        else
            existing.add(domainStr);

        String html = new HtmlFetcher().fetchAsString(url, 20000);
        String outFile = domainStr + counterStr + ".html";
        BufferedWriter writer = new BufferedWriter(new FileWriter(outFile));
        writer.write(html);
        writer.close();
    }
    reader.close();
}

From source file:akori.Impact.java

static public void main(String[] args) throws IOException {
    String PATH = "E:\\Trabajos\\AKORI\\datosmatrizgino\\";
    String PATHIMG = "E:\\NetBeansProjects\\AKORI\\Proccess_1\\ImagesPages\\";
    for (int i = 1; i <= 32; ++i) {
        for (int k = 1; k <= 15; ++k) {
            System.out.println("Matrix " + i + "-" + k);
            BufferedImage img = null;
            try {
                img = ImageIO.read(new File(PATHIMG + i + ".png"));
            } catch (IOException ex) {
                ex.getStackTrace();//from  w w  w .  ja va2  s .co  m
            }

            int ymax = img.getHeight();
            int xmax = img.getWidth();

            double[][] imagen = new double[ymax][xmax];

            BufferedReader in = null;
            try {
                in = new BufferedReader(new FileReader(PATH + i + "-" + k + ".txt"));
            } catch (FileNotFoundException ex) {
                ex.getStackTrace();
            }

            String linea;
            ArrayList<String> lista = new ArrayList<String>();
            HashMap<String, String> lista1 = new HashMap<String, String>();
            try {
                for (int j = 0; (linea = in.readLine()) != null; ++j) {
                    String[] datos = linea.split(",");
                    int x = (int) Double.parseDouble(datos[1]);
                    int y = (int) Double.parseDouble(datos[2]);
                    if (x >= xmax || y >= ymax || x <= 0 || y <= 0) {
                        continue;
                    }
                    lista.add(x + "," + y);
                }
            } catch (Exception ex) {
                ex.getStackTrace();
            }

            try {
                in.close();
            } catch (IOException ex) {
                ex.getStackTrace();
            }

            Iterator iter = lista.iterator();
            int[][] matrix = new int[lista.size()][2];

            for (int j = 0; iter.hasNext(); ++j) {
                String xy = (String) iter.next();
                String[] datos = xy.split(",");
                matrix[j][0] = Integer.parseInt(datos[0]);
                matrix[j][1] = Integer.parseInt(datos[1]);
            }

            for (int j = 0; j < matrix.length; ++j) {

                int std = 50;
                int x = matrix[j][0];
                int y = matrix[j][1];
                imagen[y][x] += 1;
                double aux;
                normalMatrix(imagen, y, x, std);

            }

            FileWriter fw = new FileWriter(PATH + "Matrix" + i + "-" + k + ".txt");
            BufferedWriter bw = new BufferedWriter(fw);
            for (int j = 0; j < imagen.length; ++j) {
                for (int t = 0; t < imagen[j].length; ++t) {
                    if (t + 1 == imagen[j].length)
                        bw.write(imagen[j][t] + "");
                    else
                        bw.write(imagen[j][t] + ",");
                }
                bw.write("\n");
            }
            bw.close();
        }
    }
}

From source file:edu.cmu.lti.oaqa.knn4qa.apps.FilterVocabulary.java

public static void main(String[] args) {
    String optKeys[] = { IN_VOC_FILE_PARAM, OUT_VOC_FILE_PARAM, CommonParams.MEM_FWD_INDEX_PARAM,
            CommonParams.MAX_WORD_QTY_PARAM };
    String optDescs[] = { IN_VOC_FILE_DESC, OUT_VOC_FILE_DESC, CommonParams.MEM_FWD_INDEX_DESC,
            CommonParams.MAX_WORD_QTY_DESC };
    boolean hasArg[] = { true, true, true, true };

    ParamHelper mParamHelper = null;/*from  w w w .ja v a  2 s  . c o m*/

    try {

        mParamHelper = new ParamHelper(args, optKeys, optDescs, hasArg);

        CommandLine cmd = mParamHelper.getCommandLine();

        String outputFile = cmd.getOptionValue(OUT_VOC_FILE_PARAM);
        if (null == outputFile) {
            UsageSpecify(OUT_VOC_FILE_DESC, mParamHelper.getOptions());
        }

        String inputFile = cmd.getOptionValue(IN_VOC_FILE_PARAM);
        if (null == inputFile) {
            UsageSpecify(IN_VOC_FILE_DESC, mParamHelper.getOptions());
        }

        int maxWordQty = Integer.MAX_VALUE;

        String tmpi = cmd.getOptionValue(CommonParams.MAX_WORD_QTY_PARAM);

        if (null != tmpi) {
            maxWordQty = Integer.parseInt(tmpi);
        }

        String memFwdIndxName = cmd.getOptionValue(CommonParams.MEM_FWD_INDEX_PARAM);
        if (null == memFwdIndxName) {
            UsageSpecify(CommonParams.MEM_FWD_INDEX_DESC, mParamHelper.getOptions());
        }

        VocabularyFilterAndRecoder filter = new FrequentIndexWordFilterAndRecoder(memFwdIndxName, maxWordQty);

        BufferedReader finp = new BufferedReader(
                new InputStreamReader(CompressUtils.createInputStream(inputFile)));
        BufferedWriter fout = new BufferedWriter(
                new OutputStreamWriter(CompressUtils.createOutputStream(outputFile)));
        try {

            String line;

            int wordQty = 0;
            long addedQty = 0;
            long totalQty = 0;

            for (totalQty = 0; (line = finp.readLine()) != null;) {
                ++totalQty;
                // Skip empty lines
                line = line.trim();
                if (line.isEmpty())
                    continue;

                GizaVocRec rec = new GizaVocRec(line);

                if (filter.checkWord(rec.mWord)) {
                    rec.save(fout);
                    addedQty++;
                }

                if (totalQty % REPORT_INTERVAL_QTY == 0)
                    System.out.println(String.format(
                            "Processed %d lines (%d source word entries) from '%s', added %d lines", totalQty,
                            wordQty, inputFile, addedQty));
            }

        } finally {
            finp.close();
            fout.close();
        }
    } catch (ParseException e) {
        Usage("Cannot parse arguments", mParamHelper != null ? mParamHelper.getOptions() : null);
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
        System.err.println("Terminating due to an exception: " + e);
        System.exit(1);
    }
}