java 本地文件复制的方法

本文介绍三种使用Java进行文件复制的方法:直接使用文件字节流、使用带缓冲区的字节流以及利用FileChannel进行NIO传输复制和缓冲复制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用java代码复制一个本地文件

1)直接使用文件字节流的方式读取:

    private static void writeFile1(String name) throws Exception {
        File file = new File(name);
        if (file.isFile()) {
            OutputStream fos = new FileOutputStream(file + ".bak");
            InputStream fis = new FileInputStream(file);
            System.out.println(file.length());
            byte[] buf = new byte[4096];
            int len;
            while ((len = fis.read(buf)) != -1) {
                fos.write(buf, 0, len);
            }
            fis.close();
            fos.close();
        }
    }

2)使用带缓冲区的字节流读取:

    private static void writeFile2(String name) throws Exception {
        File file = new File(name);
        if (file != null && file.isFile()) {
            OutputStream fos = new FileOutputStream(file + ".bak");
            InputStream fis = new FileInputStream(file);
            BufferedInputStream bis = new BufferedInputStream(fis);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            System.out.println(file.length());
            byte[] buf = new byte[4096];
            int len;
            while ((len = bis.read(buf)) != -1) {
                bos.write(buf, 0, len);
            }
            bos.flush();
            fis.close();
            fos.close();
        }
    }

3)使用FileChannel

	private static void nioTransferCopy(String name) throws Exception {  
		File file = new File(name);
		if (file != null && file.isFile()) {
			System.out.println(file.length());
		    FileChannel in = null;  
		    FileChannel out = null;  
		    FileInputStream inStream = null;  
		    FileOutputStream outStream = null;
		    try {  
		        inStream = new FileInputStream(file);
		        outStream = new FileOutputStream(file + ".bak"); 
		        in = inStream.getChannel();  
		        out = outStream.getChannel();  
		        in.transferTo(0, in.size(), out);  
		    } catch (IOException e) {  
		        e.printStackTrace();  
		    } finally {  
		        in.close();
		        inStream.close();
		        out.close();
		        outStream.close();
		    }
		}
	}
	
	private static void nioBufferCopy(String name) throws Exception {
		File file = new File(name);
		if (file != null && file.isFile()) {
			System.out.println(file.length());
		    FileChannel in = null;  
		    FileChannel out = null;  
		    FileInputStream inStream = null;  
		    FileOutputStream outStream = null;  
		    try {  
		        inStream = new FileInputStream(file);
		        outStream = new FileOutputStream(file + ".bak"); 
		        in = inStream.getChannel();  
		        out = outStream.getChannel();  
		        ByteBuffer buffer = ByteBuffer.allocate(4096);  
		        while (in.read(buffer) != -1) {  
		            buffer.flip();  
		            out.write(buffer);  
		            buffer.clear();  
		        }  
		    } catch (IOException e) {  
		        e.printStackTrace();  
		    } finally {  
		        in.close();
		        inStream.close();
		        out.close();
		        outStream.close(); 
		    } 
		}
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值