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!");
        }
    }
  • 相关阅读:
    PDF格式简单分析
    python 2.x 版本 pip 的使用
    网络读书笔记-运输层
    网络读书笔记-应用层
    线程池源码解析
    管道流创建订单
    @autowire、@resource原理
    Spring如何解决循环依赖
    结合Spring特性实现策略模式
    自定义注解+AOP实现redis分布式锁
  • 原文地址:https://www.cnblogs.com/fantasy01/p/3997146.html
Copyright © 2011-2022 走看看