java-io流--(六)--文件io复制

参考:https://blog.csdn.net/qq_43470725/article/details/121283862
https://blog.csdn.net/Enjoy96/article/details/78650182

详情参考:io流

1、文本io流的复制 代码演示:

package com.example.io.fileIo.copy.charCopy;

import java.io.*;

public class CharCopyTest01 {

    public static void main(String[] args) {

        CharCopyTest01 charCopyTest01 = new CharCopyTest01();
        String path = charCopyTest01.getPath();
        path = getPath01(path);
        System.out.println(path);
//        创建文件夹和文件并返回File:
        File file = createFile(path,"test","\\test.txt");
//        创建要复制到目标的文件夹及文件:
        File fileCopy = createFile(path, "test", "\\testCopy.txt");

//        复制文件
        try {
//            一次读一个字符数组
//            copyFile(file, fileCopy);
//            一次读一个字符
//            copyFileByOne(file, fileCopy);
//            一次读一行
//            copyFileByLine(file,fileCopy);
            //基本字符流读取一个字符数组(效率低)
//            copyFileByBasic(file, fileCopy);
            //基本字符流读取一个字符(效率低)
//            copyFileByBasicOne(file, fileCopy);
//            采用打印流PrintWriter的println()方法进行文件的复制
            copyFileByPrintWriter(file, fileCopy);

        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    //    得到该类的路径
    public String getPath(){
        String path = this.getClass().getResource("").getPath();
        return path;
    }

    public static String getPath01(String pathIn){

        pathIn = pathIn.substring(1,pathIn.length());
        pathIn = pathIn.replace("/", "\\");
        System.out.println("pathIn==="+pathIn);
        return pathIn;
    }

    public static File createFile(String path,String filePath,String fileName){
//        创建文件夹
        path = path + filePath;
        File file = new File(path);
        if(!file.exists()){
            file.mkdir();
        }
//        创建文件
        path = path + fileName;
        System.out.println("path---last===="+path);
        file = new File(path);
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return file;
    }

//    缓冲字符流一次读取一个字符数组
    public static void copyFile(File pathSource,File pathCopyRoot) throws IOException {
//        封装数据源:
//        创建一个文件字节输入流对象
        BufferedReader br = new BufferedReader(new FileReader(pathSource));
//        封装目的地
//        创建文件输出流对象
        BufferedWriter bw = new BufferedWriter(new FileWriter(pathCopyRoot));

//        一次读取一个字符数组
        char[] chs = new char[1024];
        int len = 0;
        while ((len = br.read(chs)) != -1){
//            一次读一个字符数组,使用输出流给testCopy.txt文件写一个字符数组
            bw.write(chs, 0, len);
//            刷新流
            bw.flush();

        }
//        释放资源
        br.close();
        bw.close();

    }

    //    缓冲字符流一次读取一个字符
    public static void copyFileByOne(File pathSource,File pathCopyRoot) throws IOException {
//        封装数据源:
//        创建一个文件字节输入流对象
        BufferedReader br = new BufferedReader(new FileReader(pathSource));
//        封装目的地
//        创建文件输出流对象
        BufferedWriter bw = new BufferedWriter(new FileWriter(pathCopyRoot));


        int ch = 0;
        while ((ch = br.read()) != -1){
//            一次读一个字符数组,使用输出流给testCopy.txt文件写一个字符数组
            bw.write(ch);
//            刷新流
            bw.flush();

        }
//        释放资源
        br.close();
        bw.close();

    }

//    缓冲字符流一次读一行:
public static void copyFileByLine(File pathSource,File pathCopyRoot) throws IOException {
//        封装数据源:
//        创建一个文件字节输入流对象
    BufferedReader br = new BufferedReader(new FileReader(pathSource));
//        封装目的地
//        创建文件输出流对象
    BufferedWriter bw = new BufferedWriter(new FileWriter(pathCopyRoot));

// 使用BufferedReader中的特有功能public String readLine() 一次读取一行
    String str = null;
    while ((str = br.readLine()) != null){
// 一次读一行,使用输出流给b.txt文件写入此行字符串
        bw.write(str);
//BufferedWriter中的特有功能public void newLine() 写入一个分行符
        bw.newLine();
//        刷新流
        bw.flush();

    }
//        释放资源
    br.close();
    bw.close();

}

//基本字符流读取一个字符数组(效率低)
    public static void copyFileByBasic(File pathSource,File pathCopyRoot) throws IOException {

        FileReader fr = new FileReader(pathSource);

        FileWriter fw = new FileWriter(pathCopyRoot);

        char[] chs = new char[1024];
        int len = 0;
        while ((len = fr.read(chs)) != -1){

            fw.write(chs,0,len);
            fw.flush();

        }
        fr.close();
        fw.close();

    }
    //基本字符流读取一个字符(效率低)
    public static void copyFileByBasicOne(File pathSource,File pathCopyRoot) throws IOException {

        FileReader fr = new FileReader(pathSource);

        FileWriter fw = new FileWriter(pathCopyRoot);

        int ch = 0;
        while ((ch = fr.read()) != -1){

            fw.write(ch);
            fw.flush();

        }
        fr.close();
        fw.close();

    }

//    采用打印流PrintWriter的println()方法进行文件的复制
public static void copyFileByPrintWriter(File pathSource,File pathCopyRoot) throws IOException{

        BufferedReader br = new BufferedReader(new FileReader(pathSource));

        PrintWriter pw = new PrintWriter( new FileWriter(pathCopyRoot),true);// 此种构造方式可以自动刷新

//        读取数据
        String line = null;
        while ((line = br.readLine()) != null){
//            写入数据,读取一行并启动自动换行
            pw.println(line);

        }

//        释放资源
        br.close();
        pw.close();

}


}

2、文件(图片、视频等)的复制 代码演示:

package com.example.io.fileIo.copy.byteCopy;

import java.io.*;

public class ByteCopyTest01 {

    public static void main(String[] args) {

        ByteCopyTest01 byteCopyTest01 = new ByteCopyTest01();
        String path = byteCopyTest01.getPath();
        path = getPath01(path);
        System.out.println(path);
        //        创建文件夹和文件并返回File:
        File file = createFlode(path,"test");

        String jpgPathSource = "D:\\2.jpg";
        try {
            String jpgPathRoot = file.getCanonicalPath().toString();
            System.out.println(jpgPathRoot);
            jpgPathRoot = jpgPathRoot + "\\2copy.jpg";
            //      缓冲字节流一次读一个字节数组
//            copyFileByBuffer(jpgPathSource, jpgPathRoot);
            //      缓冲字节流一次读一个字节
//            copyFileByBufferOne(jpgPathSource, jpgPathRoot);
            //采用基本字节输入流,一次读一个字节数组
//            copyFileByBasic(jpgPathSource, jpgPathRoot);
            //    采用基本字节输入流,一次读一个字节
//            copyFileByBasicOne(jpgPathSource, jpgPathRoot);
            //    采用打印流PrintStream的writer()方法进行图片的复制
            copyFileByPrintStream(jpgPathSource, jpgPathRoot);


        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    //    得到该类的路径
    public String getPath(){
        String path = this.getClass().getResource("").getPath();
        return path;
    }

    public static String getPath01(String pathIn){

        pathIn = pathIn.substring(1,pathIn.length());
        pathIn = pathIn.replace("/", "\\");
        System.out.println("pathIn==="+pathIn);
        return pathIn;
    }

//    创建文件
    public static File createFile(String path, String filePath, String fileName){
//        创建文件夹
        path = path + filePath;
        File file = new File(path);
        if(!file.exists()){
            file.mkdir();
        }
//        创建文件
        path = path + fileName;
        System.out.println("path---last===="+path);
        file = new File(path);
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return file;
    }
    //    创建文件夹
    public static File createFlode(String path, String filePath){
//        创建文件夹
        path = path + filePath;
        File file = new File(path);
        if(!file.exists()){
            file.mkdir();
        }
        return file;
    }

//    缓冲字节流一次读一个字节数组

    public static void copyFileByBuffer(String pathSource,String pathRoot) throws IOException {

        //获取当前的系统时间
        long startTime = System.currentTimeMillis() ;

        // 封装数据源:
        // 创建一个文件字节输入流对象
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(pathSource));

        // 封装目的地
        // 创建文件输出流对象
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(pathRoot));//如果没有文件,会自动创建


        // 使用高效字节输入流,一次读取一个字节数组
        byte[] bytes = new byte[1024];
        int len;
        while ((len = bis.read(bytes)) != -1){
            // 一次读一个字节数组,使用输出流给Marco.jpg文件写一个字节数组
            bos.write(bytes,0,len);
//            刷新流
            bos.flush();
        }

//        释放资源
        bis.close();
        bos.close();

//        时间测试
        long endTime = System.currentTimeMillis() ;
        System.out.println("共耗时:"+(endTime-startTime)+"毫秒");
    }

//      缓冲字节流一次读一个字节
public static void copyFileByBufferOne(String pathSource,String pathRoot) throws IOException {

    //获取当前的系统时间
    long startTime = System.currentTimeMillis() ;

    // 封装数据源:
    // 创建一个文件字节输入流对象
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(pathSource));

    // 封装目的地
    // 创建文件输出流对象
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(pathRoot));//如果没有文件,会自动创建


    // 使用高效字节输入流,一次读取一个字节

    int by;
    while ((by = bis.read()) != -1){
        // 一次读一个字节数组,使用输出流给Marco.jpg文件写一个字节数组
        bos.write(by);
//            刷新流
        bos.flush();
    }

//        释放资源
    bis.close();
    bos.close();

//        时间测试
    long endTime = System.currentTimeMillis() ;
    System.out.println("共耗时:"+(endTime-startTime)+"毫秒");
}

//采用基本字节输入流,一次读一个字节数组

