2015-10-14 92 views
1

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

的目录是通过由它来启动根目录下我程序 enter image description here

+0

添加了Java标记;作为一般暗示:被拼写检查的问题吸引了更多的积极关注,所以我在文本资本中做了所有的“我”,以及句子的开始。 –

+2

“获得最后一个”A.2“和”PE0120A“并将其写入csv”?向我们展示一个应该进入csv的例子。 – JimmyB

+0

找到了解决办法: “字符串splitpath = file.getAbsolutePath(); \t \t字符串[] pathsplit = splitpath.split(” \\\\“); \t \t \t INT \t L = pathsplit.length; \t \t字符串版本= pathsplit [1- 2]; \t \t字符串foldname = pathsplit [1-3]; FileWriter的作家=新的FileWriter( “C:/Users/wcadmin/Desktop/test_output.csv”); 作家.append(vesioin); writer.append(文件夹名称); writer.append(splitpath); “ @marcus muller,HannoBinder感谢您的回复。 –

回答

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); 
相关问题