• 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!");
        }
    }
  • 相关阅读:
    音频,视频简单运用
    转载:Linux Used内存到底到哪里去了?
    shell awk统计重复个数
    Java中的单例模式
    Grub启动配置文件
    C语言实现全排列
    C语言缓冲区清空
    c语言内存对齐(1)
    防盗链原理
    C语言内存对齐(2)
  • 原文地址:https://www.cnblogs.com/fantasy01/p/3997146.html
Copyright © 2020-2023  润新知