本文整理了Java中java.io.BufferedReader.readLine()
方法的一些代码示例,展示了BufferedReader.readLine()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。BufferedReader.readLine()
方法的具体详情如下:
包路径:java.io.BufferedReader
类名称:BufferedReader
方法名:readLine
[英]Returns the next line of text available from this reader. A line is represented by zero or more characters followed by '\n', '\r', "\r\n" or the end of the reader. The string does not include the newline sequence.
[中]返回此读取器中可用的下一行文本。行由零个或多个字符表示,后跟“\n”、“\r”、“\r\n”或读取器的结尾。该字符串不包括换行符序列。
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: stackoverflow.com
try(BufferedReader br = new BufferedReader(new FileReader(file))) {
for(String line; (line = br.readLine()) != null; ) {
// process the line.
}
// line is not visible here.
}
代码示例来源: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
BufferedReader br = new BufferedReader(new FileReader("file.txt"));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
String everything = sb.toString();
} finally {
br.close();
}
代码示例来源: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: google/guava
private static List<String> readUsingJava(String input, int chunk) throws IOException {
BufferedReader r = new BufferedReader(getChunkedReader(input, chunk));
List<String> lines = Lists.newArrayList();
String line;
while ((line = r.readLine()) != null) {
lines.add(line);
}
r.close();
return lines;
}
代码示例来源:origin: commons-io/commons-io
@Test(timeout = 5000)
public void testReadBytesEOF() throws IOException {
final BoundedReader mr = new BoundedReader( sr, 3 );
try ( BufferedReader br = new BufferedReader( mr ) ) {
br.readLine();
br.readLine();
}
}
}
代码示例来源:origin: google/guava
public void testNewReader() throws IOException {
File asciiFile = getTestFile("ascii.txt");
try {
Files.newReader(asciiFile, null);
fail("expected exception");
} catch (NullPointerException expected) {
}
try {
Files.newReader(null, Charsets.UTF_8);
fail("expected exception");
} catch (NullPointerException expected) {
}
BufferedReader r = Files.newReader(asciiFile, Charsets.US_ASCII);
try {
assertEquals(ASCII, r.readLine());
} finally {
r.close();
}
}
代码示例来源:origin: iluwatar/java-design-patterns
/**
* Loads the data of the file specified.
*/
public String loadData() {
String dataFileName = this.fileName;
try (BufferedReader br = new BufferedReader(new FileReader(new File(dataFileName)))) {
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append('\n');
}
this.loaded = true;
return sb.toString();
} catch (Exception e) {
LOGGER.error("File {} does not exist", dataFileName);
}
return null;
}
代码示例来源: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: 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: stackoverflow.com
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
// process the line.
}
}
代码示例来源:origin: stackoverflow.com
BufferedReader br = new BufferedReader(new FileReader(path));
try {
return br.readLine();
} finally {
br.close();
}
代码示例来源: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: stackoverflow.com
BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line).append('\n');
}
代码示例来源:origin: stackoverflow.com
try(BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
String everything = sb.toString();
}
代码示例来源:origin: stackoverflow.com
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
System.out.println(br.readLine());
} catch (Exception e) {
...
} finally {
...
}
代码示例来源: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: 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();
}
}
内容来源于网络,如有侵权,请联系作者删除!