本文整理了Java中java.io.InputStreamReader.<init>()
方法的一些代码示例,展示了InputStreamReader.<init>()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。InputStreamReader.<init>()
方法的具体详情如下:
包路径:java.io.InputStreamReader
类名称:InputStreamReader
方法名:<init>
[英]Constructs a new InputStreamReader on the InputStream in. This constructor sets the character converter to the encoding specified in the "file.encoding" property and falls back to ISO 8859_1 (ISO-Latin-1) if the property doesn't exist.
[中]在中的InputStream上构造新的InputStreamReader。此构造函数将字符转换器设置为“file.encoding”属性中指定的编码,如果该属性不存在,则返回ISO 8859_1(ISO-Latin-1)。
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
}
}
}
代码示例来源:origin: jenkinsci/jenkins
/**
* 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();
}
}
代码示例来源:origin: google/guava
/**
* 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));
}
代码示例来源:origin: libgdx/libgdx
/** 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);
}
代码示例来源:origin: eugenp/tutorials
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();
}
}
代码示例来源:origin: apache/incubator-shardingsphere
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);
}
}
代码示例来源:origin: stackoverflow.com
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());
}
代码示例来源:origin: apache/incubator-dubbo
/**
* 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();
}
}
代码示例来源:origin: neo4j/neo4j
public static BufferedReader newBufferedFileReader( File file, Charset charset ) throws FileNotFoundException
{
return new BufferedReader( new InputStreamReader( new FileInputStream( file ), charset ) );
}
代码示例来源:origin: stackoverflow.com
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.
}
代码示例来源:origin: stackoverflow.com
String result = new BufferedReader(new InputStreamReader(inputStream))
.lines().collect(Collectors.joining("\n"));
代码示例来源:origin: apache/incubator-shardingsphere
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);
}
}
代码示例来源:origin: apache/incubator-dubbo
/**
* 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();
}
}
代码示例来源:origin: prestodb/presto
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));
}
代码示例来源:origin: stackoverflow.com
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();
代码示例来源:origin: libgdx/libgdx
/** 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);
}
代码示例来源:origin: apache/incubator-shardingsphere
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;
}
}
代码示例来源:origin: Netflix/eureka
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();
}
}
代码示例来源:origin: stanfordnlp/CoreNLP
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();
}
内容来源于网络,如有侵权,请联系作者删除!