当前位置:网站首页>File copy method
File copy method
2022-07-03 22:27:00 【User 2700206】
package top.my.test.case1;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
public class FileCopyTest {
public static void main(String[] args) {
// File size is 4.5G
System.out.println(System.currentTimeMillis());
// 1595581151315 -- 1595581253196 = 101881ms = 101s
// copyFile(new File("D:\\xl\\big.mkv"), new File("D:\\big.mkv"));
// 1595582378585 -- 1595582548529 = 169944ms = 169s
// fileChannelCopy(new File("D:\\xl\\big.mkv"), new File("D:\\big2.mkv"));
// 1595582683903 -- 1595582805496 = 121593ms = 121s
// fileCopy(new File("D:\\xl\\big.mkv"), new File("D:\\big2.mkv"));
// 1595583767345 -- 1595583897985 = 130640ms = 130s
// filesCopy(new File("D:\\xl\\big.mkv"), new File("D:\\big2.mkv"));
//1595584222455 -- 1595584325169 = 102714ms = 102s
copyBigFile(new File("D:\\xl\\big.mkv"), new File("D:\\big.mkv"));
System.out.println(System.currentTimeMillis());
}
// Single file copy
public static boolean copyFile(File fromFile, File toFile) {
try (FileInputStream in = new FileInputStream(fromFile); FileOutputStream os = new FileOutputStream(toFile);) {
byte[] b = new byte[1024];
int n = 0;
while ((n = in.read(b)) != -1) {
os.write(b, 0, n);
}
in.close();
os.close();
System.out.println(" End of file copy ");
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static boolean filesCopy(File s, File t) {
Path sourcePath = Paths.get(s.getAbsolutePath());
Path destinationPath = Paths.get(t.getAbsolutePath());
try {
Files.copy(sourcePath, destinationPath, StandardCopyOption.REPLACE_EXISTING);
return true;
} catch (IOException e) {
// something else went wrong
e.printStackTrace();
}
return false;
}
public static boolean fileCopy(File s, File t) {
FileInputStream fi = null;
FileOutputStream fo = null;
FileChannel in = null;
FileChannel out = null;
try {
fi = new FileInputStream(s);
fo = new FileOutputStream(t);
in = fi.getChannel();// Get the corresponding file channel
out = fo.getChannel();// Get the corresponding file channel
long left_size = in.size();
System.out.println(" Source file size :" + left_size / 1024 / 1024);
long position = 0;
while (left_size > 0) {
long write_size = in.transferTo(position, left_size, out);
position += write_size;
left_size -= write_size;
}
// in.transferTo(0, in.size(), out);// Connecting two channels , And from in Channel read , And then write out passageway
System.out.println("FileChannel End of file copy ");
System.out.println(" Target file size :" + out.size() / 1024 / 1024);
return true;
} catch (IOException e) {
System.out.print(" File copy exception :{}" + e.getMessage());
;
} finally {
try {
fi.close();
in.close();
fo.close();
out.close();
} catch (IOException e) {
System.out.print(" File copy exception :{}" + e.getMessage());
}
}
return false;
}
public static boolean fileChannelCopy(File s, File t) {
FileChannel in = null;
FileChannel out = null;
RandomAccessFile fi = null;
RandomAccessFile fo = null;
try {
if (!t.isFile()) {
if (!t.createNewFile()) {
return false;
}
}
fi = new RandomAccessFile(s, "r");
fo = new RandomAccessFile(t, "rw");
in = fi.getChannel();// Get the corresponding file channel
out = fo.getChannel();// Get the corresponding file channel
long left_size = in.size();
long position = 0;
while (left_size > 0) {
long write_size = in.transferTo(position, left_size, out);
position += write_size;
left_size -= write_size;
}
// in.transferTo(0, in.size(), out);// Connecting two channels , And from in Channel read , And then write out passageway
System.out.println("FileChannel End of file copy ");
return true;
} catch (IOException e) {
System.out.print(" File copy exception :{}" + e.getMessage());
;
} finally {
try {
fi.close();
in.close();
fo.close();
out.close();
} catch (IOException e) {
System.out.print(" File copy exception :{}" + e.getMessage());
}
}
return false;
}
public static boolean copyBigFile(File s, File t) {
FileInputStream fi = null;
FileOutputStream fo = null;
FileChannel in = null;
FileChannel out = null;
ByteBuffer buffer = ByteBuffer.allocate(10 * 1024);
try {
fi = new FileInputStream(s);
fo = new FileOutputStream(t);
in = fi.getChannel();// Get the corresponding file channel
out = fo.getChannel();// Get the corresponding file channel
while (true) {
int read = in.read(buffer);
if (read == -1)
break;
buffer.flip();
out.write(buffer);
buffer.clear();
}
System.out.println("ByteBuffer End of file copy ");
return true;
} catch (IOException e) {
System.err.print(" File copy exception :{}" + e.getMessage());
} finally {
try {
fi.close();
in.close();
fo.close();
out.close();
} catch (IOException e) {
System.err.print(" File copy exception :{}" + e.getMessage());
}
}
return false;
}
}边栏推荐
- Blue Bridge Cup -- guess age
- [actual combat record] record the whole process of the server being attacked (redis vulnerability)
- Ansible common usage scenarios
- Blue Bridge Cup Guoxin Changtian single chip microcomputer -- software environment (II)
- string
- Some 5000+ likes, the development notes of a director of cosmic factory, leaked
- regular expression
- IPhone development swift foundation 08 encryption and security
- 1 Introduction to spark Foundation
- Sed、Awk
猜你喜欢

Shell script three swordsman awk

Exclusive interview with the person in charge of openkruise: to what extent has cloud native application automation developed now?

Go Technology Daily (2022-02-13) - Summary of experience in database storage selection

Unique in China! Alibaba cloud container service enters the Forrester leader quadrant

STM32 multi serial port implementation of printf -- Based on cubemx

2 spark environment setup local

1068. Consolidation of ring stones (ring, interval DP)

Mysql database - Advanced SQL statement (I)

webAssembly

How the computer flushes the local DNS cache
随机推荐
Classification and extension of OC
How can enterprises and developers take advantage of the explosion of cloud native landing?
Harbor integrated LDAP authentication
6.0 kernel driver character driver
Programming language (1)
[golang] leetcode intermediate - alphabetic combination of island number and phone number
Mindmanager2022 serial number key decompression installer tutorial
Quick one click batch adding video text watermark and modifying video size simple tutorial
Report on the development strategy of China's engineering bidding agency and suggestions for the 14th five year plan Ⓙ 2022 ~ 2028
[flax high frequency question] leetcode 426 Convert binary search tree to sorted double linked list
Wisdom tooth technology announced that it had completed the round D financing of US $100million and had not obtained a valid patent yet
Kali2021.4a build PWN environment
Exclusive interview with the person in charge of openkruise: to what extent has cloud native application automation developed now?
Development mode and Prospect of China's IT training industry strategic planning trend report Ⓣ 2022 ~ 2028
BUUCTF,Misc:LSB
Programming language (2)
Electronic tube: Literature Research on basic characteristics of 6j1
Analysis report on the development trend and Prospect of global and Chinese supercontinuum laser source industry Ⓚ 2022 ~ 2027
js demo 計算本年度還剩下多少天
[sg function]split game (2020 Jiangxi university student programming competition)