在java中从文件夹id和父标识创建文件夹

在java中从文件夹id和父标识创建文件夹

本文介绍了在java中从文件夹id和父标识创建文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个类,获取存储在数据库中的文件夹列表,并在本地机器上以正确的层次结构创建它们。

I need to make a class that get the list of folders stored in a database and creates them on the local machine in the correct hierarchy.

在数据库上这样:


id name parent_id

1 documents 0
2 movies 0
3 videos 0
4 my files 1
5 desktop 0
6 other 4

因此,文档,电影,视频和桌面都在根。 '我的文件'进入ID为1(文档)的文件夹,'other'进入ID为4的文件夹(我的文件)

So documents, movies, videos and desktop are in the root. 'my files' goes in the folder with the id of 1(documents) and 'other' goes in the folder with the id of 4(my files)

尝试使用whyle循环,但不知道如何让他们进入正确的文件夹。

I have been trying to do it by using a whyle loop but dont know how to get them to go into the correct folders.

try {
            con = DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        while( i < 50 )
        {
            try {

            Statement st = con.createStatement();
            ResultSet result = st.executeQuery("SELECT name, id, parent_id FROM categories WHERE parent_id = '"+PID+"' AND repository_id = '"+RepoID+"'");



            while (result.next ())
            {
                String FolderName = result.getString ("name");
                String FolderId = result.getString ("id");
                String FolderId = result.getString ("parent_id");
make the folder name here
                System.out.println( FolderName+" "+FolderId );
            }

            System.out.println( " ");
                    i++ ;
            PID++;
            } catch (SQLException ex) {
                 System.out.println(ex.getMessage());
             }
            }


推荐答案

一个用于存储数据的Folder对象,然后在从数据库中读取时构建这些对象。一旦你构建了所有的文件夹对象,做一个最后的循环将每个文件夹绑定到其父类。也许这样:

Create a Folder object to store the data, then build these objects as you read from the database. Once you have built all the Folder objects, do a final loop to bind each Folder to its parent class. Perhaps something like this:

class Folder {
    private String name;
    private int id;
    private int parentId;
    private List<Folder> children = new ArrayList<Folder>();

    public Folder(String name, int id, int parentId) {
        this.name = name;
        this.id = id;
        this.parentId = parentId;
    }

    public void addChildFolder(Folder folder) {
        this.children.add(folder);
    }

    public List<Folder> getChildren() {
        return Collections.unmodifiableList(children);
    }

    public int getParentFolderId() {
        parentId;
    }

    public String getName() {
        return name;
    }

    public int getId() {
        return id;
    }
}

现在当您从数据库读取数据时,创建这些Folder对象(没有子对象),并将它们添加到地图中:

Now as you read the data from the database, you create these Folder objects (with no children) and add them to the map with:

Map<Integer, Folder> data = new HashMap<Integer, Folder>();

... loop through your result set getting folder data...
Folder newFolder = new Folder(nameString, id, parentId);
data.put(newFolder.getId(), newFolder);

使用Integer.valueOf(String)将String转换为int。

Use Integer.valueOf(String) to convert String to int.

一旦你创建了所有的文件夹,你可以做一个最后一个循环来连接父文件夹到孩子,如下:

Once you have the created all the Folders, you can make one final loop to connect the parent folders to the children, like this:

for(Folder folder : data.values()) {
    int parentId = folder.getParentFolderId();
    Folder parentFolder = data.get(parentId);
    if(parentFolder != null)
        parentFolder.addChildFolder(folder);
}

最后,只需抓取id为0的文件夹,磁盘,使用folder.getChildren()作为一个方便的方式向下移动树。检查File对象上的javadoc,你将特别想使用mkdirs()方法。

Finally, just grab the folder with id 0 and start building your Files on the disk, using folder.getChildren() as a convenient way to move down the tree. Check out the javadoc on the File object, you will particularly want to use the mkdirs() method.

希望有帮助。

这篇关于在java中从文件夹id和父标识创建文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 09:03