List of usage examples for java.util Scanner useDelimiter
public Scanner useDelimiter(String pattern)
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 .j av a2 s . co m*/ * 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: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();//from w ww .j a v a 2 s . c o m if (isValidJSON(json)) { return new JSONObject(json); } return null; }
From source file:UtilFunctions.java
public static String[] parseFileTypes(String inputString) { //ArrayList to store each parsed filetype ArrayList<String> fileTypes = new ArrayList<>(); //Scanner to iterate over the string filetype by filetype Scanner in = new Scanner(inputString); in.useDelimiter("[,] | [, ]"); //Iterate over the string while (in.hasNext()) { fileTypes.add(in.next());/*from w w w .j a va 2 s .c om*/ } if (fileTypes.size() > 0) { //Generate an array from the ArrayList String[] returnArray = new String[fileTypes.size()]; returnArray = fileTypes.toArray(returnArray); //Return that magnificent array return returnArray; } else { return null; } }
From source file:com.alibaba.openapi.client.util.URLEncodedUtils.java
/** * Adds all parameters within the Scanner to the list of * <code>parameters</code>, as encoded by <code>encoding</code>. For * example, a scanner containing the string <code>a=1&b=2&c=3</code> would * add the {@link NameValuePair NameValuePairs} a=1, b=2, and c=3 to the * list of parameters./* w w w. ja v a2 s . c o m*/ * * @param parameters * List to add parameters to. * @param scanner * Input that contains the parameters to parse. * @param encoding * Encoding to use when decoding the parameters. */ public static void parse(final List<NameValuePair> parameters, final Scanner scanner, final String encoding) { scanner.useDelimiter(PARAMETER_SEPARATOR); while (scanner.hasNext()) { final String[] nameValue = scanner.next().split(NAME_VALUE_SEPARATOR); if (nameValue.length == 0 || nameValue.length > 2) throw new IllegalArgumentException("bad parameter"); final String name = decode(nameValue[0], encoding); String value = null; if (nameValue.length == 2) value = decode(nameValue[1], encoding); parameters.add(new BasicNameValuePair(name, value)); } }
From source file:game.utils.Utils.java
public static String readFile(File file) { String ret = null;/* www . ja v a 2 s.c o m*/ try { Scanner scanner = new Scanner(file); ret = scanner.useDelimiter("\\Z").next(); scanner.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } return ret; }
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;//from w ww . j a v a2 s . c om 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:jfractus.app.Main.java
private static void parseSize(String sizeStr, Dimension dim) throws ArgumentParseException, BadValueOfArgumentException { Scanner scanner = new Scanner(sizeStr); scanner.useLocale(Locale.ENGLISH); scanner.useDelimiter("x"); int outWidth = 0, outHeight = 0; try {//from www .j a va 2 s. co m outWidth = scanner.nextInt(); outHeight = scanner.nextInt(); if (outWidth < 0 || outHeight < 0) throw new BadValueOfArgumentException("Bad value of argument"); } catch (InputMismatchException e) { throw new ArgumentParseException("Command line parse exception"); } catch (NoSuchElementException e) { throw new ArgumentParseException("Command line parse exception"); } if (scanner.hasNext()) throw new ArgumentParseException("Command line parse exception"); dim.setSize(outWidth, outHeight); }
From source file:org.geotools.data.couchdb.client.CouchDBUtils.java
public static String read(File f) throws FileNotFoundException { String text = null;// w w w .j ava 2 s. c o m Scanner s = new Scanner(f); try { text = s.useDelimiter("\\Z").next(); } finally { s.close(); } if (f.getName().endsWith(".json")) { // strip comments text = stripComments(text); } return text; }
From source file:com.yoavst.quickapps.URLEncodedUtils.java
/** * Adds all parameters within the Scanner to the list of * <code>parameters</code>, as encoded by <code>encoding</code>. For * example, a scanner containing the string <code>a=1&b=2&c=3</code> would * add the {@link NameValuePair NameValuePairs} a=1, b=2, and c=3 to the * list of parameters.//from w w w . j a va 2s .c o m * * @param parameters List to add parameters to. * @param scanner Input that contains the parameters to parse. * @param encoding Encoding to use when decoding the parameters. */ public static void parse(final List<NameValuePair> parameters, final Scanner scanner, final String encoding) { scanner.useDelimiter(PARAMETER_SEPARATOR); while (scanner.hasNext()) { final String[] nameValue = scanner.next().split(NAME_VALUE_SEPARATOR); if (nameValue.length == 0 || nameValue.length > 2) throw new IllegalArgumentException("bad parameter"); final String name = decode(nameValue[0], encoding); String value = null; if (nameValue.length == 2) value = decode(nameValue[1], encoding); parameters.add(new BasicNameValuePair(name, value)); } }
From source file:org.kercoin.magrit.ArgumentsParserTest.java
static String[] split(String cmdLine) { Scanner s = new Scanner(cmdLine); s.useDelimiter(Pattern.compile("\\s+")); List<String> args = new ArrayList<String>(); while (s.hasNext()) { args.add(s.next());/*from w ww.ja va 2 s . com*/ } return args.toArray(new String[0]); }