List of usage examples for java.util Scanner close
public void close()
From source file:org.jdal.text.PeriodFormatter.java
/** * {@inheritDoc}/* w w w. ja v a 2s .c o m*/ */ public Number parse(String text, Locale locale) throws ParseException { long value = 0; Scanner scanner = new Scanner(text); while (scanner.hasNext()) value += parse(scanner.nextLong(), scanner.next("[dhms]")); scanner.close(); return value; }
From source file:hu.petabyte.redflags.engine.gear.indicator.hu.ContrDescCartellingIndicator.java
@Override public void beforeSession() throws Exception { keywords.clear();//from w w w . j a va 2 s . c om Scanner s = new Scanner(new ClassPathResource(filename).getInputStream(), "UTF-8"); while (s.hasNextLine()) { keywords.add(s.nextLine()); } s.close(); LOG.debug("Loaded {} expressions for cartelling", keywords.size()); setWeight(0.5); super.beforeSession(); }
From source file:net.bashtech.geobot.BotManager.java
public static String postDataLinkShortener(String postData) { URL url;// w ww .j a v a 2 s. co m HttpURLConnection conn; postData = "{\"longUrl\": \"" + postData + "\"}"; try { url = new URL("https://www.googleapis.com/urlshortener/v1/url"); conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setRequestProperty("User-Agent", "CoeBot"); conn.setRequestProperty("Content-Type", "application/json"); conn.setRequestProperty("Content-Length", "" + Integer.toString(postData.getBytes().length)); PrintWriter out = new PrintWriter(conn.getOutputStream()); System.out.println(postData); out.print(postData); out.close(); String response = ""; Scanner inStream = new Scanner(conn.getInputStream()); while (inStream.hasNextLine()) response += (inStream.nextLine()); inStream.close(); return response; } catch (MalformedURLException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } return ""; }
From source file:net.bashtech.geobot.BotManager.java
public static String postRemoteDataTwitch(String urlString, String postData, int krakenVersion) { URL url;/*ww w . j ava 2s . c o m*/ HttpURLConnection conn; try { url = new URL(urlString); conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setFixedLengthStreamingMode(postData.getBytes().length); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setRequestProperty("Accept", "application/vnd.twitchtv.v" + krakenVersion + "+json"); conn.setRequestProperty("Authorization", "OAuth " + BotManager.getInstance().krakenOAuthToken); conn.setRequestProperty("Client-ID", BotManager.getInstance().krakenClientID); // conn.setConnectTimeout(5 * 1000); // conn.setReadTimeout(5 * 1000); PrintWriter out = new PrintWriter(conn.getOutputStream()); out.print(postData); out.close(); String response = ""; Scanner inStream = new Scanner(conn.getInputStream()); while (inStream.hasNextLine()) response += (inStream.nextLine()); inStream.close(); return response; } catch (MalformedURLException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } return ""; }
From source file:net.bashtech.geobot.BotManager.java
public static String postRemoteDataStrawpoll(String urlString) { String line = ""; try {//from ww w . j ava2s .c om HttpURLConnection c = (HttpURLConnection) (new URL("http://strawpoll.me/api/v2/polls") .openConnection()); c.setRequestMethod("POST"); c.setRequestProperty("Content-Type", "application/json"); c.setRequestProperty("User-Agent", "CB2"); c.setDoOutput(true); c.setDoInput(true); c.setUseCaches(false); String queryString = urlString; c.setRequestProperty("Content-Length", Integer.toString(queryString.length())); DataOutputStream wr = new DataOutputStream(c.getOutputStream()); wr.writeBytes(queryString); wr.flush(); wr.close(); Scanner inStream = new Scanner(c.getInputStream()); while (inStream.hasNextLine()) line += (inStream.nextLine()); inStream.close(); System.out.println(line); try { JSONParser parser = new JSONParser(); Object obj = parser.parse(line); JSONObject jsonObject = (JSONObject) obj; line = (Long) jsonObject.get("id") + ""; } catch (Exception e) { e.printStackTrace(); } } catch (MalformedURLException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } return line; }
From source file:com.google.api.ads.adwords.keywordoptimizer.KeywordOptimizer.java
/** * Reads settings (keywords / urls / serach terms) line-by-line from a file. * * @param fileName the name of the file to read from * @return a {@link List} of strings line-by-line * @throws KeywordOptimizerException in case there is a problem reading the file *//*from ww w .ja va2s. c o m*/ private static List<String> loadFromFile(String fileName) throws KeywordOptimizerException { List<String> out = new ArrayList<String>(); Scanner scan = null; try { scan = new Scanner(new File(fileName)); while (scan.hasNextLine()) { String line = scan.nextLine().trim(); // Ignore comment lines. if (line.startsWith("#")) { continue; } out.add(line); } return out; } catch (IOException e) { throw new KeywordOptimizerException("Error loading file '" + fileName + "'", e); } finally { if (scan != null) { scan.close(); } } }
From source file:dsfixgui.FileIO.DSFixFileController.java
public static String readTextFile(String filePath) throws FileNotFoundException { //The file to be read File readFile = new File(filePath); //Initialize Scanner for reading file Scanner fileReader = new Scanner(readFile); //The String to be returned String text = null;/*from w ww . j av a 2 s . co m*/ while (fileReader.hasNextLine()) { if (text == null) { //Initialize return String text = ""; } else { //Add new linebreak text += String.format("%n"); } //Add line to text text += fileReader.nextLine(); } fileReader.close(); return text; }
From source file:edu.harvard.hul.ois.fits.junit.AudioStdSchemaTestXmlUnit_NoMD5.java
@Test public void testAudioMD_noMD5() throws Exception { Fits fits = new Fits(); // First generate the FITS output File input = new File("testfiles/test.wav"); FitsOutput fitsOut = fits.examine(input); XMLOutputter serializer = new XMLOutputter(Format.getPrettyFormat()); String actualXmlStr = serializer.outputString(fitsOut.getFitsXml()); // Read in the expected XML file Scanner scan = new Scanner(new File("testfiles/output/FITS_test_wav_NO_MD5.xml")); String expectedXmlStr = scan.useDelimiter("\\Z").next(); scan.close(); // Set up XMLUnit XMLUnit.setIgnoreWhitespace(true);/*from w w w . j a v a2s. com*/ XMLUnit.setNormalizeWhitespace(true); Diff diff = new Diff(expectedXmlStr, actualXmlStr); // Initialize attributes or elements to ignore for difference checking diff.overrideDifferenceListener(new IgnoreNamedElementsDifferenceListener("version", "toolversion", "dateModified", "fslastmodified", "lastmodified", "startDate", "startTime", "timestamp", "fitsExecutionTime", "executionTime", "filepath", "lastmodified", "location")); DetailedDiff detailedDiff = new DetailedDiff(diff); // Display any Differences List<Difference> diffs = detailedDiff.getAllDifferences(); if (!diff.identical()) { StringBuffer differenceDescription = new StringBuffer(); differenceDescription.append(diffs.size()).append(" differences"); System.out.println(differenceDescription.toString()); for (Difference difference : diffs) { System.out.println(difference.toString()); } } assertTrue("Differences in XML", diff.identical()); }
From source file:net.bashtech.geobot.BotManager.java
public static String postCoebotConfig(String postData, String channel) { if (BotManager.getInstance().CoeBotTVAPIKey.length() > 4) { URL url;//from w ww.j av a 2s .c o m HttpURLConnection conn; try { url = new URL("http://coebot.tv/api/v1/channel/update/config/" + channel.toLowerCase() + "$" + BotManager.getInstance().CoeBotTVAPIKey + "$" + BotManager.getInstance().nick); conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setRequestProperty("User-Agent", "CoeBot"); conn.setRequestProperty("Content-Type", "application/json"); conn.setRequestProperty("Content-Length", "" + Integer.toString(postData.getBytes().length)); // conn.setConnectTimeout(5 * 1000); // conn.setReadTimeout(5 * 1000); PrintWriter out = new PrintWriter(conn.getOutputStream()); out.print(postData); out.close(); String response = ""; Scanner inStream = new Scanner(conn.getInputStream()); while (inStream.hasNextLine()) response += (inStream.nextLine()); inStream.close(); return response; } catch (MalformedURLException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } return ""; } else return ""; }
From source file:net.bashtech.geobot.BotManager.java
public static String postCoebotVars(String postData, String requestURL) { if (BotManager.getInstance().CoeBotTVAPIKey.length() > 4) { URL url;/*from w w w.ja va 2 s . c o m*/ HttpURLConnection conn; try { url = new URL(requestURL + "$" + BotManager.getInstance().CoeBotTVAPIKey + "$" + BotManager.getInstance().nick); conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setRequestProperty("User-Agent", "CoeBot"); conn.setRequestProperty("Content-Type", "application/json"); conn.setRequestProperty("Content-Length", "" + Integer.toString(postData.getBytes().length)); // conn.setConnectTimeout(5 * 1000); // conn.setReadTimeout(5 * 1000); PrintWriter out = new PrintWriter(conn.getOutputStream()); out.print(postData); out.close(); String response = ""; Scanner inStream = new Scanner(conn.getInputStream()); while (inStream.hasNextLine()) response += (inStream.nextLine()); inStream.close(); return response; } catch (MalformedURLException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } return ""; } else return ""; }