IO流常用基类:
字节流常用基类:
InputStream,OutputStream
字符流常用基类:
Reader Writer
转换流:
InputStreamReader:字节流通向字符流的桥梁 用于操作字节流的字符流对象
OutputStreamWriter:字符流通向字节流的桥梁 用于操作字符流的字节流对象
由这四个类派生出来的子类名称都是以其父类名作为子类名的后缀
字符流Reader&Writer的使用方法:
1.创建:
以FileReader FileWriter为例
创建FileReader对象:
FileRead fr = new FileReader ("demo.txt");
创建FileWriter对象:
FileWriter fw = new FileWriter ("demo.txt");
该文件会自动创建到指定的文件夹下,若是有同名文件,将会把原来的覆盖。
创建时会发生异常,需进行异常的处理。
2.读取/写入:
fr.read() 结束标志:返回值为-1
一次读一个,自动往下读
可以这么做:
int len = 0; while ((len = fr.read()) != -1){ System.out.println (len);//打印出结果是数字,可强制转换 }
一次读一个数组,自动往下读
可以这么做:
char buf[] = new char[1024]; int num = 0; while ((num = fr.read(buf)) != -1){ System.out.println (new String (buf)); }
写入就简单很多了
fw.write("shdsdhksldlksjdlksjdkl"); fw.flush ();//刷新,将内容刷进目的地
3.关闭字符流对象:
fr.close (); fw.close ();
关闭之前一定要先判断该对象是否被创建了。
4.异常的处理:
以FileWriter为例,FileReader也是一样的。
FileWriter fw = null; try { fw = new FileWriter("demo.txt"); fw.write("sssssssssssssssssss"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (fw != null) { fw.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
5.使用缓冲区提高效率:
缓冲区的出现时为了提高流的读取效率,所以在创建缓冲区之前必须要有流对象.
创建:
将需要提高效率的流对象作为参数传递给缓冲区的构造函数。
BufferedReader bufr = new BufferedReader (new FileReader ("demo.txt")); BufferedWriter bufw = new BufferedWriter (new FilrWriter ("demo.txt"));
写入/读取
读取:
缓冲区新提供了一个高效的方法readLine ()一次读一行
String line = bufr.readLine (); System.out.println (line);
写入:
bufw.write ("dsadsadsadsadsafrfg"); bufw.flush ();
关闭缓冲区:
关闭缓冲区之后就不必再关闭字符流对象了
bufr.close(); bufw.close ();
字节流 的使用方法:
1.创建:
FileOutputStream fos = new FileOutputStream ("demo.txt");
会自动创建,若有同名文件将会覆盖。
FileInputStream fis = new FileInputStream ("demo.txt");
2.读取/写入
写入:
fos.write ("dsadsadsadwerfetrythy"); fow.flush ();
读取:
一次读一个
int len = 0; while ((len = fis.read ()) != -1){ System.out.println ((char)ch); }
一次读一数组:
byte buf[] = new byte [1024]; int len = 0; while ((len = fis.read (buf)) != -1){ System.out.println (new String (buf,0,len)); }
byte buf[] = new byte[fis.available()];//估计字节数 int len = 0; while ((len = fis.read (buf)) != -1){ System.out.println (new String (buf,0,len)); }
4.缓冲区
将需要被提高效率的字节流对象作为参数传递给缓冲区的构造函数.
BufferedOutputStream bufos = new BufferedOutputStream (new FileOutputStream ("demo.txt")); BufferedInputStream bufis = new BufferedInputStream (new FileInputStream("demo.txt"));
5.关闭
有缓冲区的时候,只需要关闭缓冲区就好,没有的话调用close方法关闭流对象。
转换流:
InputStreamReader:字节流通向字符流的桥梁 用于操作字节流的字符流对象
OutputStreamWriter:字符流通向字节流的桥梁 用于操作字符流的字节流对象
1.创建:
将字节流转换为字符流对象,使用转换流
FileInputStream fis = new FileInputStream ("demo.txt"); InputStreamReader isr = new InputStreamReader (fis); 两句写成一句 InputStreamReader isr = new InputStreamReader (new FileInputStream ("demo.txt"));
FileOutputStream fis = new FIleOutputStream ("demo.txt"); OutputStreamWriter osw = new OutputStreamWriter (fis); 两句写成一句 OutputStreamWriter osw = new OutputStreamWriter (new FileOutputStream ("demo.txt"));
2.写入/读取
写入:
osw.write ("sgydgasddsagukwe");
读取:
int len = 0; while ((len = isr.read()) != -1) { System.out.print((char)len); }
3.缓冲区
FileInputStream fis = new FileInputStream ("demo.txt"); InputStreamReader isr = new InputStreamReader (fis); BufferedReader bufr = new BufferedReader (isr); 三句成一句: BufferedReader bufr = new BufferedReader (new InputStreamReader (new FileInputStream ("demo.txt")));
BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("demo.txt")));