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?

有帮助吗?

解决方案

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");
        }
    });

其他提示

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"));
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top