Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 创建和写入文件,返回布尔值_Java - Fatal编程技术网

Java 创建和写入文件,返回布尔值

Java 创建和写入文件,返回布尔值,java,Java,编写一个名为q1的公共静态方法,该方法不接受任何参数,且返回类型为boolean。此方法将尝试打开名为“location.txt”的文件,如果该文件存在并且在任何行上包含字符串“statistical”作为子字符串,则返回true;如果未找到“statistical”,则返回false。如果“location.txt”不存在,此方法也将返回false 这就是我所做的,我不知道如何把它作为一个布尔值 public static boolean q1() { String filename

编写一个名为q1的公共静态方法,该方法不接受任何参数,且返回类型为boolean。此方法将尝试打开名为“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;

}

代码的第一部分似乎是创建一个满足给定标准的文件(也就是说,它使下面的代码和需求变得毫无意义)。不要那样做。逐行读取文件。检查您读取的行是否包含要搜索的字符串。如果是
则返回true
。否则
返回false
。像

public static boolean q1() {
    String fileName = "location.txt", toFind = "statistical";
    try (BufferedReader br = new BufferedReader(new FileReader(new File(fileName)))) {
        String line;
        while ((line = br.readLine()) != null) {
            if (line.contains(toFind)) {
                return true;
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}

使用Java 8中引入的流API:

/**
 * Returns whether the file 'location.txt' exists and any line contains the string "statistical".
 *
 * @return true if the file exists and any line contains "statistical", false otherwise
 * @throws IOException if an I/O error occurs
 */
public static boolean q1() throws IOException {
    Path path = Paths.get("location.txt");
    try (Stream<String> lines = Files.lines(path)) {
        return lines.anyMatch(line -> line.contains("statistical"));
    } catch (FileNotFoundException e) {
        return false;
    } catch (UncheckedIOException e) {
        // Stream wraps IOExceptions, because streams don't throw checked exceptions. Unwrap them.
        throw e.getCause();
    }
}
/**
*返回文件“location.txt”是否存在以及任何一行是否包含字符串“statistical”。
*
*@如果文件存在且任何一行包含“统计”,则返回true,否则返回false
*@在发生I/O错误时引发IOException
*/
公共静态布尔q1()引发IOException{
Path Path=Path.get(“location.txt”);
try(流行=文件。行(路径)){
返回行.anyMatch(行->行.contains(“统计”));
}catch(filenotfounde异常){
返回false;
}捕获(未勾选异常e){
//流包装IOExceptions,因为流不会抛出选中的异常。请打开它们。
抛出e.getCause();
}
}
编辑:使用“试用资源”来处置文件系统资源

返回的流封装了一个读卡器。如果需要及时处理文件系统资源,则应使用try-with-resources构造来确保在流操作完成后调用流的close方法

编辑2:展开流的未选中异常,使调用方更容易处理异常

此方法返回后,从文件读取时发生的任何后续I/O异常或读取格式错误或不可映射的字节序列时发生的任何后续I/O异常都会被包装在一个未选中的异常中,该异常将从导致读取的Stream方法中抛出。如果在关闭文件时抛出IOException,它也会包装为未选中的IOException


作业上说你们必须读文件,那个么你们为什么要写呢?另外,至少尝试打印异常,否则您不知道发生了什么。请向下投票人解释这个答案的问题是什么?它的特点是编写良好的代码;清晰合理的解释;正确解决了原有问题。