ActionListener al = new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
File fi = new File("F:\\Java\\testData\\tree.dat");
FileOutputStream out1;
try {
out1 = new FileOutputStream(fi);
BufferedOutputStream bout1 = new BufferedOutputStream(out1);
PrintStream pout1 = new PrintStream(bout1);
TreePath path;
int i = 1; //第一行为根目录 所以从第二行开始
while((path =tree.getPathForRow(i))!= null)
{
tree.expandRow(i); //将所有节点展开
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)
path.getLastPathComponent();
if(selectedNode.isLeaf()) //如果这条路径不是完整路径则不保存
{
String treePath = path.toString().substring(1,
path.toString().length()-1); //由于开头和结尾是[],要去掉
pout1.println(treePath); //将路径写入
}
i++;
}
pout1.close();
bout1.close();
out1.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
};在创建JTree的时候,就可以利用文件里的路径,创建节点了
读取时,利用readline()读取一条路径,并用 split(",")来分割即可
嘿嘿 我刚学java不久,xml还不会,所以只能用我会的知识来实现