在了解 字节流 的读写方式后,我们来看看 字符流 的读写方式
Reader和Writer是java的IO库提供的的另一个流接口,它们和InputStreasm OutputStream的区别是:前者是字符流,以cahr为单位读写,后者是字节流,以byte单位读取
-
Reader
Reader的常用方法
void close() 关闭流并释放与之相关联的所有系统资源
String getEncoding() 返回此流使用的字符编码的名称
int read() 读一个字符
int read(char[] cbuf, int offset, int length) 将字符读入数组的一部分。
boolean ready() 告诉这个流是否准备好被读取
参考代码:
public class TestFileReader { public static void main(String[] args) { String path = "d:\\a.txt"; Reader reader = null; try { reader = new FileReader(path); char[] datas = new char[6]; // 把读取到的2个字符,保存到数组下标从2开始的位置 reader.read(datas,2,2); System.out.println(data); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (reader != null) { reader.close(); } } catch (IOException e) { e.printStackTrace();