为啥做这个
在网上找了好久没找到,都是实时UI模式去点击去查看的。
然后就自己做了个,主要是实现了将本地的文件信息,包括文件的名字、路径、子文件、大小等存入txt文本以及数据库中。
其实主要是因为要做毕设- -
思路
主要idea点是树节点的方式去代表一个文件,然后用递归的思想去做。
没更新之前【没有代码规范】已经放到github上了
代码代码
代码
看代码:
- 首先是 node,也就是节点描述
package computer.saveinformation;
import java.io.File;
import javax.swing.Icon;
import javax.swing.filechooser.FileSystemView;
/**
* Title: Node
* Description:
* @author Zero_Hwa
* @date Apr 10, 2020 9:26:50 PM
*/
public class Node {
File file;
File[] files;
int count;
String path;
FileSystemView fsv = FileSystemView.getFileSystemView();
File root = fsv.getFiles(fsv.getHomeDirectory(), true)[0];
public Node() {
this.file = root;
/**
* 这里用fsv.getFiels好像不能get到各个磁盘,原因不明,直接用files.listFiles()可以。
*/
// files = fsv.getFiles(file, true);
files = file.listFiles();
count = files.length;
path = this.file.getAbsolutePath();
}
public File[] getFiles() {
return files;
}
public int getCount() {
return count;
}
public String getPath() {
return path;
}
public Node(File file) {
this.file = file;
files = fsv.getFiles(file, true);
count = files.length;
path = file.getAbsolutePath();
}
@Override
public String toString() {
// TODO Auto-generated method stub
return fsv.getSystemDisplayName(file);
}
/**
* 在系统文件浏览器中显示的文件、目录或文件夹的图标。
* @return
*/
public Icon getIcon() {
return fsv.getSystemIcon(file);
}
}
- 然后是Connector,这个挺有用的,也是自己图个方便写的,可以看这个链接~
- 再然后是数据库的方法,包括新建表和插入
package computer.saveinformation;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* <p>Title: SaveInMysql</p>
* <p>Description: </p>
* @author Zero_Hwa
* @date Apr 10, 2020
*/
public class SaveInMysql {
public boolean newTable() throws SQLException{
boolean isSuccessful;
String sql = "create table if not exists "
+ "computerInformation"
+ "("
+ "id int unsigned not null auto_increment primary key,"
+ "name varchar(2000) comment '文件名', "
+ "childFiles text comment '子文件名,用空格隔开',"
+ "path text comment '绝对路径',"
+ "size bigint unsigned comment '大小,文件夹不显示大小',"
+ "isFile boolean comment 'false为文件夹,true为文件'"
// + "parentid int unsigned comment '父文件夹的id'"
+ ");"