程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java寫的文件浏覽器

java寫的文件浏覽器

編輯:關於JAVA

//------------------------------------------------------------
// 編譯下邊的類,並運行無需任何參數
//-----------------------------------------------------------
package com.swing.tree;
import java.io.*;
import java.util.*;
import javax.swing.tree.*;
public class file_path_obj
{
private String dir_tree="";
private File temp;
DefaultMutableTreeNode root=null;
public DefaultMutableTreeNode getPath_Obj()
{
if(this.root==null)
return new DefaultMutableTreeNode("null NODE");
return this.root;
}
public file_path_obj(String path)
{
try{
root= this.set(path);
}catch(Exception e)
{
System.out.println("tree error:\n+++++++++++++++++++++++++++++++++");
e.printStackTrace();
}}
public DefaultMutableTreeNode set(String dir) throws Exception //第歸遍歷文件甲
{
File[] temp_list = null;
if(new File(dir).exists()==false)
throw new Exception("file name is null");
DefaultMutableTreeNode tempNode;
temp = new File(dir);
if(temp.isFile())
{
return new DefaultMutableTreeNode(temp.getAbsolutePath());
}
temp_list = temp.listFiles();
tempNode = new DefaultMutableTreeNode(temp.getAbsolutePath());
//設置上級菜單的名字
if(temp_list.length==0) return new DefaultMutableTreeNode(temp.getAbsolutePath());
for(int i=0;i<temp_list.length;i++)
{
if(temp_list[i].isDirectory())
{
System.out.println("the file is:"+temp_list[i].getAbsolutePath());
tempNode.add(this.set(temp_list[i].getAbsolutePath()));
}
else
tempNode.add(new DefaultMutableTreeNode(temp_list[i].getAbsolutePath()));
}
return tempNode;
}
}
//------------------------------------------------------------
// 編譯下邊的類,並運行無需任何參數
//-----------------------------------------------------------
package com.tree;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
import com.swing.tree.*;
/** Example tree built out of DefaultMutableTreeNodes.
* ycj
*/
public class pathTree extends JFrame {
public static void main(String[] args) {
try
{
BufferedReader bin = null;
bin = new java.io.BufferedReader(new InputStreamReader(System.in));
String path="";
File temp = null;
System.out.print("輸入要顯示的文件路徑");
while(bin!=null)
{
path = bin.readLine();
if(path==null)
{
System.out.println("請輸入合法的文件路徑");
continue;
}
temp = new File(path);
if(temp.exists()==false||temp.isFile())
System.out.println("請輸入正確的文件路徑");
else
{new pathTree(path);
break;
}
}
}catch(Exception e)
{ e.printStackTrace(); }
}
public pathTree(String path)
{
super("Creating a Simple JTree");
WindowUtilities.setNativeLookAndFeel();//set the UIManager
addWindowListener(new ExitListener());
Container content = getContentPane();
file_path_obj obj = new file_path_obj(path);
System.out.println(obj.getPath_Obj());
JTree tree = new JTree(obj.getPath_Obj());//MAKE A TREE
Cursor cor = new Cursor(Cursor.HAND_CURSOR);
tree.setCursor(cor);
content.setLayout(new FlowLayout());
JPanel jp1 = new JPanel(new BorderLayout());
JPanel jp2 = new JPanel(new BorderLayout());
content.add(jp1);
content.add(jp2);
content.add(new JScrollPane(tree), BorderLayout.CENTER);
setSize(275, 500);
setVisible(true);
this.pack();
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved