Search code examples
javanio

Get content of a file in Java


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?


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