java的io流相关类使用

InputStreamReader类与OutputStreamWriter类

InputStreamReader
将字节流转换为字符流。是字节流通向字符流的桥梁。如果不指定字符集编码,该解码过程将使用平台默认的字符编码,如:GBK。 构造方法: InputStreamReader isr = new InputStreamReader(InputStream in);//构造一个默认编码集的InputStreamReader类

InputStreamReader isr = new InputStreamReader(InputStream in,String charsetName);//构造一个指定编码集的InputStreamReader类。

参数 in(InputStream )对象获得方法:

InputStream in = System.in;//读取键盘上的数据。
InputStream in = new FileInputStream(String fileName);//读取文件中的数据。

InputStreamReader 主要方法:

int read();//读取单个字符。
int read(char []cbuf);//将读取到的字符存到数组中。返回读取的字符数。

例子:

     public static void main(String[] args) throws IOException {
        // 读取字节流
        FileInputStream fis = new FileInputStream("d:\\ab.txt");
        // 将字节流向字符流的转换。
        InputStreamReader isr = new InputStreamReader(fis);
        //创建字符流缓冲区 使用缓冲区 可以使用缓冲区对象的 read()
        //  和 readLine()方法。     
        BufferedReader bufr = new BufferedReader(isr);
        String line = null;
        while ((line = bufr.readLine()) != null) {
            System.out.println(line);
        }
        isr.close();
        } 

OutputStreamWriter 将字节流转换为字符流。是字节流通向字符流的桥梁。如果不指定字符集编码,该解码过程将使用平台默认的字符编码,如:GBK。

构造方法:

OutputStreamWriter osw = new OutputStreamWriter(OutputStream out);//构造一个默认编码集的OutputStreamWriter类

OutputStreamWriter osw = new OutputStreamWriter(OutputStream out,String charsetName);//构造一个指定编码集的OutputStreamWriter类。

参数对象(OutputStream)

InputStream out = System.out;获得。//打印到控制台上。
InputStream out = new FileoutputStream(String fileName);//输出到文件中

主要方法:

        void write(int c);//将单个字符写入。

        viod write(String str,int off,int len);//将字符串某部分写入。

        void flush();//将该流中的缓冲数据刷到目的地中去。

例子:

    public static void main(String[] args) throws IOException {
        OutputStream out = new FileOutputStream("D:\\ab.txt");// 打印到文件。
        OutputStreamWriter osr = new OutputStreamWriter(out);// 输出
        BufferedWriter bufw = new BufferedWriter(osr);// 缓冲
        String str = "你  ff 好吗?\r\n我很好!";// 你好吗?
        bufw.write(str);
        bufw.flush();
        bufw.close();
        osr.close();
        out.close();
    }

FileReader类与FileWriter类

1.FileWriter类(字符输出流类)
继承OutputStreamWriter 这里写图片描述 构造方法:

   FileWriter fw = new FileWriter(String fileName);//创建字符输出流类对象和已存在的文件相关联。文件不存在的话,并创建。如 FileWriter fw = new FileWriter("C:\\demo.txt");
   FileWriter fw = new FileWriter(String fileName,boolean append);//创建字符输出流类对象和已存在的文件相关联,并设置该该流对文件的操作是否为续写。如:FileWriter fw = new  FileWriter("C:\\demo.txt",ture); //表示在fw对文件再次写入时,会在该文件的结尾续写,并不会覆盖掉。

主要方法:

     void write(String str)   //写入字符串。当执行完此方法后,字符数据还并没有写入到目的文件中去。此时字符数据会保存在缓冲区中。此时在使用刷新方法就可以使数据保存到目的文件中去。
     viod flush()                //刷新该流中的缓冲。将缓冲区中的字符数据保存到目的文件中去。
     viod close()               //关闭此流。在关闭前会先刷新此流的缓冲区。在关闭后,再写入或者刷新的话,会抛IOException异常。

2.FileReader类
继承InputStreamReader 这里写图片描述 1,构造方法

FileReader fr = new FileReader(String fileName);//使用带有指定文件的String参数的构造方法。创建该输入流对象。并关联源文件。

2,主要方法

int read(); // 读取单个字符。返回作为整数读取的字符,如果已达到流末尾,则返回 -1。
int read(char []cbuf);//将字符读入数组。返回读取的字符数。如果已经到达尾部,则返回-1。
void close();//关闭此流对象。释放与之关联的所有资源。

FileReaderAndFileWriter例子

        // 复印
    public static void main(String[] args) throws IOException {
        FileReader fr = null;
        BufferedReader br = null;
        FileWriter fw = null;
        BufferedWriter bw = null;
        try {
            // 继承InpuStreamReader
            fr = new FileReader("d:\\ab.txt");
            br = new BufferedReader(fr);
            // 需要加\n换行 ,printWriter不需要
            fw = new FileWriter("d:\\abnew.txt");
            bw = new BufferedWriter(fw);

            String one_line;
            while ((one_line = br.readLine()) != null) {
                // 注意加\n

                bw.write(one_line);
                bw.newLine();

            }

            bw.flush();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                bw.close();
            }
            if (fw != null) {
                fw.close();
            }
            if (br != null) {
                br.close();
            }
            if (fr != null) {
                fr.close();
            }
        }
    }