前言
平时我们写一些类或者方法时,经常需要一些常常写但是又重复的代码,比如一些排序算法,文件操作,一般我们可以打个jar包导入使用,如果只需要用到jar包的部分代码,打jar包就显得没必要了。
于是笔者做了这样一个小工具,方便管理一些代码片段。
功能简介
以上为软件截图,功能比较简单:
1.新建分类
2.新建代码文件
3.点击复制课直接将代码内容复制到剪切板
下载链接(0积分下载)
含jre版(自带jre,不需要电脑安装jre)
https://download.csdn.net/download/weixin_44155115/12121933
不含jre版(需要电脑安装jre)
https://download.csdn.net/download/weixin_44155115/12121937
开发技术
开发环境: eclipse
编程语言: JavaFX
算法源码
1.生成目录
使用递归算法,生成文件目录的TreeView
(当目录下文件结构复杂时,程序会卡顿)
/**
* 创建文件夹目录
*
* @param path 父文件夹路径
*/
private void buidContentTree(String path, TreeItem<String> rootItem) {
File[] files = new File(path).listFiles();
for (File file : files) {
if (file.isFile() && FileUtil.getFileType(file).equals(".txt")) { // 创建文件节点
ImageView file_icon = new ImageView(MyIcons.IMG_FILE);
file_icon.setFitHeight(16);
file_icon.setFitWidth(14);
TreeItem<String> item = new TreeItem<>(file.getName(), file_icon);
rootItem.getChildren().add(item);
} else if (file.isDirectory()) { // 创建文件夹节点
ImageView dir_icon = new ImageView(MyIcons.IMG_FOLDER_CLOSE);
dir_icon.setFitHeight(16);
dir_icon.setFitWidth(16);
TreeItem<String> item = new TreeItem<>(file.getName(), dir_icon);
rootItem.getChildren().add(item);
buidContentTree(file.getAbsolutePath(), item); // 递归创建子文件夹内的节点
}
}
}
2.查找文件
使用深度优先算法
(当目录下文件结构复杂时,程序会卡顿)
/**
* 查找TreeView对应的item
*
* @param item 查找的对象节点
* @param keyWord 查找的关键字
* @return 目标节点
*/
private TreeItem<String> findTreeItem(TreeItem<String> item, String keyWord) {
Pattern regex = Pattern.compile(keyWord);
ObservableList<TreeItem<String>> childItems = item.getChildren();
for (TreeItem<String> childItem : childItems) {
if (childItem.isLeaf() && regex.matcher(childItem.getValue()).find())
return childItem;
TreeItem<String> aimItem = findTreeItem(childItem, keyWord); // 递归搜索子文件夹
if (aimItem != null)
return aimItem; // 如果不为null则返回找到的item
}
return null;
}
来源:CSDN
作者:Nonoas
链接:https://blog.csdn.net/weixin_44155115/article/details/104092873