下面是我的代码“
import java.awt.HeadlessException;
import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.event.EventListenerList;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
@SuppressWarnings("serial")
public class JavaFileTree implements TreeModel, Serializable,Cloneable
{
protected EventListenerList listeners;

private static final Object LEAF = new Serializable() {}; @SuppressWarnings("unchecked")
private Map map;

private File root; @SuppressWarnings("unchecked")
public JavaFileTree(File root) throws HeadlessException
{
// super("FileTreeModelDemo");
try {
this.root = root; if (!root.isDirectory())
map.put(root, LEAF); this.listeners = new EventListenerList();
this.map = new HashMap(); }
catch (Exception e) 
{
e.printStackTrace();
}
} public thisTreeListener GetListener()
{
thisTreeListener a = new thisTreeListener();
return a;
}
public Object getRoot()
{
return root;
} public boolean isLeaf(Object node)
{
return map.get(node) == LEAF;
} @SuppressWarnings("unchecked")
public int getChildCount(Object node) 
{
List children = children(node); if (children == null)
return 0; return children.size();
} public Object getChild(Object parent, int index)
{
return children(parent).get(index);
} public int getIndexOfChild(Object parent, Object child)
{
return children(parent).indexOf(child);
} @SuppressWarnings("unchecked")
protected List children(Object node) 
{
File f = (File) node; Object value = map.get(f); if (value == LEAF)
return null; List children = (List) value; if (children == null)
{
File[] c = f.listFiles(); if (c != null)
{
children = new ArrayList(c.length); for (int len = c.length, i = 0; i < len; i++) 
{
children.add(c[i]);
if (!c[i].isDirectory())
map.put(c[i], LEAF);
}
} else
children = new ArrayList(0); map.put(f, children);
} return children;
} public void valueForPathChanged(TreePath path, Object value) {} public void addTreeModelListener(TreeModelListener l)
{
listeners.add(TreeModelListener.class, l);
} public void removeTreeModelListener(TreeModelListener l)
{
listeners.remove(TreeModelListener.class, l);
} @SuppressWarnings("unchecked")
public Object clone() {
try 
{
JavaFileTree clone = (JavaFileTree) super.clone(); clone.listeners = new EventListenerList(); clone.map = new HashMap(map); return clone;

catch (CloneNotSupportedException e)
{
throw new InternalError();
}
} class thisTreeListener implements javax.swing.event.TreeSelectionListener 
{
// public void
public void valueChanged(javax.swing.event.TreeSelectionEvent tse)
{
System.out.println(tse.getNewLeadSelectionPath().toString());
}
}
}
请问为什么读出来的文件名都包含有完整的文件名呢,请问如何修改后就只显示文件名
另外如何获取选择文件的路径呢
望指教

解决方案 »

  1.   

    字符串截取呗。/opt/app/a.out
    找的最后一个'/' 截取后面的字符串~
      

  2.   

    读出来完整的文件名数据是有用的,可以用该数据满足你的两个要求:
    1、只显示文件名,如楼上的兄弟所说截取字符串
    2、获取文件路径public class Ts {
    public static void main(String[] args) {
                //s对应的是你的完整路径
        String s = "C:/Java/jdk1.6/include/web/test.pdf";
                //拿到文件名
        String s2 = s.substring(s.lastIndexOf("/")+1);
        //拿到文件路径
        String[] s3 = s.split(s2);
        
        System.out.println(s2);
        
        for (int i = 0; i < s3.length; i++) {
    System.out.println(s3[i]);
        }
    }


    }