Reputation: 1670
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
?
Upvotes: 0
Views: 280
Reputation: 7461
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"));
Upvotes: 0
Reputation: 4189
You can get files by getting output after executing ls -d /etc/*-release
.
And then work with them via Java File
.
See also:
Upvotes: 0
Reputation: 5802
Use Java java.io.File#listFiles and then simply iterate over the array that it returns to open the files.
Upvotes: 2
Reputation: 17622
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");
}
});
Upvotes: 6