Example usage for java.util Scanner close

List of usage examples for java.util Scanner close

Introduction

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

Prototype

public void close() 

Source Link

Document

Closes this scanner.

Usage

From source file:eu.databata.engine.util.PropagatorTableExport.java

private static String askTableName() {
    System.out.println("Insert table name:");
    Scanner input = new Scanner(System.in);
    String name = input.nextLine();
    input.close();
    return name;//  w  w  w.  j a va  2s  . c  o  m
}

From source file:Main.java

/**
 * Reads a file relative to the dir this app was started from.
 * @param filename relative filename to load
 * @return entire file as a String/*  www .  j  ava 2s  .  c  o m*/
 * @throws FileNotFoundException if file not found!
 */
public static String readFile(String filename) throws FileNotFoundException {

    String startDir = System.getProperty("user.dir");
    File propertyFile = new File(startDir, filename);

    Scanner scan = new Scanner(new FileInputStream(propertyFile));
    scan.useDelimiter("\\Z");
    String content = scan.next();
    scan.close();

    return content;
}

From source file:org.gkh.racf.JSONUtil.java

public static JSONObject getJSONAsResource(String path) throws JSONException {
    InputStream in = JSONParserTemplateBuilder.class.getResourceAsStream(path);

    Scanner scanner = new Scanner(in, "UTF-8");
    String json = scanner.useDelimiter("\\A").next();
    scanner.close();

    if (isValidJSON(json)) {
        return new JSONObject(json);
    }/*w w  w. ja va  2  s.co  m*/

    return null;
}

From source file:com.cocktail.initializer.ItemReader.java

/**
 * Read items.//ww  w .  j  a  v  a 2 s  .  c o  m
 *
 * @param <I>
 *            the generic type
 * @param path
 *            the path
 * @param itemMapper
 *            the item mapper
 * @return the list
 * @throws Exception
 *             the exception
 */
public static <I> List<I> readItems(String path, FieldSetMapper<I> itemMapper) throws Exception {

    ClassPathResource resource = new ClassPathResource(path);
    Scanner scanner = new Scanner(resource.getInputStream());
    String line = scanner.nextLine();
    scanner.close();

    FlatFileItemReader<I> itemReader = new FlatFileItemReader<I>();
    itemReader.setResource(resource);

    // DelimitedLineTokenizer defaults to | as its delimiter
    DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer("|");
    tokenizer.setNames(line.split("\\|"));
    tokenizer.setStrict(false);

    DefaultLineMapper<I> lineMapper = new DefaultLineMapper<I>();
    lineMapper.setLineTokenizer(tokenizer);
    lineMapper.setFieldSetMapper(itemMapper);
    itemReader.setLineMapper(lineMapper);
    itemReader.setRecordSeparatorPolicy(new DefaultRecordSeparatorPolicy());
    itemReader.setLinesToSkip(1);
    itemReader.open(new ExecutionContext());

    List<I> items = new ArrayList<>();
    I item = null;

    do {

        item = itemReader.read();

        if (item != null) {
            items.add(item);
        }

    } while (item != null);

    return items;
}

From source file:com.ExtendedAlpha.SWI.SeparatorLib.Separator.java

public static String getStringFromStream(InputStream stream) {
    Scanner x = new Scanner(stream);
    String str = "";
    while (x.hasNextLine()) {
        str += x.nextLine() + "\n";
    }//  ww  w .ja  v a 2 s . c om
    x.close();
    return str.trim();
}

From source file:br.univali.ps.fuzzy.portugolFuzzyCorretor.control.FileController.java

public static String formatarArquivoTexto(File file) throws FileNotFoundException {
    BufferedReader reader = new BufferedReader(new FileReader(file));
    StringBuilder text = new StringBuilder();
    String NL = System.getProperty("line.separator");
    Scanner scanner = new Scanner(file, "ISO-8859-1");
    while (scanner.hasNextLine()) {
        text.append(scanner.nextLine() + NL);
    }/*  ww w  .  j  a  v a  2s .c o  m*/
    scanner.close();
    return text.toString();
}

From source file:edu.usf.cutr.obascs.utils.CommandLineUtil.java

public static String getSpreadSheetId(CommandLine cmd) {

    String spreadSheetId;//from w ww  . j  a  v a2s  . c o  m
    if (cmd.hasOption(GeneralConstants.CL_OPTION_SPREADSHEET_ID)) {
        spreadSheetId = cmd.getOptionValue(GeneralConstants.CL_OPTION_SPREADSHEET_ID);
    } else {
        Scanner scanner = new Scanner(System.in);
        Logger.getInstance().log("Enter SpreadSheet id:");
        spreadSheetId = scanner.nextLine();
        scanner.close();
    }
    return spreadSheetId;
}

From source file:de.uni_freiburg.informatik.ultimate.licence_manager.util.ProcessUtils.java

public static void inheritIO(final InputStream src, final PrintStream dest) {
    new Thread(new Runnable() {
        public void run() {
            final Scanner sc = new Scanner(src);
            while (sc.hasNextLine()) {
                dest.println(sc.nextLine());
            }//from   w ww  .j  ava 2s . c  om
            sc.close();
        }
    }).start();
}

From source file:example.springdata.rest.stores.StoreInitializer.java

/**
 * Reads a file {@code starbucks.csv} from the class path and parses it into {@link Store} instances about to
 * persisted.//w ww .  j  av a 2 s .com
 * 
 * @return
 * @throws Exception
 */
public static List<Store> readStores() throws Exception {

    ClassPathResource resource = new ClassPathResource("starbucks.csv");
    Scanner scanner = new Scanner(resource.getInputStream());
    String line = scanner.nextLine();
    scanner.close();

    FlatFileItemReader<Store> itemReader = new FlatFileItemReader<Store>();
    itemReader.setResource(resource);

    // DelimitedLineTokenizer defaults to comma as its delimiter
    DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
    tokenizer.setNames(line.split(","));
    tokenizer.setStrict(false);

    DefaultLineMapper<Store> lineMapper = new DefaultLineMapper<Store>();
    lineMapper.setFieldSetMapper(fields -> {

        Point location = new Point(fields.readDouble("Longitude"), fields.readDouble("Latitude"));
        Address address = new Address(fields.readString("Street Address"), fields.readString("City"),
                fields.readString("Zip"), location);

        return new Store(fields.readString("Name"), address);
    });

    lineMapper.setLineTokenizer(tokenizer);
    itemReader.setLineMapper(lineMapper);
    itemReader.setRecordSeparatorPolicy(new DefaultRecordSeparatorPolicy());
    itemReader.setLinesToSkip(1);
    itemReader.open(new ExecutionContext());

    List<Store> stores = new ArrayList<>();
    Store store = null;

    do {

        store = itemReader.read();

        if (store != null) {
            stores.add(store);
        }

    } while (store != null);

    return stores;
}

From source file:Main.java

public static String readTxtFile(String filePath) {
    File file = new File(filePath);
    if (file == null || !file.exists()) {
        return null;
    }/*from   w  w  w  . j  av a  2s  .  c  om*/
    try {
        Scanner scanner = new Scanner(file);
        StringBuilder sb = new StringBuilder();
        while (scanner.hasNextLine()) {
            sb.append(scanner.nextLine());
        }
        scanner.close();
        return sb.toString();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}