我的文件夹中有很多文件,它们都是.yml文件,我需要从所有这些文件中读取字符串的结果,如果结果等于X,请执行某些操作。例:
user-data:
name: Lucas
age: 33
gender: F
etc...
我需要检查文件是否包含字符串(例如“ age”)以及结果(在这种情况下为int)和
如果扫描结果> = 18则执行某项操作
我尝试使用扫描仪,但不知道如何完成
File f = new File(pl.getDataFolder() + File.separator + "userdata");
File folder = new File(f.getAbsolutePath());
File[] data = folder.listFiles();
if (data != null) {
for (File datum : data) {
final Scanner scanner = new Scanner(datum);
while (scanner.hasNextLine()) {
final String lineFromFile = scanner.nextLine();
/*Read the whole file, get the string "age" and check the result (= after ":") and return it*/
}
}
}
最佳答案
作为建议,请勿使用扫描仪执行此任务。...太慢了。使用类似BufferedReader的方法:
// The path to the directory which holds all the '.yml' files.
String path = pl.getDataFolder() + File.separator + "userdata";
File folder = new File(path);
// Does the supplied directory exist?
if (!folder.exists()) {
throw new RuntimeException("Invalid Directory Path Supplied! (" + path + ")");
}
// Acquire a list of files contained within the supplied directory
File[] dataFiles = folder.listFiles();
// If there are files then iterate through them to read.
if (dataFiles != null) {
// Read each file...
for (File datum : dataFiles) {
/* Skip the file if it's a directory, can't be read for some reason,
or doesn't contain a '.yml' or '.YML' file name extension. */
if (datum.isDirectory() || !datum.canRead() || !datum.getName().toLowerCase().endsWith(".yml")) {
continue;
}
// A File Reader - 'Try With Resourses' is used here to auto-close the reader.
try (BufferedReader reader = new BufferedReader(new FileReader(datum))) {
String lineFromFile;
// Read through each line of the current file line-by-line
while ((lineFromFile = reader.readLine()) != null) {
// Trim leading & trailing whitespaces from file line
lineFromFile = lineFromFile.trim();
/* Skip all file lines that are blank, start with a hash character (#),
and does not contain the property name of 'age:'. */
if (lineFromFile.equals("") || lineFromFile.startsWith("#") ||
!lineFromFile.matches("(?i)(.*)?(\\s+)?(age:)(\\s+)?(\\d+)?")) {
continue;
}
// Split the found 'age:' property to get its value.
String[] ageParts = lineFromFile.split(":");
int age = 0;
// Convert the value in the 'age:' property to Integer
if (ageParts.length == 2) {
age = Integer.parseInt(ageParts[1].trim());
}
// If age is 18 or more
if (age >= 18) {
System.out.println("Do what you want with any age 18 or older! "
+ "(from file: " + datum.getName() + " - age: "+ age + ")");
}
// We have what we came for so get out of reading
// the rest of this particular file.
break;
}
}
// Handle Exceptions...
catch (FileNotFoundException ex) {
System.err.println("File Not Found! (" + datum.getAbsolutePath() + ")");
}
catch (IOException ex) {
System.err.println("I/O Error!" + ex.getMessage() +
" (" + datum.getAbsolutePath() + ")");
}
}
}
String#matches()方法中使用的正则表达式的说明:
(?一世)
将模式的其余部分与以下有效标志匹配:gmi
i修饰符:不敏感。不区分大小写的匹配(忽略[a-zA-Z]的大小写)。
第一捕获组:(。*)?
?量词-匹配零到一遍,尽可能多地匹配,并根据需要返回(贪婪)
.
匹配任何字符(行终止符除外)。*
量词-在零和无限制的时间之间进行匹配,并尽可能地多次匹配,并根据需要返回(贪婪)。第二捕获组(\ s +)?
?
量词-匹配0到1次,尽可能多地匹配,并根据需要返回(贪婪)。\s+
匹配任何空格字符(等于[\ r \ n \ t \ f \ v])。+
量词-匹配一次和无限次,尽可能多地匹配,并根据需要返回(贪婪)。第三捕获组(年龄:)
age:
匹配字符age:字面意义(不区分大小写)。第四捕获组(\ s +)?
?
量词-匹配0到1次,尽可能多地匹配,并根据需要返回(贪婪)。\s+
匹配任何空格字符(等于[\ r \ n \ t \ f \ v])。+
量词-匹配一次和无限次,尽可能多地匹配,并根据需要返回(贪婪)。第五捕获组(\ d +)?
?
量词-匹配0到1次,尽可能多地匹配,并根据需要返回(贪婪)。\d+
匹配一个数字(等于[0-9])。+
量词-匹配一次和无限次,尽可能多地匹配,并根据需要返回(贪婪)。
关于java - 如何从文件(.yml)中读取字符串结果,从而通读多个文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59444224/