package main.java.filemanager;

import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by mac on 03/05/15.
 */
public class FileManager {
    public static List<DirItem> getDir(String dir) {
        File folder = new File(dir);
        File[] listOfFiles = folder.listFiles();

        List<DirItem> list = new ArrayList<DirItem>(listOfFiles.length);

        for (File file : listOfFiles) {
            DirItem item = new DirItem(file);
            list.add(item);
        }
        return list;
    }
    public static void getDirJson(Writer out, String dir) throws IOException{
        //keep in list, so we can sort on arbitrary property
        List<DirItem> list = getDir(dir);
        String col="";
        out.write("\"dir\" : [\n");
        for (DirItem item : list) {
            out.write(col);
            out.write("\"i\" : {\n");
            out.write("\"n\" : \"");
            out.write(item.getName());
            out.write("\",\n");
            out.write("\"t\" : \"");
            out.write(item.getType().toString());
            out.write("\"}");
            col=",";
        }
        out.write( "]" );
    }
}

@@ -803,16 +803,17 @@
 .write(%22
+%7B
 %5C%22dir%5C%22 
@@ -908,16 +908,17 @@
 .write(%22
+%7B
 %5C%22i%5C%22 : 
@@ -1145,16 +1145,17 @@
 ite(%22%5C%22%7D
+%7D
 %22);%0A    
@@ -1202,16 +1202,17 @@
 rite( %22%5D
+%7D
 %22 );%0A   

test api.jsp