一、描述
对txt、pdf等类型文件,通过输入文件路径以及待保存的文件夹路径,对相应的文件进行拷贝。输入的示例文件大小约为20MB。
二、源代码
FileCopyTool.java
import java.io.*;
import java.nio.channels.FileChannel;
public class FileCopyTool {
/** 源文件目录 */
private static String sourceFilePath;
/** 目标文件夹目录 */
private static String targetDirectoryPath;
/** 目标文件目录 */
private static String targetFilePath;
public FileCopyTool(String sourceFilePath, String targetDirectoryPath) {
this.sourceFilePath = sourceFilePath;
this.targetDirectoryPath = targetDirectoryPath;
getTargetFilePath();
try {
copyFile();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 判断指定目录下文件是否存在,存在返回true,否则返回false
* @param Path 文件目录
* */
public static boolean judgeIfFileExist(String Path){
File file = new File(Path);
if (!file.exists()) {
return false;
} else {
return true;
}
}
/**
* 判断指定目录下文件夹是否存在,不存在则新建文件夹
* @param Path 文件夹目录
* */
public static void judgeIfFolderExist(String Path){
File folder = new File(Path);
if (!folder.exists() && !folder.isDirectory()) {
folder.mkdirs();
}
}
/**
* 获取目标文件保存目录
* */
private static void getTargetFilePath() {
File temp = new File(sourceFilePath);
String fileName = temp.getName();
targetFilePath = targetDirectoryPath +"\\"+ fileName;
}
/**
* 文件拷贝
* */
private static void copyFile() throws IOException {
File source = new File(sourceFilePath);
File target = new File(targetFilePath);
FileChannel sourceChannel = null;
FileChannel targetChannel = null;
try {
sourceChannel = new FileInputStream(source).getChannel();
targetChannel = new FileOutputStream(target).getChannel();
targetChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
} finally {
sourceChannel.close();
targetChannel.close();
}
}
}
FileCopyGUI.java
import javax.swing.*;
import java.awt.*;
import java.io.File;
public class FileCopyGUI extends JFrame {
private final Container container = getContentPane();
public FileCopyGUI() {
container.setLayout(null);
setContent();
setWindow();
}
/**
* 窗口设置
* */
private void setWindow() {
this.setVisible(true);
this.setSize(600, 270);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setTitle("文件复制界面");
}
/**
* 组件设置
* */
private void setContent() {
/* 标签 */
JLabel jLabel1 = new JLabel("源文件目录");
jLabel1.setBounds(60, 30, 200, 50);
jLabel1.setFont(new Font("楷体", Font.PLAIN, 20));
JLabel jLabel2 = new JLabel("目标文件目录");
jLabel2.setBounds(60, 80, 200, 50);
jLabel2.setFont(new Font("楷体", Font.PLAIN, 20));
/* 文本域 */
JTextField jTextField1 = new JTextField(20);
jTextField1.setBounds(220, 40, 300, 30);
JTextField jTextField2 = new JTextField(20);
jTextField2.setBounds(220, 90, 300, 30);
/* 按钮 */
JButton jButton = new JButton("开始复制");
jButton.setBounds(200, 160, 200, 40);
jButton.setFont(new Font("楷体", Font.PLAIN, 20));
jButton.addActionListener(e -> {
try {
String sourceFilePath = jTextField1.getText();
String targetDirectoryPath = jTextField2.getText();
if (sourceFilePath.equals("") || targetDirectoryPath.equals("")){
new Prompt("内容不能为空!");
return;
}
File file = new File(sourceFilePath);
if(file.isDirectory()){
new Prompt("请输入文件路径!");
return;
}
File folder = new File(targetDirectoryPath);
if(folder.isFile()){
new Prompt("请输入文件夹路径!");
return;
}
if (!FileCopyTool.judgeIfFileExist(sourceFilePath))
new Prompt("该文件不存在,请重新输入!");
else {
FileCopyTool.judgeIfFolderExist(targetDirectoryPath);
new FileCopyTool(sourceFilePath, targetDirectoryPath);
new Prompt("文件复制成功!");
}
} catch (NumberFormatException a) {
new Prompt("该文件不存在,请重新输入!");
}
});
container.add(jLabel1);
container.add(jLabel2);
container.add(jTextField1);
container.add(jTextField2);
container.add(jButton);
}
}
/**
* 错误提示窗口
* */
class Prompt extends JFrame {
Container container = getContentPane();
String promptMessage;
public Prompt(String promptMessage){
this.promptMessage = promptMessage;
container.setLayout(null);
setWindow();
setContent();
}
private void setWindow(){
this.setVisible(true);
this.setSize(420,200);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setTitle("提示");
}
private void setContent(){
JLabel jLabel = new JLabel(promptMessage);
jLabel.setBounds(70,30,300,80);
jLabel.setFont(new Font("楷体",Font.PLAIN,20));
jLabel.setForeground(Color.red);
container.add(jLabel);
}
}
Main.java
public class Main {
public static void main(String []args){
// 源文件目录示例 = "E:\\Java\\JavaGuide面试突击版.pdf";
// 目标文件目录示例 = "E:\\Java\\test";
new FileCopyGUI();
}
}
三、效果
-
拷贝成功
拷贝前:
拷贝界面:
拷贝后:
-
输入错误信息进行提示