Example usage for java.lang String substring

List of usage examples for java.lang String substring

Introduction

In this page you can find the example usage for java.lang String substring.

Prototype

public String substring(int beginIndex) 

Source Link

Document

Returns a string that is a substring of this string.

Usage

From source file:MainClass.java

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

    ParserGetter kit = new ParserGetter();
    HTMLEditorKit.Parser parser = kit.getParser();

    URL u = new URL("http://www.java2s.com");
    InputStream in = u.openStream();
    InputStreamReader r = new InputStreamReader(in);
    String remoteFileName = u.getFile();
    if (remoteFileName.endsWith("/")) {
        remoteFileName += "index.html";
    }// www  .ja v a2 s. co m
    if (remoteFileName.startsWith("/")) {
        remoteFileName = remoteFileName.substring(1);
    }
    File localDirectory = new File(u.getHost());
    while (remoteFileName.indexOf('/') > -1) {
        String part = remoteFileName.substring(0, remoteFileName.indexOf('/'));
        remoteFileName = remoteFileName.substring(remoteFileName.indexOf('/') + 1);
        localDirectory = new File(localDirectory, part);
    }
    if (localDirectory.mkdirs()) {
        File output = new File(localDirectory, remoteFileName);
        FileWriter out = new FileWriter(output);
        HTMLEditorKit.ParserCallback callback = new PageSaver(out, u);
        parser.parse(r, callback, false);
    }

}

From source file:com.gagein.crawler.Extractor.java

