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 235 views
1 answer 209 views
2 answers 215 views
1 answer 116 views
116 views asked Aug 6, 2023 by avibootz
1 answer 287 views
2 answers 247 views
247 views asked Jun 14, 2016 by avibootz
...