进阶day09-字符流的读取一个字符数组
This commit is contained in:
39
javaSE-day09/src/com/inmind/reader03/Demo11.java
Normal file
39
javaSE-day09/src/com/inmind/reader03/Demo11.java
Normal file
@@ -0,0 +1,39 @@
|
||||
package com.inmind.reader03;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
/*
|
||||
字符流一次读取一个字符数组
|
||||
|
||||
字符流的读取一个字符
|
||||
在java中使用抽象类Reader表示字符输入流
|
||||
常用子类FileReader
|
||||
构造方法:
|
||||
FileReader(File file) 创建一个新的 FileReader ,给出 File读取。
|
||||
FileReader(String fileName) 创建一个新的 FileReader ,给定要读取的文件的名称。
|
||||
常用方法:
|
||||
abstract void close() 关闭资源
|
||||
int read(char[] cbuf) 将字符读入数组。
|
||||
*/
|
||||
public class Demo11 {
|
||||
public static void main(String[] args) throws IOException {
|
||||
//创建一个字符输入流
|
||||
FileReader fr = new FileReader("a.txt");
|
||||
|
||||
//创建一个字符数组,用来保存读取到的多个字符
|
||||
char[] chars = new char[1024];
|
||||
//用来获取读取的字符的个数
|
||||
int len;
|
||||
/*len = fr.read(chars);
|
||||
System.out.println(len);//6
|
||||
System.out.println(new String(chars,0,len));//*/
|
||||
|
||||
while ((len = fr.read(chars)) != -1) {
|
||||
System.out.println(new String(chars,0,len));//读多少个字符,就获取多少个
|
||||
}
|
||||
|
||||
fr.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user