字符流Reader&Writer的使用方法:
1.创建:
以FileReader FileWriter为例
创建FileReader对象:
FileRead fr = new FileReader ("demo.txt");
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"));
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");
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:字符流通向字节流的桥梁 用于操作字符流的字节流对象
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"));
osw.write ("sgydgasddsagukwe");
int len = 0; while ((len = isr.read()) != -1) { System.out.print((char)len); }
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")));
原文地址:http://blog.csdn.net/u013476556/article/details/45535925