0
点赞
收藏
分享

微信扫一扫

2022-3-28作业

禾木瞎写 2022-03-30 阅读 36
java

编写代码完成复制粘贴的功能

 分别使用字符输入输出流FileWriter/FileReader

public class zy1 {
    public static void main(String[] args) throws IOException {
        //通过Reader 读取目标文件中的内容
        //通过Writer 将读取到的内容写入指定位置
        FileReader reader = new FileReader("111.txt");
        FileWriter write = new FileWriter("111_副本.txt");
        while (true){
            int read = reader.read();
            if (read != -1){
                write.write(read);
            }else {
                break;
            }
        }
        reader.close();
        write.close();
        System.out.println();
    }
}


 文件输入输出流FileInputStream/FileOutputStream

public class zy2 {
    public static void main(String[] args) throws IOException {
        FileInputStream in = new FileInputStream("C:\\Users\\fate.j\\IdeaProjects\\untitled\\1.mp4");
        FileOutputStream out = new FileOutputStream("C:\\Users\\fate.j\\IdeaProjects\\untitled\\1_副本.mp4");
        long start = System.currentTimeMillis();
        byte[] bytes = new byte[1024];
        while(true){
            int r = in.read(bytes);
            if (r == -1){
                break;
            }else {
                out.write(bytes , 0 ,r);
            }
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);
        in.close();
        out.close();
    }
}


 文件缓冲流BufferedInputStream/BufferedOutputStream

public class zy3 {
    public static void main(String[] args) throws IOException {
        //BufferOutputStream 缓冲输出流 ,目的:提高输出流的·写的效率
        //创建一个文件输出流
        FileOutputStream fos = new FileOutputStream("C:\\Users\\fate.j\\IdeaProjects\\untitled\\1.mp4");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        //创建一个缓冲输出流bos,并将文件输出流fos ,交给bos来提高写的效率
        //操作bos就等于间接的操作了fos
        FileInputStream fis = new FileInputStream("C:\\Users\\fate.j\\IdeaProjects\\untitled\\1_副本.mp4");
        BufferedInputStream bis = new BufferedInputStream(fis);
        long start = System.currentTimeMillis();
        byte[] bytes = new byte[1024];
        while(true){
            int r = bis.read(bytes);
            if (r == -1){
                break;
            }else {
                bos.write(r);
            }
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);
        fos.close();
        bos.close();
        fis.close();
        bis.close();
    }
}


 字符缓冲流BufferedWriter/BufferedReader

public class zy4 {
    public static void main(String[] args) throws IOException {
        //通过Reader 读取目标文件中的内容
        //通过Writer 将读取到的内容写入指定位置
        FileReader reader = new FileReader("111.txt");
        BufferedReader br = new BufferedReader(reader);

        FileWriter write = new FileWriter("111_副本.txt");
        BufferedWriter bw = new BufferedWriter(write);

        char[] chars = new char[1024];
        while (true){
            int r = br.read();
            if (r == -1 ){
                break;
            }else {
                bw.write(r);
            }
        }
        br.close();
        bw.close();
    }
}


 4种方式实现
 

举报

相关推荐

0 条评论