相关文章推荐
挂过科的肉夹馍  ·  Failed to convert ...·  1 周前    · 
才高八斗的椅子  ·  OptimizeWarning: ...·  1 月前    · 
魁梧的汉堡包  ·  【C】——sysmalloc: ...·  1 年前    · 
1、首先创建FileReader对象
2、将FileReader传递给BufferedReader
3、采用BufferedReader的readLine()方法和read()方法来读取文件内容
4、最后一定要的finally语句中关闭BufferedReaders
5、FileReader与BufferedReader配合使用,File,FileInputStream,BufferedInputStream配合使用
import java.io.*;
public class Exercise {
  public static void main(String args[]) {
    BufferedReader br = null;
    BufferedReader br2 = null;
    try {
      br = new BufferedReader(new FileReader("/home/zjz/Desktop/myfile.txt"));
      // The first way of reading the file
      System.out.println("Reading the file using readLine() method: ");
      String contentLine = br.readLine();
      while (contentLine != null) {
        System.out.println(contentLine);
        contentLine = br.readLine();
      br2 = new BufferedReader(new FileReader("/home/zjz/Desktop/myfile2.txt"));
      // The second way of reading the file
      System.out.println("Reading the file using read() method: ");
      int num = 0;
      char ch;
      while ((num = br2.read()) != -1) {
        ch = (char) num;
        System.out.print(ch);
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        if (br != null) {
          br.close();
        if (br2 != null) {
          br2.close();
      } catch (IOException e) {
        System.out.println("Error in closing the BufferedReader");

From: http://beginnersbook.com/2014/01/how-to-read-file-in-java-bufferedinputstream/

java读取txt文件,可以以文件路径构造这个流,:FileInputStream fin = new FileInputStream("d:/test.txt"); 然后使用这个流直接读取文件,再使用reader构造BufferedReader,按行读取一整行的文本,作为字符串返回: BufferedReader buffReader = new BufferedReader(reader); 通常,对 Reader 发出的每个读取请求都会导致对底层字符或字节流发出相应的读取请求。因此,建议将 BufferedReader 包装在任何其 read() 操作可能代价高昂的 Reader 周围,例如 FileReaders 和 InputStreamReaders.如果没有缓冲,每次调用 read() 或 readLine() 都可能导致从文件读取字节,将其转换为字符,然后返回,这可能非常低效。可以指定缓冲区大小,也可以使用默认大小。从字符输入流读取文本,缓冲字符以便有效地读取字符、数组和行。 掌握键盘输入的基本形式 Buffer:表示缓冲区的。之前的StringBuffer,缓冲区内容可以更改,可以提高效率。 如果想接收任意长度的数据,而且避免乱码的产生,就可以使用BufferedReader。 public class BufferedReader extends Reader 因为输入的数据有可能出现文,所以此处使用字符流完成 BufferedReade 注意看第5行判空那里,我直接用工具类判空了,但是StringUtils.isBlank()方法是判断字符串是否为null、是否为""、是否为空字符串(引号间有空格)" ",以上情景都会返回true。而读取文件的空行时line返回的是""不是null,所以读到空行后 直接就跳出循环了导致后面的数据没有读到。今天读取文件的时候,发现最终读出来的文件缺少数据,然后发下源文件有几个空行,但是代码里已经写了readLine判空了呀,为啥还不行呢?把判空逻辑修改下就可以读到完全的数据了。 public static String loadAFileToStringDE1(File f) throws IOException { long beginTime = System.currentTimeMillis(); InputStream is = null; String ret = null; try { is = new BufferedInputStream( new FileInputStream(f) ); long contentLength = f.length(); ByteArrayOutputStream outstream = new ByteArrayOutputStream( contentLength > 0 ? (int) contentLength : 1024); byte[] buffer = new byte[4096]; int len; while ((len = is.read(buffer)) > 0) { outstream.write(buffer, 0, len); outstream.close(); ret = outstream.toString(); //byte[] ba = outstream.toByteArray(); //ret = new String(ba); } finally { if(is!=null) {try{is.close();} catch(Exception e){} } long endTime = System.currentTimeMillis(); System.out.println("方法1用时"+ (endTime-beginTime) + "ms"); return ret; public static String loadAFileToStringDE2(File f) throws IOException { long beginTime = System.currentTimeMillis(); InputStream is = null; String ret = null; try { is = new FileInputStream(f) ; long contentLength = f.length(); byte[] ba = new byte[(int)contentLength]; is.read(ba); ret = new String(ba); } finally { if(is!=null) {try{is.close();} catch(Exception e){} } long endTime = System.currentTimeMillis(); System.out.println("方法2用时"+ (endTime-beginTime) + "ms"); return ret; public static String loadAFileToStringDE3(File f) throws IOException { long beginTime = System.currentTimeMillis(); BufferedReader br = null; String ret = null; try { br = new BufferedReader(new FileReader(f)); String line = null; StringBuffer sb = new StringBuffer((int)f.length()); while( (line = br.readLine() ) != null ) { sb.append(line).append(LINE_BREAK); ret = sb.toString(); } finally { if(br!=null) {try{br.close();} catch(Exception e){} } long endTime = System.currentTimeMillis(); System.out.println("方法3用时"+ (endTime-beginTime) + "ms"); return ret; 3个方法去读取一个大于50M的文件,当不设置jvm参数时都OutofMemery,当设置-Xmx128M时。只有方法3 可以通过,设置到-Xmx256M时也只有方法3可以通过,干脆设置512M,都可以了,运行时间如果正常的话一般都在4~5S 笔者在大二下初学软件构造课,课程实验要求使用Java语言,由于事先仅仅接触过C/C++语言,且编写代码能力并不高,难免磕磕碰碰,借此机会记录一些错误以及心得体会。Java语言对笔者来说是一片新天地,若有理解有失偏颇方面,还请见谅。 在课程实验一构造当,有这样一个要求: 从指定文本文件路径获取信息 事先笔者并未接触过有关Java语言文件读入的相关信息与方法,只能从Java学习渠道搜索解决方法。以下将从基础层次逐步解读问题。 import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStreamReader; import ja import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Timer; import java.u...