java编程思想第18章 JAVA I/O系统 IO流的典型使用方式

缓冲输入文件:

打开一个文件作为文件输入,为了提高速度,可以对读取的文件进行缓冲,可以将产生的引用传入BufferedReader构造器。由于Buffered也提供readLine()方法,所以这是最终对象和进行读取的接口。当readLine()最后返回null时,就已经达到了文件的末尾。

下面是读取的一个代码

public class BufferedInputFile {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		System.out.println(read("C:\\Users\\xql\\Desktop\\data\\connectData\\center_chengdu.txt"));
	}
	public static String read(String File) throws IOException {
		BufferedReader in =new BufferedReader(new FileReader(File));
		String s;
		StringBuilder sb=new StringBuilder();
		while((s=in.readLine())!=null) {
			sb.append(s+"\n");
		}
		return sb.toString();
	}

}

 

你可能感兴趣的:(java及其框架学习)