문제

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