把指定的路徑轉化為json串。用于jstree的資料源
最近在用jstree。把任一個個指定的路徑,把該路徑下的所有檔案用jstree形成目錄樹。jstree接收的資料是json格式。這裡需要無限遞歸,周遊路徑下的所有檔案,并轉化成json串。.
舉個例子:如路徑是E:/test
則應把test下的所有檔案周遊一遍,并轉化成json串存起來。
在E盤下建了一個名為test的檔案夾,裡面有test1(檔案夾),test2.txt, test3.txt。test1檔案夾下又有test1.1.txt
目錄結構如下:
test1 E:\test\test1 Pid=0
test1.1.txt E:\test\test1\test1.1.txt Pid=1
test2.txt E:\test\test2.txt Pid=2
test3.txt E:\test\test3.txt Pid=3
則輸出的json串應是這樣的:
[{attributes:{id:"0"},state:"open",data:"test1" ,children:[{attributes:{id:"1"},state:"open",data:"test1.1.txt" }] },{attributes:{id:"2"},state:"open",data:"test2.txt" },{attributes:{id:"3"},state:"open",data:"test3.txt" }]
我整整調了半天的代碼,這樣一個小程式我也是醉了……不過最後終于成功了!好吧,上代碼。親測通過
簡單的測試用例是:在E盤下建了一個名為test的檔案夾,裡面有test1(檔案夾),test2.txt, test3.txt。test1檔案夾下又有test1.1.txt。就是上面的例子。

下面是java代碼:
<span style="font-size:12px;">/**
* 給定任意的路徑 ,無限遞歸獲得該路徑下的所有節點,并轉化為json串 ,把該json串存到root.json檔案中, 用作jstree的資料源
* @param f
* 路徑名
* @return str,然後将str寫入檔案中。
* @author YanniZhang
* @date 2015.4.6
*/
package test;
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;
public class test {
public static int parentId=0;
public static String str="";
public static void main(String[] args) {
File f =new File("E:/myjstree");//給定的路徑是E盤下的test檔案夾。這裡換成你想要轉換成json串的任意路徑
String str="[";
// 從根開始 ,調用ToJson()方法,把路徑轉化成json串。結果是str
str+=ToJeson(f);
str += "]";
/**
* 把json串寫入檔案root.json中
*
*/
try {
File file = new File("E:/root.json");
// if file doesn't exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(str);
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/*ToJson()方法,實作把指定路徑轉化為json串。采用無限遞歸周遊路徑下的所有節點的方法實作*/
private static String ToJeson(File f) {
//杳出頂層的子節點
File[] files = f.listFiles();
//周遊它的子節點
for(int i=0; i<files.length; i++)
{
//有子節點
if(files[i].isDirectory())
{
str+= "{\"attributes\":{\"id\":\"" +parentId
+ "\"},\"state\":\"open\",\"text\":\"" + files[i].getName() + "\" ,";
str += "\"children\":[";
parentId++;
//周遊它的子節點
File[] list=files[i].listFiles();
for(int j=0;j<list.length;j++)
{
if(list[j].isDirectory())
{
//還有子節點(遞歸調用)
str+= "{\"attributes\":{\"id\":\"" +parentId
+ "\"},\"state\":\"open\",\"text\":\"" + list[j].getName() + "\" ,";
str += "\"children\":[";
parentId++;
ToJeson(list[j]);
str+="]";
str+=" }";
if(j<list.length-1)
{
str+=",";
}
}
else
{
str += "{\"attributes\":{\"id\":\"" + parentId
+ "\"},\"state\":\"open\",\"text\":\"" + list[j].getName()
+ "\" " + ", \"type\":\"leaf\" }";
parentId++;
if (j < list.length - 1)
{
str += ",";
}
}
}
str+="]";
str+=" }";
if(i<files.length-1)
{
str+=",";
}
}
else
{
str += "{\"attributes\":{\"id\":\"" + parentId
+ "\"},\"state\":\"open\",\"text\":\"" + files[i].getName()
+ "\" " + ", \"type\":\"leaf\" }";
parentId++;
if (i < files.length - 1)
{
str += ",";
}
}
}
return str;
}
}
<strong>
</strong></span><span style="color:#ff0000;font-weight: bold; font-size: 18px;">
</span>
<span style="font-weight: bold; color: rgb(255, 0, 0); font-size: 18px; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">下面是java代碼在eclipse上的運作結果:</span>
好吧,太長了載圖不太好看。其實是這樣的:
[{attributes:{id:"0"},state:"open",data:"test1" ,children:[{attributes:{id:"1"},state:"open",data:"test1.1.txt" }] },{attributes:{id:"2"},state:"open",data:"test2.txt" },{attributes:{id:"3"},state:"open",data:"test3.txt" }]
是不是成功了?哈哈,我覺得應該給自己獎勵,對于一個沒學過java 的人,我已經很棒啦!獎勵就是4.12号去看“速7”!