D
D
d_shister2015-05-14 12:31:36
linux
d_shister, 2015-05-14 12:31:36

How to get the physical address of a file using Java (not mounted partition)?

I need to write a function that will return the total amount of available disk space according to the list of input strings (directory path): long getTotalSpace(String pathDir1, String pathDir2){ ... }.
The problem is that pathDir1 and pathDir2 can be on the same mounted partition, in which case TotalSpace does not need to be summed. I can't determine the real path to the file
. Tried to use toRealPath(), getCanonicalPath(), getAbsolutePath() - displays a symbolic link
. For example, I specify pathDir1 = /dir1/dor1_1 - displays the same thing, but you need: /dev/mapper/disk_01/dor1_1
Please tell me the solution

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ix_Didicus, 2015-05-15
@Ix_Didicus

Look towards the java.nio.file package .
For example, the Files class has an interesting isSameFile method. I quickly jotted down the following code:

import java.nio.file.Files;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;

public class UnixPath{
  public static void main(String[] args) throws Exception {
    Path path1 = FileSystems.getDefault().getPath(args[0]);
    Path path2 = FileSystems.getDefault().getPath(args[1]);
    System.out.println(Files.isSameFile(path1, path2));
  }
}

I'm too lazy to reboot into Linux, but on Windows it worked as it should (I passed the real name of the folder as parameters and the hardlink leading to it returned true). Even if that's not it, try to take a closer look at the java.nio package, if there's an answer to your question somewhere, it's most likely there.
edit: Checked in Linux on a symlink, also true.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question