检索路径中的最后一个子字符串和最后一个子字符串

时间:2015-10-14 13:48:39

标签: java file-io

我有一个包含子目录和文件的文件夹 我能够以递归方式重命名文件。我的问题是获取最后一个A.2PE0120A并将其写为CSV 如果路径是静态的,那么我将计算出现次数并获取值,但我不知道文件夹可能包含多少个子文件夹。

public static void dirTree(File dir) throws IOException 
{
    File[] subdirs=dir.listFiles();
    for(File subdir: subdirs) 
    {
        if (subdir.isDirectory()) 
        {
            dirTree(subdir);
        } 
        else 
        {
            doFile(subdir);
        }
    }
}
  

C:/f1/f2/f3/manoj/Manoj_Eclipse/PE0120A/A.2/filename.txt

dir是启动程序所必需的根目录 enter image description here

1 个答案:

答案 0 :(得分:1)

以下代码将获取最后一个但最后一个文件夹名称。感谢支持人员。

String splitpath = file.getAbsolutePath(); 
String[] pathsplit = splitpath.split("\\\\"); 
int l = pathsplit.length; 
String version = pathsplit[l-2]; 
String foldname = pathsplit[l-3]; 
FileWriter writer = new FileWriter("C:/Users/username/Desktop/test_output.csv"); writer.append(vesioin); 
writer.append(foldername); 
writer.append(splitpath);
相关问题