This is the folder structure. Folder1 ---> Folder2, Folder3. Inside 3 there is java code. How do I make java code which will be able to search the top level folder (ie 1) for a file or folder? Is this even possible ?
Asked
Active
Viewed 3,853 times
-3
-
its possible, but what have u tried? – Ankit Apr 04 '13 at 05:20
-
It can search relative to where the class is loaded from, not where the (source) code is. Elaborate on your question, with example. – Miserable Variable Apr 04 '13 at 05:20
3 Answers
-1
Use getParent()
method to get name of parent or top level directory name.
import java.io.File;
public class DirectoryExample6 {
public static void main(String args[]){
File f = new File("D://tutorialData/java");
String parent = f.getParent();
System.out.println("Name of parent dir : "+parent);
}
}
Source: http://www.tutorialdata.com/examples/java/file-input-output/directory/get-name-of-parent-directory

user2236292
- 235
- 2
- 4
-
Replace D://tutorialData/java with the relative path of a file in your eclipse project folder. Does it work ? – david blaine Apr 04 '13 at 06:23
-1
This code gets the path -
String absolutePath = new File(".").getAbsolutePath();
System.out.println(absolutePath);// Shows you the path of your Project Folder
int last = absolutePath.length()-1;
absolutePath = absolutePath.substring(0, last);//Remove the dot at the end of path
System.out.println(absolutePath);
String filePath = "MyFolderInsideEclipseProject\\file.txt";//You know this
System.out.println(absolutePath + filePath);//Get the full path.

Brad Larson
- 170,088
- 45
- 397
- 571

david blaine
- 5,683
- 12
- 46
- 55
-
`new File(".")` is the *current directory*, not where the class is loaded from. There are other similar questions on SO, for example [Find where java class is loaded from](http://stackoverflow.com/questions/227486/find-where-java-class-is-loaded-from/227640#227640), if it is location of the class files you are interested in. – Miserable Variable Apr 04 '13 at 17:32