zoukankan      html  css  js  c++  java
  • IO流-文件拷贝

    其实文件的拷贝还是文件读取写入的应用,实际是读取此路径上的文件,然后写入到指定路径下的文件。

    代码举例:

    import java.io.*;
    import java.lang.*;
    class Test
    {
        public static void main(String[] args)
        {
            copy("C:\log.txt", "d:\log.txt");
        }
        //块读取方式
        public static void copy(String sourcePath, String destPath)
        {
            final int READCACHELEN = 1024;
            try(FileReader fileReader = new FileReader(sourcePath))
            {
                int readReturnNum = 0;
                char[] readCache = new char[READCACHELEN];
                try(FileWriter fileWriter = new FileWriter(destPath, true))//定义添加为真
                {
                    while((readReturnNum = fileReader.read(readCache)) != -1)
                    {
                        fileWriter.write(readCache, 0, readReturnNum);
                    }
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            
           System.out.println("transport Ok!");
        }
        //单个字符读取方式
        public static void copy(String sourcePath, String destPath)
        {
            final int READCACHELEN = 1024;
            try(FileReader fileReader = new FileReader(sourcePath))
            {
                int readReturnNum = 0;
                try(FileWriter fileWriter = new FileWriter(destPath, true))
                {
                    while((readReturnNum = fileReader.read()) != -1)//read读取单个字符以整型返回
                    {
                        fileWriter.write(readReturnNum);
                    }
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            
           System.out.println("transport Ok!");
        }
    }
  • 相关阅读:
    html5 存储(删除)
    java 单例模式
    android知识点汇总
    java多线程 与 并发
    cuda GPU 编程之共享内存的使用
    vs 2015 + OPENGL 配置
    Ubuntu 14.04 安装 CUDA 问题及解决
    性能分析工具gprof介绍
    vim 换行方式 win 转Ubuntu vim 格式调整
    计算显卡对比
  • 原文地址:https://www.cnblogs.com/fantasy01/p/3997146.html
Copyright © 2011-2022 走看看