Java I/O – Get file name and file path




Java Tutorial for Beginners
Java Tutorial for Beginners

In Java File class a member method getName() returns the name of the file or directory denoted by this abstract path name, or the empty string if this path-name’s name sequence is empty

and getPath() method returns the path of a file.

Lets take an example to understand the two methods in a better way. In this example we will construct the file path using File constructor.

/**
 * Created by codebind.com.
 */
import java.io.File;
public class Sample {
    public static void main(String[] args) {
        File myFile = new File("C:" + File.separator + "jdk" + File.separator, "FileName.java");
        System.out.println(myFile.getName());
        System.out.println(myFile.getPath());

    }
}

OUTPUT :

FileName.java
C:\jdk\FileName.java

Reference:
https://docs.oracle.com/javase/7/docs/api/java/io/File.html


Partner Sites

VideoToGifs.com

EasyOnlineConverter.com

SqliteTutorials.com





Be the first to comment

Leave a Reply

Your email address will not be published.


*