java-io初步:Reader、Writer、Buf…

java四个基本的io基类:

字节流:InputStream、OutputStream

字符流:Reader、Writer.——作为硬盘的文件使用的两个子类:FileWriter、FileReader.

<wbr></wbr>

1、java早期只有字节流,后边由于处理不同字符的编码需求,出现了字符流.

2、java io体系中子类的命名通常都是以父类的名称作为后缀.

<wbr></wbr>

<wbr></wbr>

import java.io.*;
class IODemo
{
<wbr>public static void main(String[] args) // throws Exception<br><wbr>{<br><wbr><wbr>//fileWrite();<br><wbr><wbr>//fileRead();<br><wbr><wbr>//bufferdWrite();<br><wbr><wbr>//bufferdRead();</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr>//通过缓冲区拷贝一个.java文件<br><wbr><wbr>copyJavaFile();<br><wbr>}</wbr></wbr></wbr></wbr></wbr>

<wbr>public static void fileWrite()<br><wbr>{<br><wbr><wbr>FileWriter fw = null;<wbr> //fw.close()调用需要引用fw.<br><wbr><wbr>try<br><wbr><wbr>{<br><wbr><wbr><wbr>fw = new FileWriter("iodemo.txt", false); //第二个参数决定是否向覆盖原文件.<br><wbr><wbr><wbr>fw.write("sddddddd");<wbr><wbr><wbr><br><wbr><wbr><wbr>fw.write("\r\naaa");<wbr>//windows\r\n为一个回车符.<wbr><wbr><br><wbr><wbr>}<br><wbr><wbr>catch (Exception e)<br><wbr><wbr>{<br><wbr><wbr><wbr>System.out.println(e.toString());<br><wbr><wbr>}<br><wbr><wbr>finally<br><wbr><wbr>{<br><wbr><wbr><wbr>try<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>fw.close();<br><wbr><wbr><wbr>}<br><wbr><wbr><wbr>catch (Exception e)<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>System.out.println(e.toString());<br><wbr><wbr><wbr>}<br><wbr><wbr>}<br><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr>public static void fileRead()<br><wbr>{<br><wbr><wbr>//读取文件通用格式:<br><wbr><wbr>//建立字符流读取对象,于指定文件关联<br><wbr><wbr>FileReader fr = null;<br><wbr><wbr>try<br><wbr><wbr>{<br><wbr><wbr><wbr>fr = new FileReader("iodemo.txt");<br><wbr><wbr><wbr>char[] arr = new char[1024];<wbr> //将读出的数据存入到数组中.(缓存)<br><wbr><wbr><wbr>int num = 0;<br><wbr><wbr><wbr>while ((num = fr.read(arr)) != -1) //没有读到数据后,返回-1,循环结束;read方法可以自动向后读取剩下的数据,<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>System.out.println(new String(arr, 0, num));<br><wbr><wbr><wbr>}<br><wbr><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>


<wbr><wbr>catch (Exception e)<br><wbr><wbr>{<br><wbr><wbr><wbr>System.out.println(e.toString());<br><wbr><wbr>}<br><wbr><wbr>finally<br><wbr><wbr>{<br><wbr><wbr><wbr>try<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>fr.close();<br><wbr><wbr><wbr>}<br><wbr><wbr><wbr>catch (Exception e)<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>System.out.println(e.toString());<br><wbr><wbr><wbr>}<br><wbr><wbr>}<br><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr>public static void bufferdWrite()<br><wbr>{<br><wbr><wbr>//创建流对象<wbr><wbr><br><wbr><wbr>FileWriter fw = null;<br><wbr><wbr>//创建缓冲区跟流对象关联。<br><wbr><wbr>BufferedWriter bfdw = null;<br><wbr><wbr>try<br><wbr><wbr>{<br><wbr><wbr><wbr>fw = new FileWriter("iodemo.txt");<br><wbr><wbr><wbr>bfdw = new BufferedWriter(fw);<br><wbr><wbr><wbr>int i = 0;<br><wbr><wbr><wbr>while (i &lt; 5)<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>bfdw.write("aaa" + i);<br><wbr><wbr><wbr><wbr>bfdw.newLine();<wbr><br><wbr><wbr><wbr><wbr>bfdw.flush();<br><wbr><wbr><wbr><wbr>i++;<br><wbr><wbr><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr>bfdw.write("xxx"); //不调用flush或close,这一行数据不能写入.<br><wbr><wbr><wbr>bfdw.newLine();<br><wbr><wbr>}<br><wbr><wbr>catch (Exception e)<br><wbr><wbr>{<br><wbr><wbr><wbr>System.out.println(e.toString());<br><wbr><wbr>}<br><wbr><wbr>finally<br><wbr><wbr>{<br><wbr><wbr><wbr>try<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>bfdw.close();<wbr> //缓冲区的关闭是调用传入流的关闭方法.<br><wbr><wbr><wbr>}<br><wbr><wbr><wbr>catch (Exception e)<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>System.out.println(e.toString());<br><wbr><wbr><wbr>}<br><wbr><wbr>}<br><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr>public static void bufferdRead()<br><wbr>{<br><wbr><wbr>//创建流对象<br><wbr><wbr>FileReader fr = null;<br><wbr><wbr>//创建缓冲区跟流对象关联。<br><wbr><wbr>BufferedReader bfdr = null;<br><wbr><wbr>try<br><wbr><wbr>{<br><wbr><wbr><wbr>fr = new FileReader("iodemo.txt");<wbr><wbr><wbr><wbr><br><wbr><wbr><wbr>bfdr = new BufferedReader(fr);<br><wbr><wbr><wbr>String s;<br><wbr><wbr><wbr>while ( (s = bfdr.readLine()) != null)<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>System.out.println(s);<br><wbr><wbr><wbr>}<br><wbr><wbr>}<br><wbr><wbr>catch (Exception e)<br><wbr><wbr>{<br><wbr><wbr><wbr>System.out.println(e.toString());<br><wbr><wbr>}<br><wbr><wbr>finally<br><wbr><wbr>{<br><wbr><wbr><wbr>try<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>bfdr.close();<wbr> //缓冲区的关闭是调用传入流的关闭方法.<br><wbr><wbr><wbr>}<br><wbr><wbr><wbr>catch (Exception e)<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>System.out.println(e.toString());<br><wbr><wbr><wbr>}<br><wbr><wbr>}<br><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr>public static void copyJavaFile()<br><wbr>{<br><wbr><wbr>//创建流对象<br><wbr><wbr>FileReader fr = null;<br><wbr><wbr>FileWriter fw = null;<br><wbr><wbr>//创建缓冲区跟流对象关联。<br><wbr><wbr>BufferedReader bfdr = null;<br><wbr><wbr>BufferedWriter bfdw = null;<br><wbr><wbr>try<br><wbr><wbr>{<br><wbr><wbr><wbr>fr = new FileReader("iodemo.java");<br><wbr><wbr><wbr>fw = new FileWriter("iodemo复件.txt");<br><wbr><wbr><wbr>bfdr = new BufferedReader(fr);<br><wbr><wbr><wbr>bfdw = new BufferedWriter(fw);<br><wbr><wbr><wbr>String line;<br><wbr><wbr><wbr>while ( (line = bfdr.readLine()) != null)<wbr> //readLine没有读取回车符,只读取了回车符前的数据,因此写入文本时需要执行newLine方法.<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>bfdw.write(line);<br><wbr><wbr><wbr><wbr>bfdw.newLine();<br><wbr><wbr><wbr><wbr>bfdw.flush();<br><wbr><wbr><wbr><wbr>//System.out.println(line);<br><wbr><wbr><wbr>}<br><wbr><wbr>}<br><wbr><wbr>catch (IOException e)<br><wbr><wbr>{<br><wbr><wbr><wbr>System.out.println(e.toString());<br><wbr><wbr>}<br><wbr><wbr>finally<br><wbr><wbr>{<br><wbr><wbr><wbr>//对两个缓冲区分别进行关闭<br><wbr><wbr><wbr>if (bfdw != null)<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>try<br><wbr><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr>bfdw.close();<wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr><wbr>catch (Exception e)<br><wbr><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr>System.out.println(e.toString());<br><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr>}<br><wbr><wbr><wbr><br><wbr><wbr><wbr>if (bfdr != null)<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>try<br><wbr><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr>bfdr.close();<wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr><wbr>catch (Exception e)<br><wbr><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr>System.out.println(e.toString());<br><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr>}<br><wbr><wbr>}<br><wbr>}<br> }<br></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

你可能感兴趣的:(Writer)