文件复制
import java.io.*;
public class FileCopy {
public static void main(String[] args) throws IOException {
String srcPath = "src.png";
String destPath = "dest.png";
File srcFile = new File(srcPath);
if (!srcFile.exists()) {
System.out.println("原文件不存在," + srcPath);
return;
}
if (srcFile.isDirectory()) {
System.out.println("原文件是目录," + srcPath);
return;
}
File destFile = new File(destPath);
if (destFile.exists()) {
System.out.println("目标文件已存在," + destPath);
return;
}
try (InputStream is = new FileInputStream(srcFile)) {
try (OutputStream os = new FileOutputStream(destFile)) {
byte[] buffer = new byte[4096];
int len;
while ((len = is.read(buffer, 0, 4096)) != -1) {
os.write(buffer, 0, len);
}
os.flush();
}
}
}
}
文件加密解密
import java.io.*;
public class Encrypt {
public static void main(String[] args) throws IOException {
String secretKey = "Java14班";
String srcPath = "原始情报文件.txt";
String destPath = "加密后的情报文件.txt";
try (InputStream is = new FileInputStream(srcPath)) {
try (OutputStream os = new FileOutputStream(destPath)) {
byte[] buffer = new byte[4096];
int len;
while ((len = is.read(buffer, 0, 4096)) != -1) {
encrypt(buffer, 0, len, secretKey);
os.write(buffer, 0, len);
}
os.flush();
}
}
}
private static byte calcSecretKey(String secretKey) throws IOException {
byte k = 0;
for (byte b : secretKey.getBytes("UTF-8")) {
k += b;
}
return k;
}
private static void encrypt(byte[] buffer, int offset, int length, String secretKey) throws IOException {
for (int i = 0; i < length; i++) {
buffer[i + offset] = (byte)(buffer[i + offset] + calcSecretKey(secretKey));
}
}
}
import java.io.*;
public class Decrypt {
public static void main(String[] args) throws IOException {
String secretKey = "Java14班";
String srcPath = "加密后的情报文件.txt";
String destPath = "解密后的情报文件.txt";
try (InputStream is = new FileInputStream(srcPath)) {
try (OutputStream os = new FileOutputStream(destPath)) {
byte[] buffer = new byte[4096];
int len;
while ((len = is.read(buffer, 0, 4096)) != -1) {
decrypt(buffer, 0, len, secretKey);
os.write(buffer, 0, len);
}
os.flush();
}
}
}
private static byte calcSecretKey(String secretKey) throws IOException {
byte k = 0;
for (byte b : secretKey.getBytes("UTF-8")) {
k += b;
}
return k;
}
private static void decrypt(byte[] buffer, int offset, int length, String secretKey) throws IOException {
for (int i = 0; i < length; i++) {
buffer[i + offset] = (byte)(buffer[i + offset] - calcSecretKey(secretKey));
}
}
}
字符集转化程序
import java.io.*;
public class IConv {
public static void main(String[] args) throws IOException {
if (args.length < 4) {
System.out.println("请使用如下方式使用程序: ");
System.out.println("IConv 源字符集 目标字符集 源文件 目标文件");
return;
}
String fromCharset = args[0];
String toCharset = args[1];
String fromPath = args[2];
String toPath = args[3];
try (Reader reader = new InputStreamReader(new FileInputStream(fromPath), fromCharset)) {
try (Writer writer = new OutputStreamWriter(new FileOutputStream(toPath), toCharset)) {
char[] buffer = new char[4096];
int len;
while ((len = reader.read(buffer)) != -1) {
writer.write(buffer, 0, len);
}
writer.flush();
}
}
}
}
复制所有程序,包括文件夹
import java.io.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class CopyDirectory {
private static class CopyFileTask implements Runnable {
private final String srcPath;
private final String destPath;
CopyFileTask(String srcPath, String destPath) {
this.srcPath = srcPath;
this.destPath = destPath;
}
@Override
public void run() {
try {
copyFile(srcPath, destPath);
System.out.println(destPath + " 文件复制成功");
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static String srcAbsPath;
private static String desAbsPath;
private static ExecutorService threadPool;
public static void main(String[] args) throws IOException {
threadPool = Executors.newFixedThreadPool(50);
String srcPath = "E:\\videos";
String destPath = "E:\\比特科技\\目标\\目标目录";
File srcFile = new File(srcPath);
File destFile = new File(destPath);
srcAbsPath = srcFile.getAbsolutePath();
desAbsPath = destFile.getAbsolutePath();
copyDirectory(srcPath, destPath);
threadPool.shutdown();
}
private static void copyDirectory(String srcPath, String destPath) throws IOException {
File srcRoot = new File(srcPath);
travelDirectory(srcRoot);
}
private static void doDirectory(File directory) {
String srcPath = directory.getAbsolutePath();
String relativePath = srcPath.substring(srcAbsPath.length());
String destPath = desAbsPath + relativePath;
File destDirectory = new File(destPath);
destDirectory.mkdir();
System.out.println(destPath + " 目录创建成功");
}
private static void doFile(File file) throws IOException {
String srcPath = file.getAbsolutePath();
String relativePath = srcPath.substring(srcAbsPath.length());
String destPath = desAbsPath + relativePath;
threadPool.execute(new CopyFileTask(srcPath, destPath));
}
private static void copyFile(String srcPath, String destPath) throws IOException {
try (InputStream is = new FileInputStream(srcPath)) {
try (OutputStream os = new FileOutputStream(destPath)) {
byte[] buffer = new byte[4096];
int len;
while ((len = is.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
os.flush();
}
}
}
private static void doNode(File node) throws IOException {
if (node.isDirectory()) {
doDirectory(node);
} else {
doFile(node);
}
}
private static void travelDirectory(File root) throws IOException {
doNode(root);
if (root.isDirectory()) {
File[] files = root.listFiles();
if (files != null) {
for (File file : files) {
travelDirectory(file);
}
}
}
}
}