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) 

Source Link

Document

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

Usage

From source file:com.joliciel.talismane.extensions.standoff.ConllFileSplitter.java

/**
 * @param args/*w  w  w. j  a  v a2s . c o  m*/
 * @throws IOException 
 * @throws UnsupportedEncodingException 
 */
public void split(String filePath, int startIndex, int sentencesPerFile, String encoding) {
    try {
        String fileBase = filePath;
        if (filePath.indexOf('.') > 0)
            fileBase = filePath.substring(0, filePath.lastIndexOf('.'));
        File file = new File(filePath);
        Scanner scanner = new Scanner(
                new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding)));

        boolean hasSentence = false;
        int currentFileIndex = startIndex;

        int sentenceCount = 0;
        Writer writer = null;
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            if (line.length() == 0 && !hasSentence) {
                continue;
            } else if (line.length() == 0) {
                writer.write("\n");
                writer.flush();
                hasSentence = false;
            } else {
                if (!hasSentence) {
                    hasSentence = true;
                    sentenceCount++;
                }
                if (writer == null || sentenceCount % sentencesPerFile == 0) {
                    if (writer != null) {
                        writer.flush();
                        writer.close();
                    }
                    File outFile = new File(fileBase + "_" + df.format(currentFileIndex) + ".tal");
                    outFile.delete();
                    outFile.createNewFile();
                    writer = new BufferedWriter(
                            new OutputStreamWriter(new FileOutputStream(outFile, false), encoding));
                    currentFileIndex++;
                    hasSentence = false;
                }

                writer.write(line + "\n");
                writer.flush();
            }
        }
        if (writer != null) {
            writer.flush();
            writer.close();
        }
    } catch (IOException e) {
        LogUtils.logError(LOG, e);
        throw new RuntimeException(e);
    }
}

From source file:com.acme.TopTagsTupleTest.java

@Test
public void tags() throws IOException {

    final Broadcaster<Object> broadcaster = SerializedBroadcaster.create();

    Processor processor = new TopTags(1, 10);
    Stream<?> outputStream = processor.process(broadcaster);

    outputStream.consume(new Consumer<Object>() {
        @Override// w w  w .j a v a  2  s .co  m
        public void accept(Object o) {
            System.out.println("processed : " + o);
        }
        //TODO - expect
        //            processed : {"id":"55786760-7472-065d-8e62-eb83260948a4","timestamp":1422399628134,"hashtag":"AndroidGames","count":1}
        //            processed : {"id":"bd99050f-abfa-a239-c09a-f2fe721daafb","timestamp":1422399628182,"hashtag":"Android","count":1}
        //            processed : {"id":"10ce993c-fd57-322d-efa1-16f810918187","timestamp":1422399628184,"hashtag":"GameInsight","count":1}
    });

    ClassPathResource resource = new ClassPathResource("tweets.json");
    Scanner scanner = new Scanner(resource.getInputStream());
    while (scanner.hasNext()) {
        String tweet = scanner.nextLine();
        broadcaster.onNext(tweet);
        //simulateLatency();
    }
    //System.in.read();

}

From source file:com.thalesgroup.sonar.plugins.tusar.utils.Utils.java

/**
 * Parse a csv file containing user metrics information (name, type, domain)
 * @param csvFile The path of the initialisationFile
 * @return a list of String[] which contains the name of the metrics, its type and its domain
 * @throws FileNotFoundException//from   w  w w  . j  a va 2 s .  com
 */
public static List<String[]> parseIniFile(File csvFile) throws FileNotFoundException {
    List<String[]> metrics = new ArrayList<String[]>();
    if (!csvFile.getName().endsWith(Constants.CSV_EXTENSION)) {
        logger.warn(csvFile + " is not a CSVFile");
        return metrics;
    }
    Scanner scanner = new Scanner(csvFile);
    scanIniContent(scanner, metrics);
    return metrics;
}

From source file:isbnfilter.ISBNFilter.java

/**
 * This Function reads isbn.txt file and creates every single isbn to a
 * CBook object/*from   w w w. ja  va 2s .  c  o  m*/
 *
 * @param parent stage where is beeing called
 * @return strng whit isbn separated by commas
 * @throws FileNotFoundException
 * @throws IOException
 */
public List<String[]> readFile(Stage parent) throws FileNotFoundException, IOException {
    FileChooser fileChooser = new FileChooser();
    String[] separated = null;
    fileChooser.setTitle("Open Resource File");
    File selectedFile = fileChooser.showOpenDialog(parent);
    List<String[]> everything = new ArrayList();
    CSVParser parser = new CSVParser(',', '"');
    String[] parsedLine;

    /*CSVReader reader = null;
    try
    {
    //Get the CSVReader instance with specifying the delimiter to be used
    reader = new CSVReader(new FileReader(selectedFile),',','"');
    String [] nextLine;
    //Read one line at a time
    while ((nextLine = reader.readNext()) != null) 
    {
        everything.add(nextLine);
    }
    }
    catch (Exception e) {
    e.printStackTrace();
    }
    finally {
    try {
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    }*/

    if (selectedFile != null) {
        Scanner scanner = new Scanner(selectedFile);
        BufferedReader br = new BufferedReader(new FileReader(selectedFile.getAbsoluteFile()));
        try {
            StringBuilder sb = new StringBuilder();
            br.readLine();
            String line = br.readLine();
            while (line != null) {
                if (line.charAt(0) == '"') //Algunos traen " al inicio.. los que traen, se los quito   
                    line = line.substring(1);

                separated = line.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
                //separated = line.split(","); //Separo todos los datos

                everything.add(separated);
                line = br.readLine();
            }
            //everything = sb.toString();
        } finally {
            br.close();
        }

    }

    return everything;

}

