如何从DirectoryPath中获取FolderName和FileName [英] How to get FolderName and FileName from the DirectoryPath

查看:584
本文介绍了如何从DirectoryPath中获取FolderName和FileName的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有DirectoryPath作为data / data / in.com.jotSmart / app_custom /文件夹名/文件名,我作为字符串存储在 ArrayList

  ArrayList< String>一个; 

a.add(data / data / in.com.jotSmart / app_custom / page01 / Note01.png);

现在从这个路径我想将page01作为单独的字符串,Note01作为单独的字符串存储到两个字符串变量。我尝试了很多,但无法得到结果。如果有人知道帮我解决这个问题。

解决方案

f.getParent()


返回此抽象路径名父项的路径名字符串,如果此路径名未命名父目录,则返回null。


例如

 文件f =新文件(/ home / jigar / Desktop / 1.txt); 

System.out.println(f.getParent()); // / home / jigar / Desktop
System.out.println(f.getName()); //1.txt

更新:

if data / data / in.com.jotSmart / app_custom / page01 / Note01.png 文件在你的文件系统然后

  for(String fileNameStr:filesList){
File file = new File(fileNameStr) ;
String dir = file.getParent()。substring(file.getParent()。lastIndexOf(File.separator)+ 1); // page01
String fileName = f.getName();
if(fileName.indexOf(。)!= - 1){
fileName = fileName.substring(0,fileName.lastIndexOf(。));
}
}


I have DirectoryPath as "data/data/in.com.jotSmart/app_custom/folderName/FileName" which i stored as a String in ArrayList

Like

ArrayList<String> a;

a.add("data/data/in.com.jotSmart/app_custom/page01/Note01.png");

Now from this path i want to get page01 as seperate string and Note01 as seperate string and stored it into two string variable. I tried a lot, but not able to get the result. If any one knows help me to solve this out.

解决方案

f.getParent()

Returns the pathname string of this abstract pathname's parent, or null if this pathname does not name a parent directory.

For example

    File f = new File("/home/jigar/Desktop/1.txt");

    System.out.println(f.getParent());// /home/jigar/Desktop
    System.out.println(f.getName());  //1.txt

Update: (based on update in question)

if data/data/in.com.jotSmart/app_custom/page01/Note01.png is valid representation of file in your file system then

for(String fileNameStr: filesList){
  File file = new File(fileNameStr);
  String dir = file.getParent().substring(file.getParent().lastIndexOf(File.separator) + 1);//page01
  String fileName = f.getName();
  if(fileName.indexOf(".")!=-1){
     fileName = fileName.substring(0,fileName.lastIndexOf("."));
   }
}

这篇关于如何从DirectoryPath中获取FolderName和FileName的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