Example usage for java.util Scanner Scanner

List of usage examples for java.util Scanner Scanner

Introduction

In this page you can find the example usage for java.util Scanner Scanner.

Prototype

public Scanner(ReadableByteChannel source, Charset charset) 

Source Link

Document

Constructs a new Scanner that produces values scanned from the specified channel.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Scanner scanner = new Scanner(new File("c:/text.txt"), StandardCharsets.UTF_8.name());
    System.out.println(scanner.nextLine());

    scanner.close();/*from ww  w. j a va 2  s . com*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Scanner scanner = new Scanner(new FileInputStream("c:/text.txt"), StandardCharsets.UTF_8.name());
    System.out.println(scanner.nextLine());

    scanner.close();/*from  w  w  w .j a  v a 2  s . c  o m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Scanner scanner = new Scanner(FileSystems.getDefault().getPath("logs", "access.log"),
            StandardCharsets.UTF_8.name());
    System.out.println(scanner.nextLine());

    scanner.close();/*from  ww  w  .ja  v  a 2s .c o m*/
}

From source file:Main.java

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

    Scanner scanner = new Scanner(new FileInputStream("c:/text.txt").getChannel(),
            StandardCharsets.UTF_8.name());

    System.out.println(scanner.nextLine());

    // change the radix of the scanner
    scanner.useRadix(32);//w w  w  . j a v a  2  s.c  om

    // display the new radix
    System.out.println(scanner.radix());

    scanner.close();
}

From source file:de.taimos.dvalin.interconnect.model.MessageCryptoUtil.java

/**
 * start this to generate an AES key or (de/en)crypt data
 *
 * @param args the CLI arguments/*  w w w  .  j a v  a  2  s. co  m*/
 */
@SuppressWarnings("resource")
public static void main(String[] args) {
    try {
        System.out.println("Select (k=generate key; c=crypt; d=decrypt):");
        System.out.println();
        Scanner scan = new Scanner(System.in, "UTF-8");
        if (!scan.hasNextLine()) {
            return;
        }
        switch (scan.nextLine()) {
        case "k":
            MessageCryptoUtil.generateKey();
            break;
        case "c":
            System.out.print("Input data: ");
            final String data = scan.nextLine();
            System.out.println(MessageCryptoUtil.crypt(data));
            break;
        case "d":
            System.out.print("Input data: ");
            final String ddata = scan.nextLine();
            System.out.println(MessageCryptoUtil.decrypt(ddata));
            break;
        default:
            break;
        }
    } catch (final Exception e) {
        e.printStackTrace();
    }
}

From source file:com.joliciel.talismane.terminology.TalismaneTermExtractorMain.java

