How to get the absolute path of a file 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 {

            String s = File.separator + "source-code" + File.separator + "app1.java";
            
            File file = new File(s);
            
            System.out.println(file.getAbsolutePath());

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


/*
        
run:
  
C:\source-code\app1.java

 */

 



answered Nov 17, 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 {

            String s = File.separator + "source-code" + File.separator + "graphic";
            
            File file = new File(s);
            
            System.out.println(file.getAbsolutePath());

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


/*
        
run:
  
C:\source-code\graphic

 */

 



answered Nov 17, 2016 by avibootz

Related questions

3 answers 278 views
1 answer 200 views
1 answer 173 views
1 answer 187 views
187 views asked Nov 17, 2016 by avibootz
1 answer 205 views
1 answer 115 views
...