java 本地文件复制的方法

使用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(); 
		    } 
		}
	}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Java的IO流和Java文件操作类来实现复制本地图片的功能。具体步骤如下: 1. 创建一个输入流,读取本地图片文件。 ```java FileInputStream fis = new FileInputStream("D:/image.jpg"); ``` 2. 创建一个输出流,将读取到的图片数据写入到目标文件中。 ```java FileOutputStream fos = new FileOutputStream("D:/image_copy.jpg"); ``` 3. 创建一个缓存区,用于存储读取到的图片数据。 ```java byte[] buffer = new byte[1024]; ``` 4. 循环读取数据,并将读取到的数据写入到目标文件中。 ```java int len; while ((len = fis.read(buffer)) != -1) { fos.write(buffer, 0, len); } ``` 5. 关闭输入流和输出流。 ```java fis.close(); fos.close(); ``` 完整代码示例: ```java import java.io.*; public class ImageCopyDemo { public static void main(String[] args) { try { // 1. 创建一个输入流,读取本地图片文件 FileInputStream fis = new FileInputStream("D:/image.jpg"); // 2. 创建一个输出流,将读取到的图片数据写入到目标文件中 FileOutputStream fos = new FileOutputStream("D:/image_copy.jpg"); // 3. 创建一个缓存区,用于存储读取到的图片数据 byte[] buffer = new byte[1024]; // 4. 循环读取数据,并将读取到的数据写入到目标文件中 int len; while ((len = fis.read(buffer)) != -1) { fos.write(buffer, 0, len); } // 5. 关闭输入流和输出流 fis.close(); fos.close(); System.out.println("图片复制成功!"); } catch (IOException e) { e.printStackTrace(); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值