/** * Creates a new scanner. * There is also java.io.Reader version of this constructor. * * @param in the java.io.Inputstream to read input from. */ NegraPennLexer(java.io.InputStream in) { this(new java.io.InputStreamReader (in, java.nio.charset.Charset.forName("UTF-8"))); }
String line; try ( InputStream fis = new FileInputStream("the_file_name"); InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8")); BufferedReader br = new BufferedReader(isr); ) { while ((line = br.readLine()) != null) { // Deal with the line } }
BufferedReader br = new BufferedReader(new InputStreamReader(System.in, Charset.forName("ISO-8859-1")),1024); // ... // inside some iteration / processing logic: if (br.ready()) { int readCount = br.read(inputData, bufferOffset, inputData.length-bufferOffset); }
System.out.println("Usage : java NegraPennLexer [ --encoding <name> ] <inputfile(s)>"); encodingName = argv[1]; try { java.nio.charset.Charset.forName(encodingName); // Side-effect: is encodingName valid? } catch (Exception e) { System.out.println("Invalid encoding '" + encodingName + "'"); return; NegraPennLexer scanner = null; try { java.io.FileInputStream stream = new java.io.FileInputStream(argv[i]); java.io.Reader reader = new java.io.InputStreamReader(stream, encodingName); scanner = new NegraPennLexer(reader); while ( !scanner.zzAtEOF ) scanner.yylex(); System.out.println("File not found : \""+argv[i]+"\"");
OutputStreamWriter char_output = new OutputStreamWriter( new FileOutputStream("some_output.utf8"), Charset.forName("UTF-8").newEncoder() ); InputStreamReader char_input = new InputStreamReader( new FileInputStream("some_input.utf8"), Charset.forName("UTF-8").newDecoder() );
charset = Charset.forName(charsetName); decoder = charset.newDecoder(); decoder.onMalformedInput(CodingErrorAction.REPLACE); try (BufferedReader reader = new BufferedReader(new StringReader(fullText))) { final ArrayList<String> textLines = new ArrayList<>(); while (true) { final String line = reader.readLine(); if (line == null) { break;
protected static boolean containsRegularExpressions(Path f, String[] strRegexps) { Pattern[] matchers = new Pattern[strRegexps.length]; boolean[] results = new boolean[strRegexps.length]; for (int i = 0; i < strRegexps.length; i++) { matchers[i] = Pattern.compile(".*" + strRegexps[i] + ".*"); results[i] = false; } try (BufferedReader br = Files.newBufferedReader(f, Charset.forName("UTF-8"))) { String line = br.readLine(); while (line != null) { for (int i = 0; i < strRegexps.length; i++) { if (matchers[i].matcher(line).matches()) { results[i] = true; } } line = br.readLine(); } } catch (IOException e) { e.printStackTrace(); return false; } for (int i = 0; i < results.length; i++) { if (!results[i]) { throw new AssertionError("Couldn't find " + strRegexps[i]); } } return true; }
URLConnection connection = new URL("https://www.google.com/search?q=" + query).openConnection(); connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11"); connection.connect(); BufferedReader r = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName("UTF-8"))); StringBuilder sb = new StringBuilder(); String line; while ((line = r.readLine()) != null) { sb.append(line); } System.out.println(sb.toString());
public static void main(String[] args) { for (int i = 0; i < entries.length; i++) { Entry entry = entries[i]; System.out.print(i); System.out.print(','); System.out.print(' '); if (entry != null) { System.out.print(entry.mysqlCharset); System.out.print(','); System.out.print(' '); System.out.print(entry.javaCharset); if (entry.javaCharset != null) { System.out.print(','); System.out.print(' '); System.out.print(Charset.forName(entry.javaCharset).name()); } } else { System.out.print("null"); } System.out.println(); } } }
public static String urlToText(URL url, String encoding) throws IOException { Charset charset = encoding == null ? StandardCharsets.UTF_8 : Charset.forName(encoding); try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), charset))) { return reader.lines().collect(joining(System.lineSeparator())); } }
System.out.println("Usage : java NegraLexer [ --encoding <name> ] <inputfile(s)>"); encodingName = argv[1]; try { java.nio.charset.Charset.forName(encodingName); // Side-effect: is encodingName valid? } catch (Exception e) { System.out.println("Invalid encoding '" + encodingName + "'"); return; NegraLexer scanner = null; try { java.io.FileInputStream stream = new java.io.FileInputStream(argv[i]); java.io.Reader reader = new java.io.InputStreamReader(stream, encodingName); scanner = new NegraLexer(reader); while ( !scanner.zzAtEOF ) scanner.yylex(); System.out.println("File not found : \""+argv[i]+"\"");
/** * These are all fine and should not be flagged. */ @NoWarning(value="DM_DEFAULT_ENCODING") public void notBugs() throws IOException { String a = "foobar"; a.getBytes(Charset.forName("UTF-8")); a.getBytes("UTF-8"); new String(new byte[] {}, "UTF-8"); new String(new byte[] {}, 0, 0, "UTF-8"); (new ByteArrayOutputStream()).toString("UTF-8"); new InputStreamReader(new FileInputStream(""), "UTF-8"); new OutputStreamWriter(new FileOutputStream(""), "UTF-8"); new PrintStream(new File(""), "UTF-8"); new PrintStream(new FileOutputStream(""), true, "UTF-8"); new PrintStream("", "UTF-8"); new PrintWriter(new File(""), "UTF-8"); new PrintWriter("", "UTF-8"); new Scanner(new FileInputStream(""), "UTF-8"); new Formatter("", "UTF-8"); new Formatter(new File(""), "UTF-8"); new Formatter(new FileOutputStream(""), "UTF-8"); new StringBuilder().toString(); new ArrayList<Object>().toString(); List<String> failures = new ArrayList<String>(); failures.toString(); }
/** * Creates a new scanner. * There is also java.io.Reader version of this constructor. * * @param in the java.io.Inputstream to read input from. */ CHTBLexer(java.io.InputStream in) { this(new java.io.InputStreamReader (in, java.nio.charset.Charset.forName("UTF-8"))); }
public static String readAsString(URL url) { StringBuilder sb = new StringBuilder(); try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), Charset.forName("UTF-8")))) { String line; while ((line = reader.readLine()) != null) { sb.append(line); sb.append("\n"); } return sb.toString(); } catch (IOException e) { throw new RuntimeException(e); } } }
public static void save(ConfigurationManager cm, File cmLocation) { if (!cmLocation.getName().endsWith(CM_FILE_SUFFIX)) System.err.println("WARNING: Serialized s4-configuration should have the suffix '" + CM_FILE_SUFFIX + '\''); assert cm != null; try { PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(cmLocation), Charset.forName("UTF-8"))); String configXML = ConfigurationManagerUtils.toXML(cm); pw.print(configXML); pw.flush(); pw.close(); } catch (FileNotFoundException e1) { e1.printStackTrace(); } }
public static JSONArray readJsonFromUrl(String url) throws IOException, JSONException { InputStream is = new URL(url).openStream(); try { BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); String jsonText = readAll(rd); JSONArray json = new JSONArray(jsonText); return json; } finally { is.close(); } }
/** * Returns a new Reader on the underlying source object. */ public Reader getReader() throws IOException { // we want to remove the BOM windows adds from a file if the encoding is UTF-8 // in other cases we depend on the charsets Charset cs = Charset.forName(configuration.getSourceEncoding()); InputStream in = new BufferedInputStream(new FileInputStream(file)); if (UTF8.name().equalsIgnoreCase(cs.name())) { in.mark(3); boolean hasBOM = true; try { int i = in.read(); hasBOM &= i == 0xEF; i = in.read(); hasBOM &= i == 0xBB; i = in.read(); hasBOM &= i == 0xFF; } catch (IOException ioe) { hasBOM= false; } if (!hasBOM) in.reset(); } return new InputStreamReader( in, cs ); }
/** * Creates a new scanner. * There is also java.io.Reader version of this constructor. * * @param in the java.io.Inputstream to read input from. */ ArabicLexer(java.io.InputStream in) { this(new java.io.InputStreamReader (in, java.nio.charset.Charset.forName("UTF-8"))); }
private static void appendHttpEntity(StringBuilder sb, HttpEntity e) { try { InputStream is = e.getContent(); String encoding = e.getContentEncoding().getValue(); System.out.println(encoding); BufferedReader br = new BufferedReader(new InputStreamReader(is, Charset.forName(encoding))); String str = null; while ((str = br.readLine()) != null) { sb.append(str); } br.close(); } catch (IOException ex) { LOG.severe(ex.getMessage()); } }