Java实现把电脑的文件信息存入到txt文本及数据库中

为啥做这个

在网上找了好久没找到,都是实时UI模式去点击去查看的。
然后就自己做了个,主要是实现了将本地的文件信息,包括文件的名字、路径、子文件、大小等存入txt文本以及数据库中。
其实主要是因为要做毕设- -

思路

主要idea点是树节点的方式去代表一个文件,然后用递归的思想去做。
没更新之前【没有代码规范】已经放到github上了
代码代码

代码

看代码:

  1. 首先是 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);
    }
	
}
  1. 然后是Connector,这个挺有用的,也是自己图个方便写的,可以看这个链接~
  2. 再然后是数据库的方法,包括新建表和插入
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'"
				+ ");"
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值