欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

文件读写操作2

程序员文章站 2022-07-12 19:29:20
...
/** 通过字符IO进行文件读写 */
        FileReader reader = null;
        FileWriter writer = null;
        char[] charArr = new char[512];
        try
        {
            reader = new FileReader(srcFilePathName);
            writer = new FileWriter(targetFilePathName);
            start = System.currentTimeMillis();
            while (reader.read(charArr) != -1)
            {
                writer.write(charArr);
            }
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                reader.close();
                writer.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        end = System.currentTimeMillis();
        long time5 = end - start;
        System.out.println("IO_char: " + time5);

        /** 直接通过IO读写 */
        FileInputStream fin6 = null;
        FileOutputStream fout6 = null;
        c = -1;
        try
        {
            fin6 = new FileInputStream(srcFilePathName);
            fout6 = new FileOutputStream(targetFilePathName);
            start = System.currentTimeMillis();
            while ((c = fin6.read()) != -1)
            {
                fout6.write(c);
            }
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                fin6.close();
                fout6.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        end = System.currentTimeMillis();
        long time6 = end - start;
        System.out.println("IO_nobuffer: " + time6);
相关标签: io