Java是一门面标的目的对象编程说话,不仅接收了C++说话的各类长处,还摒弃了C++里难以理解的多担当、指针等概念,是以Java说话具有功能壮大和简单易用两个特征。Java说话作为静态面标的目的对象编程说话的代表,极好地实现了面标的目的对象理论,许可程序员以优雅的思维体例进行复杂的编程 。
Java具有简单性、面标的目的对象、分布式、健壮性、平安性、平台自力与可移植性、多线程、动态性等特点 。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等 。
东西/原料
- 电脑
- myeclipse和intellij IDEA
方式/步调
- 1
第一种:根基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();
}
}
- 2
第二种:借助于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();
}
}
}
- 3
第三种:利用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();
}
}
}
- 4
第四种:借助于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("原文件不存在!");
}
}
}
注重事项
- jdk 1.6 ,jdk 1.8 myeclipse 2010 ,IDEA 2018.2.2
- 上面复制文件的体例,若是文件名字已经存在则笼盖已有文件
来源:百闻(微信/QQ号:9397569),转载请保留出处和链接!
本文链接:https://www.ibaiwen.com/web/280322.html
- 上一篇: 电脑桌面杂乱无章,应该怎么整理
- 下一篇: cdr如何制作生肖蛇图标
- 热门文章
-
WB蒙特利尔(WB Montreal)——欧美十大最差视频游戏开发商
迅猛龙(Velociraptor)——欧美史前十大死亡动物
什么是果酱猫(What Marmalade Cats)?
神奇蜘蛛侠2(The Amazing Spider-Man 2)——欧美最佳蜘蛛侠电影
faceu激萌怎么把瘦脸开到最大
希瑟(Heather)——欧美十大最佳柯南灰歌
奥兹奥斯本(Ozzy Osbourne)——欧美十大高估歌手
二人梭哈
什么是小脑前下动脉(Anterior Inferior Cerebellar Artery)?
我应该知道康涅狄格州的什么(What Should I Know About Connecticut)?
- 热评文章
- 最新评论
-
- 最近访客
-
- 站点信息
-
- 文章总数:261580
- 页面总数:9
- 分类总数:1
- 标签总数:0
- 评论总数:0
- 浏览总数:74382