public static void main(String[] args) {
    String website = "http://www.gagein.com";
    String tempName = "temp";
    // ???//from  ww w.  j av a2 s.  c o m
    String dirPath = System.getProperty("user.dir") + File.separator + tempName + File.separator
            + website.substring(7).split("\\.")[1];// remove http://;
    Extractor ex = new Extractor();
    ex.getFilesPath(dirPath);
    System.out.println(ex.pathSet);
    ArticleStripper as = new ArticleStripper();
    as.setExtractAuthor(false);
    as.setExtractContent(true);
    as.setExtractDate(false);
    as.setExtractImg(false);
    as.setKeepContentLink(false);
    for (String url : ex.pathSet) {
        System.out.println(url);
        File file = new File(url);
        try {
            String page = FileUtils.readFileToString(file);
            System.out.println("Contents of file: " + page);
            logger.debug("==============?" + url + "?");
            Article article = as.retrieveArticle(null, null, page, null);
            System.out.println("##################" + article.getContent());

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

}

From source file:fr.romainf.QRCode.java

public static void main(String[] args) {
    Options options = buildOptions();/*from  www .  java 2  s  .  c  o m*/

    CommandLineParser parser = new BasicParser();

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

        int l = args.length;
        if (l != 1) {
            System.out.println("Can only encode one datum at a time (" + l + " given)");
            printUsage(options);
            System.exit(1);
        }
        if (cmd.hasOption("help")) {
            printUsage(options);
            System.exit(0);
        }

        String output = cmd.getOptionValue("o", DEFAULT_OUTPUT_FILE);
        String pixelColourText = cmd.getOptionValue("c", DEFAULT_PIXEL_COLOUR);
        if (pixelColourText.startsWith("0x")) {
            pixelColourText = pixelColourText.substring(2);
        }
        Color pixelColour;
        if (pixelColourText.length() == 6) {
            pixelColour = new Color(Integer.parseInt(pixelColourText, 16));
        } else {
            pixelColour = new Color((int) Long.parseLong(pixelColourText, 16), true);
        }

        writeQRCode(args[l - 1], output, pixelColour);

    } catch (ParseException e) {
        System.out.println(e.getMessage());
        printUsage(options);
        System.exit(1);
    } catch (WriterException e) {
        System.out.println("Could not create QRCode from data (" + e.getMessage() + ")");
        System.exit(2);
    } catch (IOException e) {
        System.out.println("Could not save QRCode to file (" + e.getMessage() + ")");
        System.exit(4);
    }
    System.exit(0);
}

From source file:ArraySet.java

public static void main(String[] args) throws java.io.IOException {
    ArraySet set = new ArraySet();
    java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
    while (true) {
        System.out.print(set.size() + ":"); //OK
        for (Iterator it = set.iterator(); it.hasNext();) {
            System.out.print(" " + it.next()); //OK
        }//www  . j ava  2s  .  c  o m
        System.out.println(); //OK
        System.out.print("> "); //OK
        String cmd = in.readLine();
        if (cmd == null)
            break;
        cmd = cmd.trim();
        if (cmd.equals("")) {
            ;
        } else if (cmd.startsWith("+")) {
            set.add(cmd.substring(1));
        } else if (cmd.startsWith("-")) {
            set.remove(cmd.substring(1));
        } else if (cmd.startsWith("?")) {
            boolean ret = set.contains(cmd.substring(1));
            System.out.println("  " + ret); //OK
        } else {
            System.out.println("unrecognized command"); //OK
        }
    }
}

From source file:cc.vidr.datum.tools.Console.java

public static void main(String[] args) throws IOException {
    System.out.print("Warming up... ");
    System.out.flush();//from   w  w  w  .  j a  va  2 s. c om
    QA.query("");
    System.out.println("Ready.");
    System.out.println("Ask me a question. Leave a blank line to exit.");
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    while (true) {
        int numServers = Server.getNumServers();
        int numFacts = Server.getNumFacts();
        System.out.print("> ");
        String q = in.readLine();
        if (q == null || q.isEmpty()) {
            System.out.println("Bye.");
            break;
        }
        Literal[] facts;
        if (q.startsWith("?-")) {
            try {
                Program program = new Program(q.substring(2));
                Clause[] query = program.parse();
                facts = Server.query(query[0].getHead());
            } catch (RecognitionException e) {
                facts = new Literal[0];
            }
        } else {
            facts = QA.query(q);
        }
        for (Literal fact : facts)
            printFact(fact, 0);
        if (facts.length == 0)
            System.out.println("I don't know.");
        if (DEBUG) {
            System.err.println((Server.getNumServers() - numServers) + " servers spawned");
            System.err.println((Server.getNumFacts() - numFacts) + " facts retrieved/generated");
        }
    }
}

From source file:GetWebPage.java

public static void main(String args[]) throws IOException, UnknownHostException {
    String resource, host, file;
    int slashPos;

    resource = "http://www.java2s.com/index.htm".substring(7); // skip HTTP://
    slashPos = resource.indexOf('/');
    if (slashPos < 0) {
        resource = resource + "/";
        slashPos = resource.indexOf('/');
    }/*from   w  w  w .  j a va2 s . c  o  m*/
    file = resource.substring(slashPos); // isolate host and file parts
    host = resource.substring(0, slashPos);
    System.out.println("Host to contact: '" + host + "'");
    System.out.println("File to fetch : '" + file + "'");
    MyHTTPConnection webConnection = new MyHTTPConnection(host);
    if (webConnection != null) {
        BufferedReader in = webConnection.get(file);
        String line;
        while ((line = in.readLine()) != null) { // read until EOF
            System.out.println(line);
        }
    }
    System.out.println("\nDone.");
}

From source file:it.alidays.mapengine.codegenerator.MapperEngineCodeGenerator.java

public static void main(String[] args) throws MapperEngineCodeGeneratorException {
    if (args.length != 2) {
        printUsage();/*w  ww  .  j ava2 s  .co m*/
    } else {
        String destinationDir = null;
        String[] engineDirectivesSources = null;

        for (String arg : args) {
            if (arg.startsWith("-d")) {
                destinationDir = arg.substring(2);
            } else if (arg.startsWith("-s")) {
                engineDirectivesSources = arg.substring(2).split("\\|");
            }
        }

        if (destinationDir == null || destinationDir.isEmpty() || engineDirectivesSources == null
                || engineDirectivesSources.length == 0) {
            printUsage();
        } else {
            for (String engineDirectivesSource : engineDirectivesSources) {
                run(new File(destinationDir), MapperEngineCodeGenerator.class.getClassLoader()
                        .getResourceAsStream(engineDirectivesSource));
            }
        }
    }
}

From source file:GetWebPageDemo.java

public static void main(String args[]) throws Exception {
    String resource, host, file;
    int slashPos;

    resource = "www.java2s.com/index.htm";
    slashPos = resource.indexOf('/'); // find host/file separator
    if (slashPos < 0) {
        resource = resource + "/";
        slashPos = resource.indexOf('/');
    }/*ww w.j ava  2 s . c om*/
    file = resource.substring(slashPos); // isolate host and file parts
    host = resource.substring(0, slashPos);
    System.out.println("Host to contact: '" + host + "'");
    System.out.println("File to fetch : '" + file + "'");

    SocketChannel channel = null;

    try {
        Charset charset = Charset.forName("ISO-8859-1");
        CharsetDecoder decoder = charset.newDecoder();
        CharsetEncoder encoder = charset.newEncoder();

        ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
        CharBuffer charBuffer = CharBuffer.allocate(1024);

        InetSocketAddress socketAddress = new InetSocketAddress(host, 80);
        channel = SocketChannel.open();
        channel.connect(socketAddress);

        String request = "GET " + file + " \r\n\r\n";
        channel.write(encoder.encode(CharBuffer.wrap(request)));

        while ((channel.read(buffer)) != -1) {
            buffer.flip();
            decoder.decode(buffer, charBuffer, false);
            charBuffer.flip();
            System.out.println(charBuffer);
            buffer.clear();
            charBuffer.clear();
        }
    } catch (UnknownHostException e) {
        System.err.println(e);
    } catch (IOException e) {
        System.err.println(e);
    } finally {
        if (channel != null) {
            try {
                channel.close();
            } catch (IOException ignored) {
            }
        }
    }

    System.out.println("\nDone.");
}

From source file:mlbench.pagerank.PagerankMerge.java

@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] args) throws IOException, InterruptedException {
    try {/*from   w  w w  .  j  a v  a2 s  .  c  o m*/
        parseArgs(args);
        HashMap<String, String> conf = new HashMap<String, String>();
        initConf(conf);
        MPI_D.Init(args, MPI_D.Mode.Common, conf);

        JobConf jobConf = new JobConf(confPath);
        if (MPI_D.COMM_BIPARTITE_O != null) {
            // O communicator
            int rank = MPI_D.Comm_rank(MPI_D.COMM_BIPARTITE_O);
            int size = MPI_D.Comm_size(MPI_D.COMM_BIPARTITE_O);
            if (rank == 0) {
                LOG.info(PagerankMerge.class.getSimpleName() + " O start.");
            }
            FileSplit[] inputs = DataMPIUtil.HDFSDataLocalLocator.getTaskInputs(MPI_D.COMM_BIPARTITE_O, jobConf,
                    inDir, rank);
            for (int i = 0; i < inputs.length; i++) {
                FileSplit fsplit = inputs[i];
                LineRecordReader kvrr = new LineRecordReader(jobConf, fsplit);

                LongWritable key = kvrr.createKey();
                Text value = kvrr.createValue();
                {
                    while (kvrr.next(key, value)) {
                        String line_text = value.toString();
                        final String[] line = line_text.split("\t");
                        if (line.length >= 2) {
                            MPI_D.Send(new IntWritable(Integer.parseInt(line[0])), new Text(line[1]));
                        }
                    }
                }
            }

        } else if (MPI_D.COMM_BIPARTITE_A != null) {
            // A communicator
            int rank = MPI_D.Comm_rank(MPI_D.COMM_BIPARTITE_A);
            if (rank == 0) {
                LOG.info(PagerankMerge.class.getSimpleName() + " A start.");
            }
            HadoopWriter<IntWritable, Text> outrw = HadoopIOUtil.getNewWriter(jobConf, outDir,
                    IntWritable.class, Text.class, TextOutputFormat.class, null, rank, MPI_D.COMM_BIPARTITE_A);

            IntWritable oldKey = null;
            double next_rank = 0;
            double previous_rank = 0;
            double diff = 0;
            int local_diffs = 0;
            random_coeff = (1 - mixing_c) / (double) number_nodes;
            converge_threshold = ((double) 1.0 / (double) number_nodes) / 10;
            Object[] keyValue = MPI_D.Recv();
            while (keyValue != null) {
                IntWritable key = (IntWritable) keyValue[0];
                Text value = (Text) keyValue[1];
                if (oldKey == null) {
                    oldKey = key;
                }
                if (!key.equals(oldKey)) {
                    next_rank = next_rank * mixing_c + random_coeff;
                    outrw.write(oldKey, new Text("v" + next_rank));
                    diff = Math.abs(previous_rank - next_rank);
                    if (diff > converge_threshold) {
                        local_diffs += 1;
                    }
                    oldKey = key;
                    next_rank = 0;
                    previous_rank = 0;
                }

                String cur_value_str = value.toString();
                if (cur_value_str.charAt(0) == 's') {
                    previous_rank = Double.parseDouble(cur_value_str.substring(1));
                } else {
                    next_rank += Double.parseDouble(cur_value_str.substring(1));
                }

                keyValue = MPI_D.Recv();
            }
            if (previous_rank != 0) {
                next_rank = next_rank * mixing_c + random_coeff;
                outrw.write(oldKey, new Text("v" + next_rank));
                diff = Math.abs(previous_rank - next_rank);
                if (diff > converge_threshold)
                    local_diffs += 1;
            }
            outrw.close();
            reduceDiffs(local_diffs, rank);
        }

        MPI_D.Finalize();
    } catch (MPI_D_Exception e) {
        e.printStackTrace();
    }
}

