How to get the parent directory as a File object in Java

2 Answers

0 votes
package javaapplication1;

import java.io.File;
import java.io.IOException;

public class JavaApplication1 {

    public static void main(String[] args) throws IOException {

        try {

            File file = new File("d:\\xampp\\mysql\\data\\seek4info");

            File parent = file.getParentFile();
            System.out.println(parent.getPath());

        } catch (Exception e) {
            System.out.print(e);
        }
    }
}

/*
         
run:
   
d:\xampp\mysql\data
 
 */

 



answered Nov 18, 2016 by avibootz
0 votes
package javaapplication1;

import java.io.File;
import java.io.IOException;

public class JavaApplication1 {

    public static void main(String[] args) throws IOException {

        try {

            File file = new File("d:\\xampp\\mysql\\data\\seek4info\\index.php");

            File parent = file.getParentFile();
            System.out.println(parent.getPath());

        } catch (Exception e) {
            System.out.print(e);
        }
    }
}

/*
         
run:
   
d:\xampp\mysql\data\seek4info
 
 */

 



answered Nov 18, 2016 by avibootz

Related questions

1 answer 331 views
1 answer 292 views
2 answers 353 views
1 answer 214 views
214 views asked Aug 6, 2023 by avibootz
1 answer 369 views
2 answers 369 views
369 views asked Jun 14, 2016 by avibootz
...