file path java code example

Example 1: get file path java

File file = new File("yourfileName");
String path = file.getAbsolutePath();

Example 2: method resolve file in java

private File initUsersText() throws Exception
{
    Path dir = testdir.getPath().toRealPath();
    FS.ensureDirExists(dir.toFile());
    File users = dir.resolve("users.txt").toFile();

    writeUser( users );
    return users;
}


private File initUsersText() throws Exception
{
    Path dir = testdir.getPath().toRealPath();
    FS.ensureDirExists(dir.toFile());
    File users = dir.resolve("users.txt").toFile();

    writeUser( users );
    return users;
}

Example 3: get absolute path from relative java

String absolutePath = FileSystems.getDefault()
  .getPath(mayBeRelativePath)
  .normalize()
  .toAbsolutePath()
  .toString();

Tags:

Java Example