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:Main.java

public static String[][] tsvToArray(File f, int columns, boolean useFirstLine) throws FileNotFoundException {
    List<String[]> rows = new LinkedList<String[]>();
    Scanner in = new Scanner(f);
    if (!useFirstLine && in.hasNextLine())
        in.nextLine();//from   w  w w. jav a 2s. co m

    while (in.hasNextLine()) {
        String[] tokens = in.nextLine().split("\t");
        if (columns > 0 && tokens.length < columns)
            continue;
        rows.add(tokens);
    }
    in.close();
    return rows.toArray(new String[0][]);
}

From source file:pl.lewica.util.FileUtil.java

/**
 * Reads contents of a stream (e.g. SQL script) and splits it into separate statements.
 * IMPORTANT: The assumption is the statements are delimited by semicolons followed by new lines.
 * //from  w  ww. ja  va  2  s  .  c om
 * If you are using this method to convert a string with multiple SQL queries to individual statements,
 * make sure the semicolon-new line sequence doesn't exist anywhere inside the SQL statements, perhaps
 * somewhere in the middle of long varchars or text fields as they would be treated as SQL statement
 * delimiters and you would therefore get unexpected results. 
 * 
 * The method might be useful on Android that is unable to e.g. create multiple tables in one go.
 */
public static List<String> convertStreamToStrings(InputStream is, String delimiter) {
    List<String> result = new ArrayList<String>();
    Scanner s = new Scanner(is);
    s.useDelimiter(delimiter);

    while (s.hasNext()) {
        String line = s.next().trim();
        if (line.length() > 0) {
            result.add(line);
        }
    }
    s.close();

    return result;
}

From source file:com.cloudant.client.org.lightcouch.internal.CouchDbUtil.java

public static String streamToString(InputStream in) {
    Scanner s = new Scanner(in, "UTF-8");
    s.useDelimiter("\\A");
    String str = s.hasNext() ? s.next() : null;
    close(in);//from  w  w w  . j  ava2 s.  c o  m
    s.close();// mdb
    return str;
}

From source file:io.github.seanboyy.lotReset.json.ReadJSON.java

/**Read JSON file by using configuration file's JSON value,
 * which points to the file/*from w  w  w.  java  2s.  c  om*/
 * @param configLocation String which specifies the location of the config.properties file
 * @return <code>ArrayList&ltArrayList&ltLot&gt&gt lots</code> if no errors are encountered
 * @since 1.0 - Implemented config.properties in 2.0
 */
