从一个文件路径Java中的目录(机器人) [英] Get the directory from a file path in java (android)
本文介绍了从一个文件路径Java中的目录(机器人)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有一个函数来获取文件路径的目录部分?所以从
字符串A =/根/ SD卡/图片/ img0001.jpg
你
/根/ SD卡/图片
解决方案
是的。首先,构造一个 文件
重新presenting图像路径:
文件file =新的文件(一);
如果你从一个相对路径开始:
文件=新的文件(file.getAbsolutePath());
然后,获取父
字符串DIR = file.getParent();
或者,如果你想要的目录作为文件
对象,
文件dirAsFile = file.getParentFile();
Is there a function to get the directory part of a file path? so from
String a="/root/sdcard/Pictures/img0001.jpg";
you get
"/root/sdcard/Pictures"
解决方案
Yes. First, construct a File
representing the image path:
File file = new File(a);
If you're starting from a relative path:
file = new File(file.getAbsolutePath());
Then, get the parent:
String dir = file.getParent();
Or, if you want the directory as a File
object,
File dirAsFile = file.getParentFile();
这篇关于从一个文件路径Java中的目录(机器人)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文