public static void main(String[] args) throws Exception {
    String termFilePath = null;//from w  ww  .  j  a  va2 s.  com
    String outFilePath = null;
    Command command = Command.extract;
    int depth = -1;
    String databasePropertiesPath = null;
    String projectCode = null;
    String terminologyPropertiesPath = null;

    Map<String, String> argMap = StringUtils.convertArgs(args);

    String logConfigPath = argMap.get("logConfigFile");
    if (logConfigPath != null) {
        argMap.remove("logConfigFile");
        Properties props = new Properties();
        props.load(new FileInputStream(logConfigPath));
        PropertyConfigurator.configure(props);
    }

    Map<String, String> innerArgs = new HashMap<String, String>();
    for (Entry<String, String> argEntry : argMap.entrySet()) {
        String argName = argEntry.getKey();
        String argValue = argEntry.getValue();

        if (argName.equals("command"))
            command = Command.valueOf(argValue);
        else if (argName.equals("termFile"))
            termFilePath = argValue;
        else if (argName.equals("outFile"))
            outFilePath = argValue;
        else if (argName.equals("depth"))
            depth = Integer.parseInt(argValue);
        else if (argName.equals("databaseProperties"))
            databasePropertiesPath = argValue;
        else if (argName.equals("terminologyProperties"))
            terminologyPropertiesPath = argValue;
        else if (argName.equals("projectCode"))
            projectCode = argValue;
        else
            innerArgs.put(argName, argValue);
    }
    if (termFilePath == null && databasePropertiesPath == null)
        throw new TalismaneException("Required argument: termFile or databasePropertiesPath");

    if (termFilePath != null) {
        String currentDirPath = System.getProperty("user.dir");
        File termFileDir = new File(currentDirPath);
        if (termFilePath.lastIndexOf("/") >= 0) {
            String termFileDirPath = termFilePath.substring(0, termFilePath.lastIndexOf("/"));
            termFileDir = new File(termFileDirPath);
            termFileDir.mkdirs();
        }
    }

    long startTime = new Date().getTime();
    try {
        if (command.equals(Command.analyse)) {
            innerArgs.put("command", "analyse");
        } else {
            innerArgs.put("command", "process");
        }

        String sessionId = "";
        TalismaneServiceLocator locator = TalismaneServiceLocator.getInstance(sessionId);
        TalismaneService talismaneService = locator.getTalismaneService();

        TalismaneConfig config = talismaneService.getTalismaneConfig(innerArgs, sessionId);

        TerminologyServiceLocator terminologyServiceLocator = TerminologyServiceLocator.getInstance(locator);
        TerminologyService terminologyService = terminologyServiceLocator.getTerminologyService();
        TerminologyBase terminologyBase = null;

        if (projectCode == null)
            throw new TalismaneException("Required argument: projectCode");

        File file = new File(databasePropertiesPath);
        FileInputStream fis = new FileInputStream(file);
        Properties dataSourceProperties = new Properties();
        dataSourceProperties.load(fis);
        terminologyBase = terminologyService.getPostGresTerminologyBase(projectCode, dataSourceProperties);

        TalismaneSession talismaneSession = talismaneService.getTalismaneSession();

        if (command.equals(Command.analyse) || command.equals(Command.extract)) {
            Locale locale = talismaneSession.getLocale();
            Map<TerminologyProperty, String> terminologyProperties = new HashMap<TerminologyProperty, String>();
            if (terminologyPropertiesPath != null) {
                Map<String, String> terminologyPropertiesStr = StringUtils.getArgMap(terminologyPropertiesPath);
                for (String key : terminologyPropertiesStr.keySet()) {
                    try {
                        TerminologyProperty property = TerminologyProperty.valueOf(key);
                        terminologyProperties.put(property, terminologyPropertiesStr.get(key));
                    } catch (IllegalArgumentException e) {
                        throw new TalismaneException("Unknown terminology property: " + key);
                    }
                }
            } else {
                terminologyProperties = getDefaultTerminologyProperties(locale);
            }
            if (depth <= 0 && !terminologyProperties.containsKey(TerminologyProperty.maxDepth))
                throw new TalismaneException("Required argument: depth");

            InputStream regexInputStream = getInputStreamFromResource(
                    "parser_conll_with_location_input_regex.txt");
            Scanner regexScanner = new Scanner(regexInputStream, "UTF-8");
            String inputRegex = regexScanner.nextLine();
            regexScanner.close();
            config.setInputRegex(inputRegex);

            Charset outputCharset = config.getOutputCharset();

            TermExtractor termExtractor = terminologyService.getTermExtractor(terminologyBase,
                    terminologyProperties);
            if (depth > 0)
                termExtractor.setMaxDepth(depth);
            termExtractor.setOutFilePath(termFilePath);

            if (outFilePath != null) {
                if (outFilePath.lastIndexOf("/") >= 0) {
                    String outFileDirPath = outFilePath.substring(0, outFilePath.lastIndexOf("/"));
                    File outFileDir = new File(outFileDirPath);
                    outFileDir.mkdirs();
                }
                File outFile = new File(outFilePath);
                outFile.delete();
                outFile.createNewFile();

                Writer writer = new BufferedWriter(
                        new OutputStreamWriter(new FileOutputStream(outFilePath), outputCharset));
                TermAnalysisWriter termAnalysisWriter = new TermAnalysisWriter(writer);
                termExtractor.addTermObserver(termAnalysisWriter);
            }

            Talismane talismane = config.getTalismane();
            talismane.setParseConfigurationProcessor(termExtractor);
            talismane.process();
        } else if (command.equals(Command.list)) {

            List<Term> terms = terminologyBase.findTerms(2, null, 0, null, null);
            for (Term term : terms) {
                LOG.debug("Term: " + term.getText());
                LOG.debug("Frequency: " + term.getFrequency());
                LOG.debug("Heads: " + term.getHeads());
                LOG.debug("Expansions: " + term.getExpansions());
                LOG.debug("Contexts: " + term.getContexts());
            }
        }
    } finally {
        long endTime = new Date().getTime();
        long totalTime = endTime - startTime;
        LOG.info("Total time: " + totalTime);
    }
}

