在java中根据文件夹id和父id创建文件夹

发布于 2024-12-06 02:51:57 字数 1313 浏览 0 评论 0原文

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

文件夹在数据库中的排列方式如下:

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(文档)的文件夹,“其他”进入 id 为 4(我的文件)的文件夹

我一直在尝试使用 Whyle 循环来做到这一点,但不知道如何让它们进入正确的文件夹。

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());
             }
            }

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.

The folders are arranged on the database like so:

id name parent_id

1 documents 0
2 movies 0
3 videos 0
4 my files 1
5 desktop 0
6 other 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)

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());
             }
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

携余温的黄昏 2024-12-13 02:51:57

创建一个文件夹对象来存储数据,然后在从数据库读取数据时构建这些对象。构建完所有文件夹对象后,执行最后一个循环将每个文件夹绑定到其父类。也许是这样的:

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;
    }
}

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

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。

创建完所有文件夹后,您可以进行最后一个循环以将父文件夹连接到子文件夹,如下所示:

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() 方法。

希望有帮助。

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;
    }
}

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);

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);
}

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.

Hope that helps.

谈下烟灰 2024-12-13 02:51:57

从数据库中获取所有值后,我建议创建一个 Map ,它将每个文件夹 id 映射到其文件夹信息(其中文件夹保存 id、名称和父 id) )。然后,您可以循环遍历此映射,对于每个文件夹,通过递归父 ID 构建完整路径,直到到达根目录(例如“foo/bar/baz”),然后使用以下命令调用 File.mkdirs()这条路。

once you get all the values out of the database, i would suggest creating a Map<Integer,Folder> which maps each folder id to its Folder information (where Folder holds the id, name, and parent id). then, you can loop through this map, and for each folder, build up the full path by recursing up the parent ids until you reach the root and (e.g. "foo/bar/baz") and then call File.mkdirs() with this path.

夏末的微笑 2024-12-13 02:51:57

我看到您在 9 月 29 日发表的评论,询问有关打印文件夹的问题。您的代码似乎没问题,只是您只打印出父文件夹的名称。如果您想打印所有内容,您需要深入研究子级并打印它们,也许是这样的:

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

printFolderRecursively(data.get(1));

然后编写一个方法来打印文件夹名称及其子级:

public void printFolderRecursively(Folder folder) {
    System.out.print("/" + rootFolderName);
    for(Folder child : folder.getChildren())
        printFolderRecursively(child);
}

希望有所帮助。

I see your comment on Sep 29 asking about printing folders. Your code there seems ok, except that you are only printing out the name of the parent folder. If you want to print them all you need to dig into the children and print them too, perhaps something like this:

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

printFolderRecursively(data.get(1));

and then write a method that prints a folder name along with it's children:

public void printFolderRecursively(Folder folder) {
    System.out.print("/" + rootFolderName);
    for(Folder child : folder.getChildren())
        printFolderRecursively(child);
}

hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文