Discuss / Java / 作业

作业

Topic source

import java.io.*;

public class CopyFile {

    public static void main(String[] args) throws IOException{

        //读取文件路径

        String ReadFilePath = "./1.txt";

        //写入文件路径

        String WriteFilePath = "./2.txt";

        //读取文件的内存大小

        int readfileLimit = 1024;

        copyfile(ReadFilePath, WriteFilePath, readfileLimit);

    }

    /*

     * 复制文件

     * */

    public static void copyfile(String ReadFilePath, String WriteFilePath, int readfileLimit) throws IOException {

        int n = 0;

        //写入对象

        OutputStream outputStream = null;

        byte[] buffer = new byte[readfileLimit];

        try(InputStream inputStream = new FileInputStream(ReadFilePath)){

            outputStream = new FileOutputStream(WriteFilePath);

            while (true){

                n = inputStream.read();

                if(n == -1){

                    break;

                }

                outputStream.write(n);

            }

        }catch (IOException e){

            System.out.println("复制失败!");

            e.printStackTrace();

        }finally {

            if(outputStream != null) {

                outputStream.close();

            }

        }

    }

}


  • 1

Reply