gpt4 book ai didi

java - 检索路径中最后一个子字符串和最后一个子字符串之前的子字符串

转载 作者:行者123 更新时间:2023-11-30 07:52:51 24 4
gpt4 key购买 nike

我有一个文件夹,其中包含子目录和文件。
我能够以递归方式重命名该文件。我的问题是获取最后一个 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

最佳答案

下面的代码将获取最后一个和最后一个文件夹名称。感谢大家的支持。

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

关于java - 检索路径中最后一个子字符串和最后一个子字符串之前的子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33127169/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com