public static ArrayList<ArrayList<Lot>> read(String configLocation) {
    JSONParser parser = new JSONParser();
    URL url;
    File f;
    //lots is multidimensional in this format: {{fromLot,toLot},{fromLot,toLot},{fromLot,toLot},etc...}
    ArrayList<ArrayList<Lot>> lots = new ArrayList<ArrayList<Lot>>();
    Config config = ReadConfig.read(configLocation);
    final String[] alphabet = config.getAlpha();
    final String[] types = config.getType();
    final String[] worlds = config.getWorld();
    try {
        url = new URL(config.getJSON());
        InputStream in = url.openStream();
        f = File.createTempFile("temp", "json");
        FileWriter file = new FileWriter(f);
        Scanner input = new Scanner(in);
        while (input.hasNextLine()) {
            file.write(input.nextLine());
        }
        input.close();
        file.close();
        Object obj = parser.parse(new FileReader(f));
        JSONObject jsonObj = (JSONObject) obj;
        JSONObject regions = (JSONObject) jsonObj.get("Regions");
        for (String a : worlds) {
            for (String b : types) {
                for (String c : alphabet) {
                    for (int d = 1; d <= alphabet.length; ++d) {
                        String lotId = a + "-" + b + "-" + c + d;
                        String lotIdA = a + "_" + b + "-" + c + d;
                        String lotIdB = a + "-" + b + "_" + c + d;
                        String lotIdC = a + "_" + b + "_" + c + d;
                        JSONObject lot = (JSONObject) regions.get(lotId);
                        JSONObject lotA = (JSONObject) regions.get(lotIdA);
                        JSONObject lotB = (JSONObject) regions.get(lotIdB);
                        JSONObject lotC = (JSONObject) regions.get(lotIdC);
                        ArrayList<Lot> lotInfo = new ArrayList<Lot>();
                        if (lot != null) {
                            lotInfo.add(new Lot((long) lot.get("source_minX"), (long) lot.get("source_maxX"),
                                    (long) lot.get("source_minY"), (long) lot.get("source_maxY"),
                                    (long) lot.get("source_minZ"), (long) lot.get("source_maxZ"),
                                    (String) lot.get("source_file"), lotId));
                            lotInfo.add(new Lot((long) lot.get("dest_minX"), (long) lot.get("dest_maxX"),
                                    (long) lot.get("dest_minY"), (long) lot.get("dest_maxY"),
                                    (long) lot.get("dest_minZ"), (long) lot.get("dest_maxZ"),
                                    (String) lot.get("dest_file"), lotId));
                            lots.add(lotInfo);
                        }
                        if (lotA != null) {
                            lotInfo.add(new Lot((long) lotA.get("source_minX"), (long) lotA.get("source_maxX"),
                                    (long) lotA.get("source_minY"), (long) lotA.get("source_maxY"),
                                    (long) lotA.get("source_minZ"), (long) lotA.get("source_maxZ"),
                                    (String) lotA.get("source_file"), lotIdA));
                            lotInfo.add(new Lot((long) lotA.get("dest_minX"), (long) lotA.get("dest_maxX"),
                                    (long) lotA.get("dest_minY"), (long) lotA.get("dest_maxY"),
                                    (long) lotA.get("dest_minZ"), (long) lotA.get("dest_maxZ"),
                                    (String) lotA.get("dest_file"), lotIdA));
                            lots.add(lotInfo);
                        }
                        if (lotB != null) {
                            lotInfo.add(new Lot((long) lotB.get("source_minX"), (long) lotB.get("source_maxX"),
                                    (long) lotB.get("source_minY"), (long) lotB.get("source_maxY"),
                                    (long) lotB.get("source_minZ"), (long) lotB.get("source_maxZ"),
                                    (String) lotB.get("source_file"), lotIdB));
                            lotInfo.add(new Lot((long) lotB.get("dest_minX"), (long) lotB.get("dest_maxX"),
                                    (long) lotB.get("dest_minY"), (long) lotB.get("dest_maxY"),
                                    (long) lotB.get("dest_minZ"), (long) lotB.get("dest_maxZ"),
                                    (String) lotB.get("dest_file"), lotIdB));
                            lots.add(lotInfo);
                        }
                        if (lotC != null) {
                            lotInfo.add(new Lot((long) lotC.get("source_minX"), (long) lotC.get("source_maxX"),
                                    (long) lotC.get("source_minY"), (long) lotC.get("source_maxY"),
                                    (long) lotC.get("source_minZ"), (long) lotC.get("source_maxZ"),
                                    (String) lotC.get("source_file"), lotIdC));
                            lotInfo.add(new Lot((long) lotC.get("dest_minX"), (long) lotC.get("dest_maxX"),
                                    (long) lotC.get("dest_minY"), (long) lotC.get("dest_maxY"),
                                    (long) lotC.get("dest_minZ"), (long) lotC.get("dest_maxZ"),
                                    (String) lotC.get("dest_file"), lotIdC));
                            lots.add(lotInfo);
                        }
                    }
                }
            }
        }
    } catch (IOException e) {
        System.out.println("FILE ERROR FROM READING JSON");
        e.printStackTrace();
        return null;
    } catch (ParseException e) {
        System.out.println("PARSER ERROR FROM READING JSON");
        e.printStackTrace();
        return null;
    }
    return lots;
}

From source file:com.almende.pi5.lch.SimManager.java

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

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

