java.io.BufferedReader.close()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(6.9k)|赞(0)|评价(0)|浏览(322)

本文整理了Java中java.io.BufferedReader.close()方法的一些代码示例,展示了BufferedReader.close()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。BufferedReader.close()方法的具体详情如下:
包路径:java.io.BufferedReader
类名称:BufferedReader
方法名:close

BufferedReader.close介绍

[英]Closes this reader. This implementation closes the buffered source reader and releases the buffer. Nothing is done if this reader has already been closed.
[中]关闭此读卡器。此实现关闭缓冲源读取器并释放缓冲区。如果此读卡器已关闭,则不会执行任何操作。

代码示例

代码示例来源: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: gocd/gocd

private static StringBuilder dump(BufferedReader reader, StringBuilder builder) throws IOException {
  String line;
  while ((line = reader.readLine()) != null) {
    builder.append(line + "\n");
  }
  reader.close();
  return builder;
}

代码示例来源:origin: stackoverflow.com

LineNumberReader  lnr = new LineNumberReader(new FileReader(new File("File1")));
lnr.skip(Long.MAX_VALUE);
System.out.println(lnr.getLineNumber() + 1); //Add 1 because line index starts at 0
// Finally, the LineNumberReader object should be closed to prevent resource leak
lnr.close();

代码示例来源:origin: stanfordnlp/CoreNLP

public TraditionalSimplifiedCharacterMap(String path) {
 // TODO: gzipped maps might be faster
 try {
  FileInputStream fis = new FileInputStream(path);
  InputStreamReader isr = new InputStreamReader(fis, "utf-8");
  BufferedReader br = new BufferedReader(isr);
  init(br);
  br.close();
  isr.close();
  fis.close();
 } catch (IOException e) {
  throw new RuntimeIOException(e);
 }
}

代码示例来源: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: stanfordnlp/CoreNLP

private void readRawBytes(String fileName) throws IOException {
 BufferedReader in = new BufferedReader(new FileReader(fileName));
 StringBuffer buf = new StringBuffer();
 int c;
 while ((c = in.read()) >= 0)
  buf.append((char) c);
 mRawBuffer = buf.toString();
 // System.out.println(mRawBuffer);
 in.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 br = new BufferedReader(new FileReader(path));
try {
  return br.readLine();
} finally {
  br.close();
}

代码示例来源:origin: checkstyle/checkstyle

void method() throws Exception {
  final String fileName = "Test";
  final BufferedReader br = new BufferedReader(new InputStreamReader(
      new FileInputStream(fileName), StandardCharsets.UTF_8));
  try {
  } finally {
    br.close();
  }
}

代码示例来源:origin: stanfordnlp/CoreNLP

public void readGrammar(String filename) {
 try {
  FileReader fin = new FileReader(filename);
  BufferedReader bin = new BufferedReader(fin);
  readGrammar(bin);
  bin.close();
  fin.close();
 } catch (IOException e) {
  throw new RuntimeIOException(e);
 }
}

代码示例来源:origin: stanfordnlp/CoreNLP

protected String getLine() {
 try {
  String result = this.reader.readLine();
  if (result == null) {
   readerOpen = false;
   this.reader.close();
  }
  return result;
 } catch (IOException e) {
  throw new RuntimeIOException(e);
 }
}

代码示例来源: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 reader = new BufferedReader(new FileReader("file.txt"));
int lines = 0;
while (reader.readLine() != null) lines++;
reader.close();

代码示例来源: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: stackoverflow.com

import java.net.*;
import java.io.*;

public class URLConnectionReader {
  public static void main(String[] args) throws Exception {
    URL yahoo = new URL("http://www.yahoo.com/");
    URLConnection yc = yahoo.openConnection();
    BufferedReader in = new BufferedReader(
                new InputStreamReader(
                yc.getInputStream()));
    String inputLine;

    while ((inputLine = in.readLine()) != null) 
      System.out.println(inputLine);
    in.close();
  }
}

代码示例来源:origin: h2oai/h2o-2

public static void log(File file, PrintStream stream) throws Exception {
 BufferedReader reader = new BufferedReader(new FileReader(file));
 try {
  for( ;; ) {
   String line = reader.readLine();
   if( line == null ) break;
   stream.println(line);
  }
 } finally {
  reader.close();
 }
}

代码示例来源:origin: stackoverflow.com

InputStream responseStream = new 
  BufferedInputStream(httpUrlConnection.getInputStream());

BufferedReader responseStreamReader = 
  new BufferedReader(new InputStreamReader(responseStream));

String line = "";
StringBuilder stringBuilder = new StringBuilder();

while ((line = responseStreamReader.readLine()) != null) {
  stringBuilder.append(line).append("\n");
}
responseStreamReader.close();

String response = stringBuilder.toString();

代码示例来源:origin: jenkinsci/jenkins

LinuxProcess(int pid) throws IOException {
  super(pid);
  BufferedReader r = new BufferedReader(new FileReader(getFile("status")));
  try {
    String line;
    while((line=r.readLine())!=null) {
      line=line.toLowerCase(Locale.ENGLISH);
      if(line.startsWith("ppid:")) {
        ppid = Integer.parseInt(line.substring(5).trim());
        break;
      }
    }
  } finally {
    r.close();
  }
  if(ppid==-1)
    throw new IOException("Failed to parse PPID from /proc/"+pid+"/status");
}

代码示例来源:origin: stackoverflow.com

// Open the file
FileInputStream fstream = new FileInputStream("textfile.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));

String strLine;

//Read File Line By Line
while ((strLine = br.readLine()) != null)   {
 // Print the content on the console
 System.out.println (strLine);
}

//Close the input stream
br.close();

相关文章