[英]Hard coding part of the Path in Java
I have the following dir tree 我有以下目录树
C:\folder1\folder2\SPECIALFolders1\folder3\file1.img
C:\folder1\folder2\SPECIALFolders2\folder3\file2.img
C:\folder1\folder2\SPECIALFolders3\folder3\file3.img
C:\folder1\folder2\SPECIALFolders4\folder3\file4.img
C:\folder1\folder2\SPECIALFolders5\folder3\file5.img
I want to get to folder2, list all dirs in it ( SpecialFolders
) then retrieve the paths of those folders while adding (folder3) to their path 我想进入folder2,列出其中的所有目录( SpecialFolders
),然后在将(folder3)添加到其路径的同时检索这些文件夹的路径
The reason I'm doing this is I want later to pass this path (paths) to a method to retrieve last modified files in folder3. 我这样做的原因是,我想稍后将此路径传递给一种方法,以检索Folder3中最后修改的文件。 I know there are way easier ways to do it but this is a very particular case. 我知道有更简单的方法可以做到这一点,但这是一个非常特殊的情况。
I'm also trying to retrieve those folders within a specific time range so I used a while loop for that 我也在尝试在特定时间范围内检索那些文件夹,因此我使用了while循环
Date first = dateFormat.parse("2015-6-4");
Calendar ystr = Calendar.getInstance();
ystr.setTime(first);
Date d = dateFormat.parse("2015-6-1");
Calendar last = Calendar.getInstance();
last.setTime(d);
while(last.before(ystr))
{
//fullPath here = "C:\folder1\folder2\"
File dir = (new File(fullPath));
File[] files = dir.listFiles();
for (File file : files)
{
//Retrieve Directories only (skip files)
if (file.isDirectory())
{
fullPath = file.getPath();
//last.add(Calendar.DATE, 1);
System.out.println("Loop " + fullPath);
}
}
}
fullPath += "\\folder3\\";
return fullPath;
The problem with my code is that it only returns one path (that's the last one in the loop) --which make sense but I want to return all of the paths like this 我的代码的问题在于,它仅返回一个路径(这是循环中的最后一个),这很有意义,但我想返回所有这样的路径
C:\folder1\folder2\SPECIALFolders1\folder3\
C:\folder1\folder2\SPECIALFolders2\folder3\
C:\folder1\folder2\SPECIALFolders3\folder3\
C:\folder1\folder2\SPECIALFolders4\folder3\
C:\folder1\folder2\SPECIALFolders5\folder3\
I appreciate your input in advance 非常感谢您的输入
Instead of fullPath String
, use for example ArrayList<String>
to store all paths. 代替fullPath String
,例如使用ArrayList<String>
来存储所有路径。 Than instead of: 而不是:
fullPath = file.getPath();
use: 采用:
yourArrayList.add(file.getPath());
Your method will return an ArrayList with all paths, and you will need to code a method to retrive all paths from it. 您的方法将返回一个包含所有路径的ArrayList,并且您需要编写一种方法以从中检索所有路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.