[英]Get part of a filepath from a longer filepath
In my tool I let the user select a specific file. 在我的工具中,我让用户选择一个特定的文件。 By calling getAbsolutePath()
on that file I will get a String such as 通过对该文件调用getAbsolutePath()
,我将得到一个字符串,例如
C:\\folder\\folder\\folder\\dataset\\MainFolder\\folder\\folder\\folder\\myfile.xml C:\\文件夹\\文件夹\\文件夹\\数据集\\ MainFolder \\文件夹\\文件夹\\文件夹\\ myfile.xml中
How can I the path of the "MainFolder" stored in a new String variable. 如何将“ MainFolder”的路径存储在新的String变量中。 What I want from the example above is 我从上面的示例中想要的是
C:\\folder\\folder\\folder\\dataset\\MainFolder\\ C:\\文件夹\\文件夹\\文件夹\\数据集\\ MainFolder \\
The structure is always 结构总是
Drive:\\random\\number\\of\\folders\\dataset\\main_folder_name\\folder1\\folder2\\folder3\\myfile.xml 驱动器:\\文件夹\\随机\\数字\\ \\数据集\\ main_folder_name \\文件夹1 \\文件夹2 \\ folder3 \\ myfile.xml中
The parent folder of the one I'm looking for always has the name "dataset". 我要查找的文件夹的父文件夹始终具有“数据集”名称。 The one that follows that folder is the one i'm interested in. 该文件夹后面的那个是我感兴趣的那个。
I'd highly recommend using the File API rather than String manipulation, this isolates you from platform differences in forward vs backslashes or any other differences. 我强烈建议您使用File API,而不要使用String操作,这样可以将您与平台之间的正斜杠,反斜杠区别或任何其他区别区别开来。
Code 码
String path = "C:\\random\\number\\of\\folders\\dataset\\main_folder_name\\folder1\\folder2\\folder3\\myfile.xml";
File search = new File(path);
File lastParent = search;
while (search != null) {
if ("dataset".equals(search.getName())) {
break;
}
lastParent = search;
search = search.getParentFile();
}
if (lastParent != null) {
System.out.println(lastParent.getCanonicalPath());
}
Output 产量
C:\random\number\of\folders\dataset\main_folder_name
Use the below code in your program. 在程序中使用以下代码。 This will solve your expectations :) 这将解决您的期望:)
import java.util.StringTokenizer;
public class MainFolder {
public static void main(String args[]) {
String separator = "/";
StringTokenizer st = new StringTokenizer("C:/folder/folder/folder/dataset/MainFolder/folder/folder/folder/myfile.xml",separator);
String mainFolderPath = "";
String searchWord = "dataset";
while (st.hasMoreTokens()) {
mainFolderPath = mainFolderPath + st.nextToken() + separator;
if (mainFolderPath.contains(searchWord)) {
mainFolderPath = mainFolderPath + st.nextToken() + separator;
break;
}
}
System.out.println(mainFolderPath);
}
}
The URI
class has a relativize, and File a toURI. URI
类具有相对性,并具有toURI。
String path = "C:\\folder\\folder\\folder\\dataset\\MainFolder\\folder\\folder\\folder\\myfile.xml";
String base = "C:\\folder\\folder\\folder\\dataset\\MainFolder";
File pathFile = new File(path);
File baseFile = new File(base);
URI pathURI = pathFile.toURI();
URI baseURI = baseFile.toURI();
URI relativeURI = baseURI.relativize(pathURI);
System.out.println(relativeURI.toString());
// folder/folder/folder/myfile.xml
File relativeFile = new File(relativeURI.getPath());
System.out.println(relativeFile.getPath());
// folder\folder\folder\myfile.xml
It can be done by using only substring() and indexOf() method of String class. 可以仅使用String类的substring()和indexOf()方法来完成此操作。 Code is: 代码是:
String path = "C:\\random\\number\\of\\folders\\dataset\\main_folder_name\\folder1\\folder2\\folder3\\myfile.xml";
int indexOfFirstBkSlashB4Dataset = path.indexOf("\\dataset");
String sub1 = path.substring(0,indexOfFirstBkSlashB4Dataset);
String sub2 = "\\dataset\\";
String sub3Intermediate = path.substring(indexOfFirstBkSlashB4Dataset+9,path.length());
int index2 = sub3Intermediate.indexOf("\\");
String sub4 = sub3Intermediate.substring(0,index2+1);
String output = sub1+sub2+sub4;
System.out.println(output);
Output is: C:\\random\\number\\of\\folders\\dataset\\main_folder_name\\ 输出为:C:\\ random \\ number \\ of \\ folders \\ dataset \\ main_folder_name \\
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.