Question

I want to get the version of Linux using Java code. In different Linux distributions I have file with diffrent name.

/etc/*-release

How I can get every file content which ends with -release?

Was it helpful?

Solution

You can use File.listFiles(FilenameFilter)

    File f = new File("/etc/");
    File[] allReleaseFiles = f.listFiles(new FilenameFilter() {

        @Override
        public boolean accept(File dir, String name) {
            return name.endsWith("-release");
        }
    });

OTHER TIPS

Use Java java.io.File#listFiles and then simply iterate over the array that it returns to open the files.

System.getProperty("os.version")

You can get files by getting output after executing ls -d /etc/*-release.

And then work with them via Java File.

See also:

Surely, you should use nio nowadays for this type of action:

Path dir = Paths.get("/etc");
for (Path file : Files.newDirectoryStream(dir, "*-release"))
    System.out.println (new String(Files.readAllBytes(file), "UTF-8"));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top