系统吧 专业的电脑操作系统,支持重装各种原版系统xp/win7/win8/win10 这个系统怎么样?试试系统吧系统,体验办公系统畅快愉悦感。 各种电脑系统安装过程一样,神一般的畅快!
-->
当前位置:主页 > 电脑技术 >

大师讲解java文件复制功能实现

作者:李虹汲 分类:电脑技术 发布时间:2017-09-19 18:04:36

电脑现已成为我们工作、生活和娱乐必不可少的工具了,在使用电脑的过程中,可能会遇到java文件复制功能实现的问题,如果我们遇到了java文件复制功能实现的情况,该怎么处理怎么才能解决java文件复制功能实现带来的困扰呢,对于这样的问题其实我们只需要电脑myeclipse和intellij IDEA第一种:基本copy通过字节流。1、具体代码如下所示:import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileI这样就...

电脑现已成为我们工作、生活和娱乐必不可少的工具了,在使用电脑的过程中,可能会遇到java文件复制功能实现的问题,如果我们遇到了java文件复制功能实现的情况,该怎么处理怎么才能解决java文件复制功能实现带来的困扰呢,对于这样的问题其实我们只需要电脑myeclipse和intellij IDEA第一种:基本copy通过字节流。1、具体代码如下所示:import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileI这样就解决了这样的问题,接下来给大家带来java文件复制功能实现的详细操作步骤。

工具/原料

电脑
myeclipse和intellij IDEA

方法/步骤

第一种:基本copy通过字节流。

1、具体代码如下所示:

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

public class Test8 {

public static void main(String[] args) {

ByteArrayOutputStream bos = null;

BufferedInputStream in = null;

try {

File file = new File("D:/Documents/Downloads/新建文件夹 (2)/代理合同.pdf");

if (!file.exists()) {

throw new FileNotFoundException("file not exists");

}

in = new BufferedInputStream(new FileInputStream(file));

bos = new ByteArrayOutputStream((int) file.length());

int buf_size = 1024;

byte[] buffer = new byte[buf_size];

int len = 0;

while (-1 != (len = in.read(buffer, 0, buf_size))) {

bos.write(buffer, 0, len);

}

copyFile(bos.toByteArray(), "d:/test.pdf");

System.out.println(bos.toByteArray());

} catch (Exception e) {}

}

public static void copyFile(byte[] fileByte, String filePath)

throws Exception {

File file = new File(filePath);

FileOutputStream fs = new FileOutputStream(file);

BufferedOutputStream bo = new BufferedOutputStream(fs);

bo.write(fileByte);

bo.close();

}

}

大师讲解java文件复制功能实现

大师讲解java文件复制功能实现

第二种:借助于java.nio.channels.FileChannel实现复制文件。

代码如下所示:

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.nio.channels.FileChannel;

public class Test8 {

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

File source = new File("d:/test.pdf");

if (source.exists()) {

File dest = new File("d:/test2.pdf");

copyFileChannels(source, dest);

} else {

System.out.println("原文件不存在!");

}

}

public static void copyFileChannels(File source, File dest)

throws IOException {

FileChannel inputChannel = null;

FileChannel outputChannel = null;

try {

inputChannel = new FileInputStream(source).getChannel();

outputChannel = new FileOutputStream(dest).getChannel();

outputChannel.transferFrom(inputChannel, 0, inputChannel.size());

} finally {

inputChannel.close();

outputChannel.close();

}

}

}

大师讲解java文件复制功能实现

大师讲解java文件复制功能实现

大师讲解java文件复制功能实现

第三种:使用java7之后提供的java.nio.file.Files实现。

代码如下:

import java.io.*;

import java.nio.file.Files;

public class CopyTest {

public static void main(String[] args) {

File inFile = new File("E:/图片/捉妖记.jpg");

File outFile = new File("E:/file/捉妖记.jpg");

try {

Files.copy(inFile.toPath(), outFile.toPath());

} catch (IOException e) {

e.printStackTrace();

}

}

}

大师讲解java文件复制功能实现

第四种:借助于Apache服务器提供类org.apache.commons.io.FileUtils

1、类在架包commons-io.jar中

2、下载架包

2.1 百度搜索:commons-io.jar下载

2.2

3、具体实现代码:

import java.io.File;

import org.apache.commons.io.FileUtils;

public class Test8 {

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

File source = new File("d:/test.pdf");

if (source.exists()) {

File dest = new File("d:/test3.pdf");

FileUtils.copyFile(source, dest);

} else {

System.out.println("原文件不存在!");

}

}

}

大师讲解java文件复制功能实现

大师讲解java文件复制功能实现

大师讲解java文件复制功能实现

xp 更多>>
win7 更多>>
win8 更多>>
win10 更多>>
U盘 更多>>
电脑技术 更多>>
网站地图 | 豫ICP备2021035069号-4 | 友情链接qq:191064436
系统吧

版权所有 © 2012-2023 系统吧 免责声明:本站资源均收集于互联网,其著作权归原作者所有,如果有侵犯您权利的资源,请来信告知,我们将及时删除相应资源。