From source file:com.joliciel.talismane.fr.TalismaneFrench.java

/**
 * @param args/*w  w w.j av a2s . co m*/
 * @throws Exception 
 */
public static void main(String[] args) throws Exception {
    Map<String, String> argsMap = TalismaneConfig.convertArgs(args);
    CorpusFormat corpusReaderType = null;
    String treebankDirPath = null;
    boolean keepCompoundPosTags = true;

    if (argsMap.containsKey("corpusReader")) {
        corpusReaderType = CorpusFormat.valueOf(argsMap.get("corpusReader"));
        argsMap.remove("corpusReader");
    }
    if (argsMap.containsKey("treebankDir")) {
        treebankDirPath = argsMap.get("treebankDir");
        argsMap.remove("treebankDir");
    }
    if (argsMap.containsKey("keepCompoundPosTags")) {
        keepCompoundPosTags = argsMap.get("keepCompoundPosTags").equalsIgnoreCase("true");
        argsMap.remove("keepCompoundPosTags");
    }

    Extensions extensions = new Extensions();
    extensions.pluckParameters(argsMap);

    TalismaneFrench talismaneFrench = new TalismaneFrench();

    TalismaneConfig config = new TalismaneConfig(argsMap, talismaneFrench);
    if (config.getCommand() == null)
        return;

    if (corpusReaderType != null) {
        if (corpusReaderType == CorpusFormat.ftbDep) {
            File inputFile = new File(config.getInFilePath());
            FtbDepReader ftbDepReader = new FtbDepReader(inputFile, config.getInputCharset());

            ftbDepReader.setKeepCompoundPosTags(keepCompoundPosTags);
            ftbDepReader.setPredictTransitions(config.isPredictTransitions());

            config.setParserCorpusReader(ftbDepReader);
            config.setPosTagCorpusReader(ftbDepReader);
            config.setTokenCorpusReader(ftbDepReader);

            if (config.getCommand().equals(Command.compare)) {
                File evaluationFile = new File(config.getEvaluationFilePath());
                FtbDepReader ftbDepEvaluationReader = new FtbDepReader(evaluationFile,
                        config.getInputCharset());
                ftbDepEvaluationReader.setKeepCompoundPosTags(keepCompoundPosTags);
                config.setParserEvaluationCorpusReader(ftbDepEvaluationReader);
                config.setPosTagEvaluationCorpusReader(ftbDepEvaluationReader);
            }
        } else if (corpusReaderType == CorpusFormat.ftb) {
            TalismaneServiceLocator talismaneServiceLocator = TalismaneServiceLocator.getInstance();
            TreebankServiceLocator treebankServiceLocator = TreebankServiceLocator
                    .getInstance(talismaneServiceLocator);
            TreebankUploadService treebankUploadService = treebankServiceLocator
                    .getTreebankUploadServiceLocator().getTreebankUploadService();
            TreebankExportService treebankExportService = treebankServiceLocator
                    .getTreebankExportServiceLocator().getTreebankExportService();
            File treebankFile = new File(treebankDirPath);
            TreebankReader treebankReader = treebankUploadService.getXmlReader(treebankFile);

            // we prepare both the tokeniser and pos-tag readers, just in case they are needed
            InputStream posTagMapStream = talismaneFrench.getDefaultPosTagMapFromStream();
            Scanner scanner = new Scanner(posTagMapStream, "UTF-8");
            List<String> descriptors = new ArrayList<String>();
            while (scanner.hasNextLine())
                descriptors.add(scanner.nextLine());
            FtbPosTagMapper ftbPosTagMapper = treebankExportService.getFtbPosTagMapper(descriptors,
                    talismaneFrench.getDefaultPosTagSet());
            PosTagAnnotatedCorpusReader posTagAnnotatedCorpusReader = treebankExportService
                    .getPosTagAnnotatedCorpusReader(treebankReader, ftbPosTagMapper, keepCompoundPosTags);
            config.setPosTagCorpusReader(posTagAnnotatedCorpusReader);

            TokeniserAnnotatedCorpusReader tokenCorpusReader = treebankExportService
                    .getTokeniserAnnotatedCorpusReader(treebankReader, ftbPosTagMapper, keepCompoundPosTags);
            config.setTokenCorpusReader(tokenCorpusReader);

            SentenceDetectorAnnotatedCorpusReader sentenceCorpusReader = treebankExportService
                    .getSentenceDetectorAnnotatedCorpusReader(treebankReader);
            config.setSentenceCorpusReader(sentenceCorpusReader);
        } else if (corpusReaderType == CorpusFormat.conll || corpusReaderType == CorpusFormat.spmrl) {
            File inputFile = new File(config.getInFilePath());

            ParserRegexBasedCorpusReader corpusReader = config.getParserService()
                    .getRegexBasedCorpusReader(inputFile, config.getInputCharset());

            corpusReader.setPredictTransitions(config.isPredictTransitions());

            config.setParserCorpusReader(corpusReader);
            config.setPosTagCorpusReader(corpusReader);
            config.setTokenCorpusReader(corpusReader);

            if (corpusReaderType == CorpusFormat.spmrl) {
                corpusReader.setRegex(
                        "%INDEX%\\t%TOKEN%\\t.*\\t.*\\t%POSTAG%\\t.*\\t.*\\t.*\\t%GOVERNOR%\\t%LABEL%");
            }

            if (config.getCommand().equals(Command.compare)) {
                File evaluationFile = new File(config.getEvaluationFilePath());
                ParserRegexBasedCorpusReader evaluationReader = config.getParserService()
                        .getRegexBasedCorpusReader(evaluationFile, config.getInputCharset());
                config.setParserEvaluationCorpusReader(evaluationReader);
                config.setPosTagEvaluationCorpusReader(evaluationReader);
            }
        } else {
            throw new TalismaneException("Unknown corpusReader: " + corpusReaderType);
        }
    }
    Talismane talismane = config.getTalismane();

    extensions.prepareCommand(config, talismane);

    talismane.process();
}

From source file:Main.java

static String convertStreamToString(InputStream is) {
    Scanner s = new Scanner(is, "utf-8").useDelimiter("\\A");
    return s.hasNext() ? s.next() : "";
}

From source file:Main.java

private static String streamToString(InputStream inputStream) {
    String text = new Scanner(inputStream, "UTF-8").useDelimiter("\\Z").next();
    return text;/* w  ww  . j  a  v a  2 s .c o m*/
}

From source file:Main.java

private static StringReader fromStreamToStringReader(InputStream inputStream, String encoding) {
    StringBuilder content = new StringBuilder();

    Scanner scanner = null;/*from   w w  w.j av  a 2s  .co m*/
    try {
        scanner = new Scanner(inputStream, encoding);

        while (scanner.hasNextLine()) {
            content.append(scanner.nextLine()).append("\n");
        }
    } finally {
        if (scanner != null) {
            scanner.close();
        }
    }

    return new StringReader(content.toString());
}