Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2535682
  • 博文数量: 245
  • 博客积分: 4125
  • 博客等级: 上校
  • 技术积分: 3113
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-25 23:56
文章分类

全部博文(245)

文章存档

2015年(2)

2014年(26)

2013年(41)

2012年(40)

2011年(134)

2010年(2)

分类: Java

2011-10-27 11:20:24

java读取文本文件的方法有很多,这个例子主要介绍最简单,最常用的BufferedReader类。

完整例子如下:

  1. package net.chinaunix.blog.hzm.text;

  2. import java.io.BufferedReader;
  3. import java.io.FileReader;
  4. import java.io.IOException;

  5. public class ReadFile {

  6.     private String path;
  7.     
  8.     
  9.     public ReadFile(String filePath){
  10.         path = filePath;
  11.     }
  12.     
  13.     public String[] openFile() throws IOException{
  14.         
  15.         FileReader fr = new FileReader(path);
  16.         BufferedReader textReader = new BufferedReader(fr);
  17.         
  18.         String[] textData = new String[readLines()];
  19.         int i;
  20.         for(i=0; i < readLines();i++){
  21.             textData[i] = textReader.readLine();
  22.         }
  23.         
  24.         textReader.close();
  25.         
  26.         return textData;
  27.     }
  28.     
  29.     int readLines() throws IOException{
  30.         FileReader fileToRead = new FileReader(path);
  31.         BufferedReader bf = new BufferedReader(fileToRead);
  32.         
  33.         int numberOfLines = 0;
  34.         
  35.         @SuppressWarnings("unused")
  36.         String oneLine;
  37.         while((oneLine = bf.readLine()) != null){
  38.             numberOfLines++;
  39.         }
  40.         bf.close();
  41.         
  42.         return numberOfLines;
  43.         
  44.     }
  45. }


  1. package net.chinaunix.blog.hzm.text;

  2. import java.io.IOException;

  3. public class FileData {

  4.     public static void main(String[] args) throws IOException{
  5.         
  6.         String filePath = "C:/text.txt";
  7.         
  8.         try{
  9.             ReadFile reader = new ReadFile(filePath);
  10.             String[] content = reader.openFile();
  11.             
  12.             int i;
  13.             for(i=0;i<content.length;i++){
  14.                 System.out.println(content[i]);
  15.             }
  16.             
  17.         }catch(IOException e){
  18.             System.out.println("异常信息:" + e.getMessage());
  19.         }
  20.     }
  21. }

java.io.BufferedReader

The buffer size may be specified, or the default size may be used. The default is large enough for most purposes.

In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream. It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders. For example,

BufferedReader in = new BufferedReader(new FileReader("foo.in")); will buffer the input from the specified file. Without buffering, each invocation of read() or readLine() could cause bytes to be read from the file, converted into characters, and then returned, which can be very inefficient.

Programs that use DataInputStreams for textual input can be localized by replacing each DataInputStream with an appropriate BufferedReader.



java.io.FileReader

FileReader is meant for reading streams of characters. For reading streams of raw bytes, consider using a FileInputStream.


参考:
阅读(2306) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~