在java中根据文件夹id和父id创建文件夹
我需要创建一个类来获取存储在数据库中的文件夹列表,并在本地计算机上以正确的层次结构创建它们。
文件夹在数据库中的排列方式如下:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
创建一个文件夹对象来存储数据,然后在从数据库读取数据时构建这些对象。构建完所有文件夹对象后,执行最后一个循环将每个文件夹绑定到其父类。也许是这样的:
现在,当您从数据库读取数据时,您创建这些文件夹对象(没有子对象)并将它们添加到映射中:
使用 Integer.valueOf(String) 将 String 转换为 int。
创建完所有文件夹后,您可以进行最后一个循环以将父文件夹连接到子文件夹,如下所示:
最后,只需获取 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:
Now as you read the data from the database, you create these Folder objects (with no children) and add them to the map with:
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:
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.
从数据库中获取所有值后,我建议创建一个
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.我看到您在 9 月 29 日发表的评论,询问有关打印文件夹的问题。您的代码似乎没问题,只是您只打印出父文件夹的名称。如果您想打印所有内容,您需要深入研究子级并打印它们,也许是这样的:
然后编写一个方法来打印文件夹名称及其子级:
希望有所帮助。
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:
and then write a method that prints a folder name along with it's children:
hope that helps.