【发布时间】:2018-03-18 03:25:03
【问题描述】:
编写一个名为 q1 的公共静态方法,该方法不带参数,返回类型为布尔值。此方法将尝试打开名为“location.txt”的文件,如果文件存在并且在任何行包含字符串“statistical”作为子字符串,则返回 true,如果未找到“statistical”则返回 false。如果 "location.txt" 不存在,此方法也会返回 false。
这就是我所做的,我不知道如何将其作为布尔值。
public static boolean q1() {
String filename = x;
// creating file name location.txt
try {
String x = "location.txt";
System.out.print("location.txt file has been created");
String textToWrite = "statistical";
Files.write(Paths.get(x), textToWrite.getBytes());
}
catch (IOException e) {
boolean r = false;
return r;
}
BufferedReader br = new BufferedReader(new FileReader("location.txt"));
String textToWrite;
while ((textToWrite = br.readLine()) != null) {
}
return f;
}
【问题讨论】:
-
作业说你必须读取文件,那你为什么要写入呢?另外至少尝试打印异常,否则您不知道发生了什么。
标签: java