如何通过读取文件名来创建文件夹树

时间:2015-07-08 15:29:08

标签: java arrays collections

我有一个要求,我需要从远程位置读取所有文件。 存储在远程位置的文件格式为categoryName.categoryName.categoryNname.documentNname.extension 例如:Interest Calculation Info.2015.October.001-00-232-232.pdf 我需要读取所有文件名并按照以下格式创建文件夹结构:

Interest Calculation Info
     |--2015  
         |---October
               |--001-00-232-232.pdf

可能存在可能具有相同类别的各种文件,这些文件需要分组在相同的类别文件夹中。 什么是最好的解决方案呢?

1 个答案:

答案 0 :(得分:0)

Just use mkdirs()

Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. Note that if this operation fails it may have succeeded in creating some of the necessary parent directories.

String myString = "Interest Calculation Info.2015.October.001-00-232-232.pdf";
String extension = myString.substring(myString.lastIndexOf('.'));
String filename = myString.substring(0,myString.lastIndexOf('.')).replace(".", File.separator) + extension;
String startFolder = "C:";

File file = new File(startFolder, filename);
file.mkdirs(); 
相关问题