1. 字符流

  字符流是以Reader和Writer为基础派生的一系列类

 

2. 字符输入流FileReader

  FileReader构造方法

public class FileReader extends InputStreamReader    //继承InputStreamReader

public FileReader(String fileName)    //创建一个指定文件名的字符输入流

public FileReader(File file)    //创建一个指定文件对象的字符输入流

public FileReader(FileDescriptor fd)    //创建一个指定文件描述符的字符输入流

  

  FileReader实例(读取字符,只能读取文本文件)

    public static void main(String[] args){
        FileReader fr = null;
        
        try{
            fr = new FileReader("C:\\Users\\THINK\\Desktop\\JavaTest\\Temp.txt");
            char[] chars = new char[4];
            int readData = 0;
            while((readData = fr.read(chars))!=-1){
                System.out.print(new String(chars,0,readData));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            if(fr != null){
                try{
                    fr.close();
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        }
    }

 

3. 字符输出流FileWriter

  FileWriter构造方法

public class FileWriter extends OutputStreamWriter    //继承OutputStreamWriter

public FileWriter(String fileName)    //创建一个指定文件名的字符输出流

public FileWriter(String fileName, boolean append)    //是否追加

public FileWriter(File file)    //创建也给指定文件对象的字符输出流

public FileWriter(File file, boolean append)    //是否追加

public FileWriter(FileDescriptor fd)    //指定字符描述符

public FileWriter(String fileName, Charset charset)    //创建一个使用命名字符集的字符输出流

public FileWriter(String fileName, Charset charset, boolean append)    //是否追加

public FileWriter(File file, Charset charset)    //使用命名字符集

public FileWriter(File file, Charset charset, boolean append)    //是否追加

 

  FileWriter实例

public class FileWriterTest01{
    public static void main(String[] args){
        FileWriter fw = null;
        
        try{
            fw = new FileWriter("C:\\Users\\THINK\\Desktop\\JavaTest\\Temp01.txt",true);
            char[] chars = {'奥','运','会'};
            fw.write(chars);
            fw.write(chars,1,2);    //可以写入指定长度字符
            fw.write("helloworld");     //可以直接写入字符串
            fw.write("hellojava");
            fw.flush();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            if(fw!=null){
                try{
                    fw.close();
            }catch(Exception e){
                e.printStackTrace();
            }
            }
        }
    }
}

 

  使用FileReader和FileWriter进行文本文件的拷贝(只能拷贝普遍文本文件)

public class CopyTest02{
    public static void main(String[] args){
        FileReader fr = null;
        FileWriter fw = null;
        
        try{
            fr = new FileReader("D:\\JavaProject\\chapter19.iml");
            fw = new FileWriter("C:\\Users\\THINK\\Desktop\\JavaTest\\chapter19.iml");
            char[] chars = new char[1024*512];
            int readData = 0;
            while((readData = fr.read(chars)) != -1){
                fw.write(chars,0,readData);
            }
            fw.flush();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            if(fw != null){
                try{
                    fw.close();
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
            if(fr != null){
                try{
                    fr.close();
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
}

 

4. 字符缓冲输入流BufferedReader

  BufferedReader是缓冲字符输入流,为其他字符输入流添加缓冲功能。  

  BufferedReader构造方法

public class BufferedReader extends Reader    //继承Reader

public BufferedReader(Reader in, int sz)    //创建指定大小输入缓冲区的缓冲字符输入流

public BufferedReader(Reader in)    //创建默认大小输入缓冲区的缓冲字符输入流

 

  BufferedReader实例

public class BufferedReaderTest01{
    public static void main(String[] args) throws IOException{
        FileReader fr = new FileReader("C:\\Users\\THINK\\Desktop\\JavaTest\\chapter19.iml");
        BufferedReader br = new BufferedReader(fr);    //需要传入一个字符流
        
    /*    String strLine1 = br.readLine();    //可以直接读取一行
        System.out.println(strLine1);
        
        String strLine2 = br.readLine();
        System.out.println(strLine2);  */
        
        String strLine = null;
        while((strLine = br.readLine())!=null){
            System.out.println(strLine);
        }
        br.close();   //只需关闭缓冲流,因为在源码中已经关闭了字符流
    }
}

 

5. 字符缓冲输出流BufferedWriter

  BufferedWriter是缓冲字符输出流,为其他字符输出流添加缓冲功能。

  BufferedWriter构造方法

public class BufferedWriter extends Writer    //继承Writer

public BufferedWriter(Writer out)    //创建默认大小的缓冲字符输出流
 
public BufferedWriter(Writer out, int sz)    //创建指定大小的缓冲字符输出流

 

  BufferedWriter实例

public class BufferedWriterTest01{
    public static void main(String[] args) throws IOException{
        FileWriter fw = new FileWriter("C:\\Users\\THINK\\Desktop\\JavaTest\\Temp04.txt");
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write("hello world");
        bw.write("hello java");
        bw.flush();
        bw.close();
    }
}

 

6. 转换流InputStreamReader

  InputStreamReader作用是将字节输入流转换为字符输入流,继承Reader。

  InputStreamReader实例

public class InputStreamReaderTest01{
    public static void main(String[] args) throws IOException{
        
    /*    FileInputStream fis = new FileInputStream("C:\\Users\\THINK\\Desktop\\JavaTest\\chapter19.iml");
        InputStreamReader isr = new InputStreamReader(fis);    //BufferedReader使用字符流作为参数,所以要转换
        BufferedReader br = new BufferedReader(isr);    //参数要传入一个字符流    */
        
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("C:\\Users\\THINK\\Desktop\\JavaTest\\chapter19.iml")));    //以上三行可以写到一块
        String strLine = null;
        while((strLine = br.readLine()) != null){
            System.out.println(strLine);
        }
        br.close();
    } 
}

 

7. 转换流OutputStreamWriter

  OutputStreamWriter作用是将字节输出流转换为字符输入流,继承Writer。

  OutputStreamWriter实例

public class OutputStreamWriterTest01{
    public static void main(String[] args) throws IOException{
        
    /*    FileOutputStream fos = new FileOutputStream("C:\\Users\\THINK\\Desktop\\JavaTest\\Temp05.txt");
        OutputStreamWriter osw = new OutputStreamWriter(fos);
        BufferedWriter bw = new BufferedWriter(osw);  */
        
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("C:\\Users\\THINK\\Desktop\\JavaTest\\Temp05.txt",true)));
        
        bw.write("hello java");
        bw.write("hello olympic");
        bw.flush();
        bw.close();
    }
}

 

posted on 2021-08-08 15:15  homle  阅读(35)  评论(0编辑  收藏  举报