    public static void copyFileByBasic(String pathSource,String pathRoot) throws IOException {

        // 获取当前的系统时间
        long startTime = System.currentTimeMillis();

        // 封装数据源:
        // 创建一个文件字节输入流对象
        FileInputStream fis = new FileInputStream(pathSource);

        // 封装目的地
        // 创建文件输出流对象
        FileOutputStream fos = new FileOutputStream(pathRoot);

        // 使用基本字节输入流,一次读取一个字节数组
        byte[] bytes = new byte[1024];
        int len = 0;
        while ((len = fis.read(bytes)) != -1){
            // 一次读一个字节数组,使用输出流给Marco.jpg文件写一个字节数组
            fos.write(bytes,0,len);
//            刷新流
            fos.flush();

        }

//        释放资源
        fis.close();
        fos.close();
        // 时间测试
        long endTime = System.currentTimeMillis();
        System.out.println("共耗时:" + (endTime - startTime) + "毫秒"); // 1249毫秒

    }

//    采用基本字节输入流,一次读一个字节
public static void copyFileByBasicOne(String pathSource,String pathRoot) throws IOException {

    // 获取当前的系统时间
    long startTime = System.currentTimeMillis();

    // 封装数据源:
    // 创建一个文件字节输入流对象
    FileInputStream fis = new FileInputStream(pathSource);

    // 封装目的地
    // 创建文件输出流对象
    FileOutputStream fos = new FileOutputStream(pathRoot);

    // 使用基本字节输入流,一次读取一个字节
    int by = 0;
    while ((by = fis.read()) != -1){
        // 一次读一个字节,使用输出流给02test.jpg文件写一个字节
        fos.write(by);
//        刷新流
        fos.flush();

    }

//    释放资源
    fis.close();
    fos.close();

    // 时间测试
    long endTime = System.currentTimeMillis();
    System.out.println("共耗时:" + (endTime - startTime) + "毫秒");

    }

//    采用打印流PrintStream的writer()方法进行图片的复制
public static void copyFileByPrintStream(String pathSource,String pathRoot) throws IOException {
    // 获取当前的系统时间
    long startTime = System.currentTimeMillis();

    // 封装数据源
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(pathSource));

