分析
在使用系统的时候,有时候需要一个更轻巧的小窗口来对文件资源操作。
为此,我们团队设计了一个简易文件资源管理器。
参考之前做过的C语言命令行的管理系统,这个将会更有挑战,要做出GUI。
大致常用的功能为:创建新文件 创建一个文件夹 增删改查 socker通信发送
所以将gui中大致分为
创建文件
创建文件夹
复制
粘贴
重命名
返回上一级目录
发送
代码
package homework;
class FileMain {
public static void main(String[] args) {
new FileSystem().start();
}
}
package homework;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Vector;
public class FileUtil {
// 创建文件
public static boolean createFile(String destFileName) {
File file = new File(destFileName);
if (file.exists()) {
System.out.println("创建文件" + destFileName + "失败,目标文件已存在!");
return false;
}
if (destFileName.endsWith(File.separator)) {
System.out.println("创建文件" + destFileName + "失败,目标文件错误!");
return false;
}
if (!file.getParentFile().exists()) {
System.out.println("目标文件所在目录不存在,准备创建它!");
if (!file.getParentFile().mkdirs()) {
System.out.println("创建目标文件所在目录失败!");
return false;
}
}
try {
if (file.createNewFile()) {
System.out.println("创建文件" + destFileName + "成功!");
return true;
} else {
System.out.println("创建文件" + destFileName + "失败!");
return false;
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("创建文件" + destFileName + "失败!" + e.getMessage());
return false;
}