canonical example by Tabnine
public void postRequest(String urlStr, String jsonBodyStr) throws IOException { URL url = new URL(urlStr); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setDoOutput(true); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setRequestProperty("Content-Type", "application/json"); try (OutputStream outputStream = httpURLConnection.getOutputStream()) { outputStream.write(jsonBodyStr.getBytes()); outputStream.flush(); } if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()))) { String line; while ((line = bufferedReader.readLine()) != null) { // ... do something with line } } } else { // ... do something with unsuccessful response } }
canonical example by Tabnine
public void readAllLines(InputStream in) throws IOException { try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in))) { String line; while ((line = bufferedReader.readLine()) != null) { // ... do something with line } } }
/** * Read the first line of the given stream, close it, and return that line. * * @param encoding * If null, use the platform default encoding. * @since 1.422 */ public static String readFirstLine(InputStream is, String encoding) throws IOException { try (BufferedReader reader = new BufferedReader( encoding == null ? new InputStreamReader(is) : new InputStreamReader(is, encoding))) { return reader.readLine(); } }
/** * Returns a buffered reader that reads from a file using the given character set. * * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link * java.nio.file.Files#newBufferedReader(java.nio.file.Path, Charset)}. * * @param file the file to read from * @param charset the charset used to decode the input stream; see {@link StandardCharsets} for * helpful predefined constants * @return the buffered reader */ public static BufferedReader newReader(File file, Charset charset) throws FileNotFoundException { checkNotNull(file); checkNotNull(charset); return new BufferedReader(new InputStreamReader(new FileInputStream(file), charset)); }
/** Returns a buffered reader for reading this file as characters. * @throws GdxRuntimeException if the file handle represents a directory, doesn't exist, or could not be read. */ public BufferedReader reader (int bufferSize) { return new BufferedReader(new InputStreamReader(read()), bufferSize); }
private String bodyToString(InputStream body) throws IOException { StringBuilder builder = new StringBuilder(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(body, StandardCharsets.UTF_8)); String line = bufferedReader.readLine(); while (line != null) { builder.append(line).append(System.lineSeparator()); line = bufferedReader.readLine(); } bufferedReader.close(); return builder.toString(); } }
private static YamlOrchestrationMasterSlaveRuleConfiguration unmarshal(final File yamlFile) throws IOException { try ( FileInputStream fileInputStream = new FileInputStream(yamlFile); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8") ) { return new Yaml(new Constructor(YamlOrchestrationMasterSlaveRuleConfiguration.class)).loadAs(inputStreamReader, YamlOrchestrationMasterSlaveRuleConfiguration.class); } }
public static void main(String[] args) throws Exception { String google = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q="; String search = "stackoverflow"; String charset = "UTF-8"; URL url = new URL(google + URLEncoder.encode(search, charset)); Reader reader = new InputStreamReader(url.openStream(), charset); GoogleResults results = new Gson().fromJson(reader, GoogleResults.class); // Show title and URL of 1st result. System.out.println(results.getResponseData().getResults().get(0).getTitle()); System.out.println(results.getResponseData().getResults().get(0).getUrl()); }
/** * read lines. * * @param is input stream. * @return lines. * @throws IOException */ public static String[] readLines(InputStream is) throws IOException { List<String> lines = new ArrayList<String>(); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); try { String line; while ((line = reader.readLine()) != null) { lines.add(line); } return lines.toArray(new String[0]); } finally { reader.close(); } }
public static BufferedReader newBufferedFileReader( File file, Charset charset ) throws FileNotFoundException { return new BufferedReader( new InputStreamReader( new FileInputStream( file ), charset ) ); }
String contentType = connection.getHeaderField("Content-Type"); String charset = null; for (String param : contentType.replace(" ", "").split(";")) { if (param.startsWith("charset=")) { charset = param.split("=", 2)[1]; break; } } if (charset != null) { try (BufferedReader reader = new BufferedReader(new InputStreamReader(response, charset))) { for (String line; (line = reader.readLine()) != null;) { // ... System.out.println(line) ? } } } else { // It's likely binary content, use InputStream/OutputStream. }
String result = new BufferedReader(new InputStreamReader(inputStream)) .lines().collect(Collectors.joining("\n"));
private static YamlOrchestrationShardingRuleConfiguration unmarshal(final File yamlFile) throws IOException { try ( FileInputStream fileInputStream = new FileInputStream(yamlFile); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8") ) { return new Yaml(new Constructor(YamlOrchestrationShardingRuleConfiguration.class)).loadAs(inputStreamReader, YamlOrchestrationShardingRuleConfiguration.class); } }
/** * read lines. * * @param is input stream. * @return lines. * @throws IOException */ public static String[] readLines(InputStream is) throws IOException { List<String> lines = new ArrayList<String>(); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); try { String line; while ((line = reader.readLine()) != null) { lines.add(line); } return lines.toArray(new String[0]); } finally { reader.close(); } }
private BufferedReader createNextReader() throws IOException { if (!files.hasNext()) { return null; } File file = files.next(); FileInputStream fileInputStream = new FileInputStream(file); InputStream in = isGZipped(file) ? new GZIPInputStream(fileInputStream) : fileInputStream; return new BufferedReader(new InputStreamReader(in)); }
String newLine = System.getProperty("line.separator"); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder result = new StringBuilder(); String line; boolean flag = false; while ((line = reader.readLine()) != null) { result.append(flag? newLine: "").append(line); flag = true; } return result.toString();
/** Returns a buffered reader for reading this file as characters. * @throw GdxRuntimeException if the file handle represents a directory, doesn't exist, or could not be read. */ public BufferedReader reader (int bufferSize) { return new BufferedReader(new InputStreamReader(read()), bufferSize); }
private YamlProxyServerConfiguration loadServerConfiguration(final File yamlFile) throws IOException { try ( FileInputStream fileInputStream = new FileInputStream(yamlFile); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8") ) { YamlProxyServerConfiguration result = new Yaml(new Constructor(YamlProxyServerConfiguration.class)).loadAs(inputStreamReader, YamlProxyServerConfiguration.class); Preconditions.checkNotNull(result, "Server configuration file `%s` is invalid.", yamlFile.getName()); Preconditions.checkState(!Strings.isNullOrEmpty(result.getAuthentication().getUsername()) || null != result.getOrchestration(), "Authority configuration is invalid."); return result; } }
public String read(InputStream inputStream) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(inputStream)); String toReturn; try { String line = br.readLine(); toReturn = line; while (line != null) { // need to read all the buffer for a clean connection close line = br.readLine(); } return toReturn; } finally { br.close(); } }
public TextTaggedFileReader(TaggedFileRecord record) { filename = record.file; try { reader = new BufferedReader(new InputStreamReader (new FileInputStream(filename), record.encoding)); } catch (IOException e) { throw new RuntimeException(e); } tagSeparator = record.tagSeparator; primeNext(); }