1.fileReader是字符的读出,只能读文件.
2.在读取文件的之前,该文件必须存在.
3.int reader();一次读取一个字符,返回的该字符的码值,如果想要返回字符,直接进行强转char
4.int reader(char[] buf);一次读取一个字符数组,返回每次读取元素的个数
5.怎样判断读取的数据是否到结尾 fileReader.reader() =-1
/** * * @author Administrator * 文件的读FileReader(字符的读) * 文件的写FileWriter(字符的写) */ public class FileTest { public static void main(String[] args) throws IOException { fileReaderTest(); } /** * 字符读 * @throws IOException */ public static void fileReaderTest() throws IOException{ //1.创建文件对象 File file = new File("a.txt"); //2.创建字符读流对象 FileReader fr = new FileReader(file); //3.一个字符一个字符的读取文件,返回的字节的码值 int read = fr.read(); System.out.println("一个字节一个字节读:"+read); //4.一个字符数组一个字符数组的读,返回的每次读取的个数 char[] chars = new char[1024]; int read2 = fr.read(chars); System.out.println("一次读取的个数"+read2); System.out.println("每次读取的char数组:"+Arrays.toString(chars)); //5.读取文件中的所有内容 char[] charts = new char[3]; int len; while((len = fr.read(charts)) != -1){ System.out.println(new String(charts, 0, len)); } //4.关闭读流 fr.close(); } }