Java是一門面標的目的對象編程說話,不僅接收了C++說話的各類長處,還摒棄了C++里難以理解的多擔當、指針等概念,是以Java說話具有功能壯大和簡單易用兩個特征。Java說話作為靜態面標的目的對象編程說話的代表,極好地實現了面標的目的對象理論,許可程序員以優雅的思維體例進行復雜的編程 。
Java具有簡單性、面標的目的對象、分布式、健壯性、平安性、平臺自力與可移植性、多線程、動態性等特點 。Java可以編寫桌面應用程序、Web應用程序、分布式系統和嵌入式系統應用程序等 。
第一種:根基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("原文件不存在!");
}
}
}
0 篇文章
如果覺得我的文章對您有用,請隨意打賞。你的支持將鼓勵我繼續創作!