IO文件复制粘贴

项目

package com.I_O;

import java.io.*;
/**
 * 文件文件
 */
public class Copy_I_O {
    /**
     * 复制粘贴方法
     */
    public void copy(){
        InputStream is=null;
        OutputStream os=null;
        try {
            is=new FileInputStream("复制.txt");
            os=new FileOutputStream("粘贴.txt");
            byte[] bytes=new byte[1024];
            int len=-1;
            try {
                while((len=is.read(bytes))!=-1){
                    System.out.println("复制完成");
                }      //复制对象
                os.write(bytes,0,bytes.length);    //粘贴对象
                System.out.println("粘贴成功");

                /**
                 * 遍历集合
                 */
                System.out.println("复制的内容:");
                for(byte b:bytes){
                    if(b==0){
                        continue;
                    }
                    System.out.print((char)b);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


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

    }



    /**
     * 创建文件方法
     */
    public void newfile(){
        File file=new File("复制.txt");
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 复制粘贴main方法
     */
    public static void main(String[] args) {
        Copy_I_O c=new Copy_I_O();
        c.copy();
    }
}

运行

猜你喜欢

转载自www.cnblogs.com/zzh630/p/10485144.html