public static ItemStack[] getInventory(File jsonFile, int size) {
    String source = "";
    try {// ww  w  .  ja va  2s . c  o  m
        Scanner x = new Scanner(jsonFile);
        while (x.hasNextLine()) {
            source += x.nextLine() + "\n";
        }
        x.close();
        return getInventory(source, size);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.freemedforms.openreact.db.DbSchema.java

/**
 * Attempt to run a database patch.//from  w ww  . j a  v a2 s . co m
 * 
 * @param patchFilename
 * @return Success.
 * @throws SQLException
 */
public static boolean applyPatch(String patchFilename) throws SQLException {
    Connection c = Configuration.getConnection();

    String patch = null;

    Scanner scanner;
    try {
        scanner = new Scanner(new File(patchFilename)).useDelimiter("\\Z");
        patch = scanner.next();
        scanner.close();
    } catch (FileNotFoundException ex) {
        log.error(ex);
        return false;
    }

    Statement cStmt = c.createStatement();
    boolean status = false;
    try {
        log.debug("Using patch length = " + patch.length());
        cStmt.execute(patch);
        // cStmt = c.prepareStatement(patch);
        // cStmt.execute();
        log.info("Patch succeeded");
        status = true;
    } catch (NullPointerException npe) {
        log.error("Caught NullPointerException", npe);
    } catch (Throwable e) {
        log.error(e.toString());
    } finally {
        DbUtil.closeSafely(cStmt);
        DbUtil.closeSafely(c);
    }

    return status;
}

From source file:com.sample.ecommerce.util.ElasticSearchUtil.java

public static String getContentFromClasspath(String resourcePath) {
    InputStream inputStream = ElasticSearchUtil.class.getResourceAsStream(resourcePath);
    java.util.Scanner scanner = new java.util.Scanner(inputStream, "UTF-8").useDelimiter("\\A");
    String theString = scanner.hasNext() ? scanner.next() : "";
    scanner.close();
    return theString;
}

From source file:apiPull.getItem.java

public static ArrayList numberOfIds() {
    // go to gw2 https://api.guildwars2.com/v2/items and get a list of items
    // return the arraylist, use to loop through implimentations

    ArrayList ids = new ArrayList();

    // Make a URL to the web page
    URL url = null;//w w w .  j ava  2s.co  m
    try {
        url = new URL("https://api.guildwars2.com/v2/items");
    } catch (MalformedURLException ex) {
        Logger.getLogger(getItem.class.getName()).log(Level.SEVERE, null, ex);
    }

    // Get the input stream through URL Connection
    URLConnection con = null;
    try {
        con = url.openConnection();
    } catch (IOException ex) {
        Logger.getLogger(getItem.class.getName()).log(Level.SEVERE, null, ex);
    }
    InputStream is = null;
    try {
        is = con.getInputStream();
        // Once you have the Input Stream, it's just plain old Java IO stuff.
        // For this case, since you are interested in getting plain-text web page
        // I'll use a reader and output the text content to System.out.
        // For binary content, it's better to directly read the bytes from stream and write
        // to the target file.

    } catch (IOException ex) {
        Logger.getLogger(getItem.class.getName()).log(Level.SEVERE, null, ex);
    }

    BufferedReader br = new BufferedReader(new InputStreamReader(is));

    String line = null;

    try {
        // read each line 
        while ((line = br.readLine()) != null) {

            Scanner scanner = new Scanner(line);
            scanner.useDelimiter(",");
            while (scanner.hasNext()) {
                ids.add(scanner.next());
            }
            scanner.close();
        }
    } catch (IOException ex) {
        Logger.getLogger(getItem.class.getName()).log(Level.SEVERE, null, ex);
    }

    return ids;
}

From source file:de.ifgi.mosia.wpswfs.Util.java

public static String readContent(InputStream is, String enc) {
    if (is == null) {
        return null;
    }/*from   w ww.  j a  va2  s. c o m*/

    Scanner sc = new Scanner(is, enc == null ? CharEncoding.ISO_8859_1 : enc);
    StringBuilder sb = new StringBuilder();

    String sep = System.getProperty("line.separator");
    while (sc.hasNext()) {
        sb.append(sc.nextLine());
        sb.append(sep);
    }

    sc.close();
    return sb.toString();
}