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 330 views
1 answer 290 views
2 answers 348 views
1 answer 213 views
213 views asked Aug 6, 2023 by avibootz
1 answer 369 views
2 answers 368 views
368 views asked Jun 14, 2016 by avibootz
...