Reputation: 3673
In my tool I let the user select a specific file. By calling getAbsolutePath()
on that file I will get a String such as
C:\folder\folder\folder\dataset\MainFolder\folder\folder\folder\myfile.xml
How can I the path of the "MainFolder" stored in a new String variable. What I want from the example above is
C:\folder\folder\folder\dataset\MainFolder\
The structure is always
Drive:\random\number\of\folders\dataset\main_folder_name\folder1\folder2\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.
Upvotes: 1
Views: 102
Reputation: 1
It can be done by using only substring() and indexOf() method of String class. 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\
Upvotes: 0
Reputation: 109547
The URI
class has a relativize, and File a 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
Upvotes: 0
Reputation: 36703
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.
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
Upvotes: 1
Reputation: 35
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);
}
}
Upvotes: 1