我想从现有目录中创建一个树,以便它显示其中的所有子文件夹和文件。我使用以下代码:
<代码>文件文件=新文件(路径);TreeModel model=新文件TreeModel(文件);JTree tree=新JTree(模型)
import java.io.File;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.WindowConstants;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
class FileTreeModel implements TreeModel {
private final ArrayList<TreeModelListener> mListeners = new ArrayList<>();
private final MyFile mFile;
public FileTreeModel(final MyFile pFile) {
mFile = pFile;
}
@Override public Object getRoot() {
return mFile;
}
@Override public Object getChild(final Object pParent, final int pIndex) {
return ((MyFile) pParent).listFiles()[pIndex];
}
@Override public int getChildCount(final Object pParent) {
return ((MyFile) pParent).listFiles().length;
}
@Override public boolean isLeaf(final Object pNode) {
return !((MyFile) pNode).isDirectory();
}
@Override public void valueForPathChanged(final TreePath pPath, final Object pNewValue) {
final MyFile oldTmp = (MyFile) pPath.getLastPathComponent();
final File oldFile = oldTmp.getFile();
final String newName = (String) pNewValue;
final File newFile = new File(oldFile.getParentFile(), newName);
oldFile.renameTo(newFile);
System.out.println("Renamed '" + oldFile + "' to '" + newFile + "'.");
reload();
}
@Override public int getIndexOfChild(final Object pParent, final Object pChild) {
final MyFile[] files = ((MyFile) pParent).listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i] == pChild) return i;
}
return -1;
}
@Override public void addTreeModelListener(final TreeModelListener pL) {
mListeners.add(pL);
}
@Override public void removeTreeModelListener(final TreeModelListener pL) {
mListeners.remove(pL);
}
/**
* stolen from http://developer.classpath.org/doc/javax/swing/tree/DefaultTreeModel-source.html
*
* <p>
* Invoke this method if you've modified the TreeNodes upon which this model
* depends. The model will notify all of its listeners that the model has
* changed. It will fire the events, necessary to update the layout caches and
* repaint the tree. The tree will <i>not</i> be properly refreshed if you
* call the JTree.repaint instead.
* </p>
* <p>
* This method will refresh the information about whole tree from the root. If
* only part of the tree should be refreshed, it is more effective to call
* {@link #reload(TreeNode)}.
* </p>
*/
public void reload() {
// Need to duplicate the code because the root can formally be
// no an instance of the TreeNode.
final int n = getChildCount(getRoot());
final int[] childIdx = new int[n];
final Object[] children = new Object[n];
for (int i = 0; i < n; i++) {
childIdx[i] = i;
children[i] = getChild(getRoot(), i);
}
fireTreeStructureChanged(this, new Object[] { getRoot() }, childIdx, children);
}
/**
* stolen from http://developer.classpath.org/doc/javax/swing/tree/DefaultTreeModel-source.html
*
* fireTreeStructureChanged
*
* @param source the node where the model has changed
* @param path the path to the root node
* @param childIndices the indices of the affected elements
* @param children the affected elements
*/
protected void fireTreeStructureChanged(final Object source, final Object[] path, final int[] childIndices, final Object[] children) {
final TreeModelEvent event = new TreeModelEvent(source, path, childIndices, children);
for (final TreeModelListener l : mListeners) {
l.treeStructureChanged(event);
}
}
}
class MyFile {
private final File mFile;
public MyFile(final File pFile) {
mFile = pFile;
}
public boolean isDirectory() {
return mFile.isDirectory();
}
public MyFile[] listFiles() {
final File[] files = mFile.listFiles();
if (files == null) return null;
if (files.length < 1) return new MyFile[0];
final MyFile[] ret = new MyFile[files.length];
for (int i = 0; i < ret.length; i++) {
final File f = files[i];
ret[i] = new MyFile(f);
}
return ret;
}
public File getFile() {
return mFile;
}
@Override public String toString() {
return mFile.getName();
}
}
public class FileWrapperDeluxe {
public static void main(final String[] args) {
final JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
f.setBounds(100, 100, 400, 400);
final File file = new File("E:\\");
final MyFile mf = new MyFile(file);
final TreeModel model = new FileTreeModel(mf);
final JTree tree = new JTree(model);
tree.setEditable(true);
f.add(new JScrollPane(tree));
f.setVisible(true);
}
}
我的程序从不是项目根目录的位置读入文档。文档包含一个相对路径。当程序应用该路径时,它确实从项目的根目录开始。如何使其应用来自文档原始位置的路径? 以下是细节。有点长,但很简单。 我在Eclipse中有一个Java项目,位于 该程序运行一个XSL转换,该转换以Schematron模式作为输入,并产生一个新的XSLT样式表作为输出 它包含这一行,还有几条类似的: 如果从模式的位置开始,并应用该相对路径
本文向大家介绍php从完整文件路径中分离文件目录和文件名的方法,包括了php从完整文件路径中分离文件目录和文件名的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了php从完整文件路径中分离文件目录和文件名的方法。分享给大家供大家参考。具体分析如下: basename()函数用于从路径中获得文件名 dirname()函数用于从路径中获得文件目录 希望本文所述对大家的php程序设计有所帮助
问题内容: 你如何grep只返回匹配的行?即结果中省略了路径/文件名。 在这种情况下,我想查看当前目录中的所有.bar文件,并搜索FOO。 问题答案: 没必要。如果您只是在特定目录中寻找模式,就足够了: 隐藏文件名的参数在哪里,例如: -h,–no-文件名 在输出中禁止文件名的前缀。当仅搜索一个文件(或仅标准输入)时,这是默认设置。 请注意,您使用的是 -H,–with-filename 打印每个
问题内容: 我想获取当前文件的目录路径。我试过了: 但是,如何检索目录的路径? 例如: 问题答案: Python 3 对于正在运行的脚本的目录: 对于当前工作目录: 对于正在运行的脚本的目录: 如果你的意思是当前工作目录: 请注意,前后分别是两个下划线,而不仅仅是一个。 另请注意,如果你正在交互运行或已从文件以外的内容(例如数据库或在线资源)中加载了代码,则可能不会设置,因为没有“当前文件”的概念
问题内容: 我似乎无法获得任何说明如何执行此操作的搜索结果。 我要做的就是能够知道给定的路径是文件还是目录(文件夹)。 问题答案: 应该告诉你。从文档: 从fs.stat()和fs.lstat()返回的对象属于这种类型。 注意: 将 上述溶液 将一个IF; 例如,or或不存在。 如果您想要一个或方法,请尝试按约瑟夫在下面的评论中所述。
我想从URI中获得完整的文件路径。URI不是图像,而是音乐文件,但如果我像纵隔解决方案那样做,如果应用程序用户选择eg Astro作为浏览器,而不是音乐播放器,它就不会工作。我怎么解决这个?