From source file:com.jwm123.loggly.reporter.AppLauncher.java

public static void main(String args[]) throws Exception {
    try {//from w ww. j a  v  a  2 s  .  c o  m
        CommandLine cl = parseCLI(args);
        try {
            config = new Configuration();
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("ERROR: Failed to read in persisted configuration.");
        }
        if (cl.hasOption("h")) {

            HelpFormatter help = new HelpFormatter();
            String jarName = AppLauncher.class.getProtectionDomain().getCodeSource().getLocation().getFile();
            if (jarName.contains("/")) {
                jarName = jarName.substring(jarName.lastIndexOf("/") + 1);
            }
            help.printHelp("java -jar " + jarName + " [options]", opts);
        }
        if (cl.hasOption("c")) {
            config.update();
        }
        if (cl.hasOption("q")) {
            Client client = new Client(config);
            client.setQuery(cl.getOptionValue("q"));
            if (cl.hasOption("from")) {
                client.setFrom(cl.getOptionValue("from"));
            }
            if (cl.hasOption("to")) {
                client.setTo(cl.getOptionValue("to"));
            }
            List<Map<String, Object>> report = client.getReport();

            if (report != null) {
                List<Map<String, String>> reportContent = new ArrayList<Map<String, String>>();
                ReportGenerator generator = null;
                if (cl.hasOption("file")) {
                    generator = new ReportGenerator(new File(cl.getOptionValue("file")));
                }
                byte reportFile[] = null;

                if (cl.hasOption("g")) {
                    System.out.println("Search results: " + report.size());
                    Set<Object> values = new TreeSet<Object>();
                    Map<Object, Integer> counts = new HashMap<Object, Integer>();
                    for (String groupBy : cl.getOptionValues("g")) {
                        for (Map<String, Object> result : report) {
                            if (mapContains(result, groupBy)) {
                                Object value = mapGet(result, groupBy);
                                values.add(value);
                                if (counts.containsKey(value)) {
                                    counts.put(value, counts.get(value) + 1);
                                } else {
                                    counts.put(value, 1);
                                }
                            }
                        }
                        System.out.println("For key: " + groupBy);
                        for (Object value : values) {
                            System.out.println("  " + value + ": " + counts.get(value));
                        }
                    }
                    if (cl.hasOption("file")) {
                        Map<String, String> reportAddition = new LinkedHashMap<String, String>();
                        reportAddition.put("Month", MONTH_FORMAT.format(new Date()));
                        reportContent.add(reportAddition);
                        for (Object value : values) {
                            reportAddition = new LinkedHashMap<String, String>();
                            reportAddition.put(value.toString(), "" + counts.get(value));
                            reportContent.add(reportAddition);
                        }
                        reportAddition = new LinkedHashMap<String, String>();
                        reportAddition.put("Total", "" + report.size());
                        reportContent.add(reportAddition);
                    }
                } else {
                    System.out.println("The Search [" + cl.getOptionValue("q") + "] yielded " + report.size()
                            + " results.");
                    if (cl.hasOption("file")) {
                        Map<String, String> reportAddition = new LinkedHashMap<String, String>();
                        reportAddition.put("Month", MONTH_FORMAT.format(new Date()));
                        reportContent.add(reportAddition);
                        reportAddition = new LinkedHashMap<String, String>();
                        reportAddition.put("Count", "" + report.size());
                        reportContent.add(reportAddition);
                    }
                }
                if (cl.hasOption("file")) {
                    reportFile = generator.build(reportContent);
                    File reportFileObj = new File(cl.getOptionValue("file"));
                    FileUtils.writeByteArrayToFile(reportFileObj, reportFile);
                    if (cl.hasOption("e")) {
                        ReportMailer mailer = new ReportMailer(config, cl.getOptionValues("e"),
                                cl.getOptionValue("s"), reportFileObj.getName(), reportFile);
                        mailer.send();
                    }
                }
            }
        }

    } catch (IllegalArgumentException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}