IO的最基本操作:字符流文件的读写

来源:互联网 发布:郑州软件开发工资 编辑:程序博客网 时间:2024/05/24 06:42

Io流的本质就是对文件的操作,Java中提供了很多相关的类。
下面是我在学习的过程中自己写的一些简单代码。

//首先是直接使用FileReafder和FileWriter进行操作  分别用单个字符和数组的方式进行操作import java.io.*;class FileReaderDemo{    public static void main(String[]args) throws IOException    {//        method1();        method2();    }    public static void method1()    {        FileReader fr = null;        FileWriter fw = null;        try        {            fr = new FileReader("E:\\IDEA_workspace\\MapDemo\\src\\MapDemo.java");            fw = new FileWriter("Write.txt");            int ch;            while((ch = fr.read())!=-1)            {                fw.write(ch);            }        }        catch(IOException e)        {            sop(e.toString());        }        finally        {            if(fr!=null)            {                try                {                    fr.close();                }                catch (IOException e)                {                    sop(e.toString());                }            }            if(fw!=null)            {                try                {                    fw.close();                }                catch (IOException e)                {                    sop(e.toString());                }            }        }    }    public static void method2()    {        FileReader fr = null;        FileWriter fw = null;        try        {            fr = new FileReader("E:\\IDEA_workspace\\MapDemo\\src\\MapDemo.java");            fw = new FileWriter("Write1.txt");            char[] ch = new char[1024];            int lenth;            while((lenth = fr.read(ch))!=-1)            {                fw.write(ch,0,lenth);            }        }        catch(IOException e)        {            sop(e.toString());        }        finally        {            if(fr!=null)            {                try                {                    fr.close();                }                catch(IOException e)                {                    sop(e.toString());                }            }            if(fw!=null)            {                try                {                    fw.close();                }                catch(IOException e)                {                    sop(e.toString());                }            }        }    }    public  static void sop(Object obj)    {        System.out.println(obj);    }}
//然后是使用BufferedReader和BufferedWriter缓冲区进行操作效率比较高    public static void method3()    {        BufferedReader bfr = null;        BufferedWriter bfw = null;        try        {            bfr = new BufferedReader(new FileReader("E:\\IDEA_workspace\\MapDemo\\src\\MapDemo.java"));            bfw = new BufferedWriter(new FileWriter("Write3.txt"));            String line= null;            while((line = bfr.readLine())!=null)            {                bfw.write(line);                bfw.newLine();                bfw.flush();            }        }        catch(IOException e)        {            sop(e.toString());        }        finally        {                if(bfr!=null)                {                    try                    {                        bfr.close();                    }                    catch(IOException e)                    {                        sop(e.toString());                    }                }            if(bfw!=null)            {                try                {                    bfw.close();                }                catch(IOException e)                {                    sop(e.toString());                }            }        }    }
原创粉丝点击