    // 封装目的地,使用字符打印流对象并启动自动刷新功能
    PrintStream ps = new PrintStream(new FileOutputStream(pathRoot),true); // 此种构造方式可以自动刷新

//    读取数据,一次读取一个字节数组
    byte[] bytes = new byte[1024];
    int len = 0;
    while ((len = bis.read(bytes)) != -1){

//        写入数据,一次写一个字节数组
        ps.write(bytes,0,len);
    }

//    释放资源
    bis.close();
    ps.close();

    // 时间测试
    long endTime = System.currentTimeMillis();
    System.out.println("共耗时:" + (endTime - startTime) + "毫秒");

    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来回答你的问题。 首先,让我们来了解一下什么是 EAV 模型。EAV 模型是一种数据建模的方法,它将所有的数据存储在一个三元组 (实体、属性、值) 中。其中,实体表示存储的对象,属性表示实体的某个特征,值则表示该属性的具体取值。这种模型可以用于存储非结构化数据和动态的数据,但是在查询和维护方面会存在一些挑战。 接下来,让我们来看一下如何实现简单的转存。Java 中提供了许多用于文件操作的类和方法,其中最常用的是 java.io 包中的类。我们可以使用 FileInputStream 和 FileOutputStream 类来实现文件的读写操作。 例如,我们可以通过以下代码将源文件的内容复制到目标文件中: ```java import java.io.*; public class FileCopy { public static void main(String[] args) { try { FileInputStream in = new FileInputStream("source.txt"); FileOutputStream out = new FileOutputStream("target.txt"); byte[] buffer = new byte[1024]; int length; while ((length = in.read(buffer)) > 0) { out.write(buffer, 0, length); } in.close(); out.close(); System.out.println("File copied successfully!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 在上面的代码中,我们首先创建了一个 FileInputStream 对象来读取源文件,然后创建了一个 FileOutputStream 对象来写入目标文件。接着,我们使用一个字节数组作为缓冲区,每次读取 1024 个字节,并将其写入目标文件中。最后,我们关闭了输入输出流,完成了文件复制操作。 希望这个简单的例子能够帮助你入门 Java IO 操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值