-3

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 ?

david blaine
  • 5,683
  • 12
  • 46
  • 55

3 Answers3

3

I think you can use File's getParent() method to do your desire job.

Sumit Singh
  • 15,743
  • 6
  • 59
  • 89
-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
-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