import java.io.*;class FileReaderDemo{    public static void main(String[]args){        copy();//method2();    }            /**    第一种文本文件读取方式,通过FileReader类中的read()方法    一次只能取一个字符,并且逐步往下取,取不到返回-1.    必须执行流资源的关闭,在关闭前先判断流对象是否存在。    */    public static void method1(){        //定义一个读取流        FileReader fr = null;        try{            fr = new FileReader("test1.txt");                    int ch = 0;            while((ch = fr.read())!= -1){                System.out.println("ch="+(char)ch)    ;            }        }            catch(IOException e){                        System.out.println("asd。。。。。。。。。。"+e.toString());//            //throw new   RuntimeException("文件读取失败!");        }        finally{            try{//必须对流进行判断是否存在                if (fr != null)                    fr.close();            }            catch(IOException e){                            System.out.println("asdssss。。。。"+e.toString());                //throw new RuntimeException("文件读取失败!");            }        }    }        /**    第二种文本文件读取方式,通过FileReader类中的read(char[] arr)方法    */    public static void method2(){        FileReader fr = null;        try{            fr = new FileReader("test.txt");            int num = 0;            //定义字符数组,用于存储字符            char arr[] = new char[1024];            while((num = fr.read(arr))!= -1){                //将字符数组转换为字符串,要从0开始取,取num个字符。                String s = new String(arr,0,num);                //打印字符串                System.out.println("arr="+s);                }            }            catch(IOException e){            System.out.println(e.toString());                }        finally{            try{                if(fr!= null)                    fr.close();            }            catch(IOException e){                System.out.println(e.toString());                    }            }    }    //打印.java文件    public static void print(){        FileReader fr = null;        try{            fr = new FileReader("CharAt.java");                int len = 0;            char arr[] = new char[1024];            while((len = fr.read(arr)) != -1){                System.out.println(new String(arr,0,len));                }        }            catch(IOException e){            System.out.println(e.toString());            }        finally{            try{                if(fr != null)                    fr.close();                }                catch(IOException e){                System.out.println(e.toString());                }        }    }        //复制文本文件内容    public static void copy(){        FileWriter fw = null;        FileReader fr = null;        try{            //创建文本文件            fw = new FileWriter("E:\\CharAt1.java");                    fr = new FileReader("D:\\CharAt.java");                        int num = 0;            char[] arr = new char[1024];            while((num = fr.read(arr))!= -1){                fw.write(arr,0,num);                }        }        catch(IOException e){            System.out.println(e.toString());        }        finally{            try{                if(fw != null)                 fw.close();                }                catch(IOException e){                System.out.println(e.toString());            }            try{                if(fw != null)                 fr.close();                }                catch(IOException e){                System.out.println(e.toString());            }        }    }}