-
大师讲解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.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();
}
}
}
第三种:使用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();
}
}
}
第四种:借助于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,从入门到精通..2024-03-17
- 快速安装高效插件,让你成为百家号大..2024-03-13
- 360装机大师轻松安装Win7系统:简单易..2023-12-29
- 揭秘:如何轻松打开神秘的PSD文件,让..2023-12-29
- 罗技G703鼠标灵敏度调整秘籍:让你成..2023-12-25
- 检测屏幕坏点?轻松掌握鲁大师,轻松..2023-12-23
相关推荐
- 如何修改WIN7用户目录位置 转移win7用户..2023-01-11
- 如何设置自动双面打印机,如何设置默..2017-10-12
- crashdumps文件夹是什么,里面的DMP文件可..2023-02-20
- 华为系统更新安装包怎么删除..2021-01-28
- 小米路由器硬盘怎么访问 如何远程访..2021-05-19
- excel表格求和函数,excel求和函数..2017-10-12