From source file:de.bley.word.menu.Menue.java

/**
 * Die Oberflaeche startet in der Console.
 *///from ww w.j  a v  a  2  s  .  c  om
@Override
public void showMenue() {
    zuordnung = PropertieManager.getInstance().getZuordnung();
    boolean run = true;

    final Scanner scan = new Scanner(System.in);
    while (run) {

        try {
            showFiles(scan);

            zuordnung.getEingabe().readFile();
            final int input = scan.nextInt();

            switch (input) {
            case 0:
                readFile();
                break;
            case 1:
                writeFrile(scan);
                break;
            default:
                run = false;
                break;
            }
        } catch (Exception e) {
            run = false;

        }

    }
}

From source file:com.hortonworks.registries.storage.tool.shell.ShellMigrationExecutor.java

@Override
public void execute(Connection connection) throws SQLException {
    String scriptLocation = this.shellScriptResource.getLocationOnDisk();
    try {// www . ja v a  2  s  .co  m
        List<String> args = new ArrayList<String>();
        args.add(scriptLocation);
        ProcessBuilder builder = new ProcessBuilder(args);
        builder.redirectErrorStream(true);
        Process process = builder.start();
        Scanner in = new Scanner(process.getInputStream());
        System.out.println(StringUtils.repeat("+", 200));
        while (in.hasNextLine()) {
            System.out.println(in.nextLine());
        }
        int returnCode = process.waitFor();
        System.out.println(StringUtils.repeat("+", 200));
        if (returnCode != 0) {
            throw new FlywayException("script exited with value : " + returnCode);
        }
    } catch (Exception e) {
        LOG.error(e.toString());
        // Only if SQLException or FlywaySqlScriptException is thrown flyway will mark the migration as failed in the metadata table
        throw new SQLException(String.format("Failed to run script \"%s\", %s", scriptLocation, e.getMessage()),
                e);
    }
}

From source file:com.liferay.ide.project.core.modules.BladeCLI.java

public static String[] execute(String args) throws BladeCLIException {
    IPath bladeCLIPath = getBladeCLIPath();

    if (FileUtil.notExists(bladeCLIPath)) {
        throw new BladeCLIException("Could not get blade cli jar.");
    }/*from w w w  .  j a  v a 2s  .co m*/

    Project project = new Project();
    Java javaTask = new Java();

    javaTask.setProject(project);
    javaTask.setFork(true);
    javaTask.setFailonerror(true);
    javaTask.setJar(bladeCLIPath.toFile());
    javaTask.setArgs(args);

    DefaultLogger logger = new DefaultLogger();

    project.addBuildListener(logger);
    List<String> lines = new ArrayList<>();
    int returnCode = 0;

    try (StringBufferOutputStream out = new StringBufferOutputStream();
            PrintStream printStream = new PrintStream(out)) {
        logger.setOutputPrintStream(printStream);

        logger.setMessageOutputLevel(Project.MSG_INFO);

        returnCode = javaTask.executeJava();

        try (Scanner scanner = new Scanner(out.toString())) {
            while (scanner.hasNextLine()) {
                lines.add(scanner.nextLine().replaceAll(".*\\[null\\] ", ""));
            }
        }

        boolean hasErrors = false;

        StringBuilder errors = new StringBuilder();

        for (String line : lines) {
            if (line.startsWith("Error")) {
                hasErrors = true;
            } else if (hasErrors) {
                errors.append(line);
            }
        }

        if ((returnCode != 0) || hasErrors) {
            throw new BladeCLIException(errors.toString());
        }

    } catch (IOException e) {
        throw new BladeCLIException(e.getMessage(), e);
    }
    return lines.toArray(new String[0]);
}

From source file:com.clutch.ClutchConf.java

public static JSONObject getConf() {
    if (conf != null) {
        return conf;
    }/*w  w w  . java2s. c  o  m*/

    String versionName = "";
    try {
        versionName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
    } catch (NameNotFoundException e) {
        Log.e(TAG, "Could not get bundle version");
        e.printStackTrace();
    }

    // First we look for a saved plist file that ClutchSync creates
    File dir = context.getDir("_clutch" + versionName, 0);
    File clutch = null;
    for (File f : dir.listFiles()) {
        if (f.getName().equals("clutch.json")) {
            clutch = f;
            break;
        }
    }

    // If we found it, then parse it and set ourselves, then return the data.
    if (clutch != null) {
        try {
            BufferedReader br = new BufferedReader(new FileReader(clutch));
            StringBuffer sb = new StringBuffer();
            String line = br.readLine();
            while (line != null) {
                sb.append(line);
                line = br.readLine();
            }
            conf = new JSONObject(sb.toString());
            br.close();
            return conf;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    // If we didn't find it in the filesystem, check in the assets
    final AssetManager mgr = context.getAssets();
    String asset = ClutchUtils.findAsset(mgr, "./", "clutch.json");
    if (asset == null) {
        conf = new JSONObject();
        return conf;
    }

    try {
        InputStream fin = mgr.open(asset);
        String data = new Scanner(fin).useDelimiter("\\A").next();
        conf = new JSONObject(data);
        return conf;
    } catch (java.util.NoSuchElementException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }

    conf = new JSONObject();

    return conf;
}

From source file:edu.utah.further.core.util.text.CsvLineScanner.java

@Override
public List<String> parse(final String line) {
    final List<String> tokens = CollectionUtil.newList();
    if (StringUtils.isNotBlank(line)) {
        try (final Scanner scanner = new Scanner(line)) {
            scanner.useDelimiter(getDelimiter());
            while (scanner.hasNext()) {
                tokens.add(scanner.next());
            }/*  ww w .  j av  a  2  s.  c om*/
        }

    }
    return tokens;
}