Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
368 views
in Technique[技术] by (71.8m points)

java - create and write files , return boolean

Write a public static method named q1 that takes no parameters and has return type boolean. This method will attempt to open a file named "location.txt" and returns true if the file exists and contains the String "statistical" as a sub-String on any line, and false if "statistical" is not found. This method will also return false if "location.txt" does not exist.

This is what I did, Im not sure how to put this as a boolean.

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;

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Using the Stream API introduced in Java 8:

/**
 * 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();
    }
}

Edit: Using try-with-resource to dispose file system resources.

The returned stream encapsulates a Reader. If timely disposal of file system resources is required, the try-with-resources construct should be used to ensure that the stream's close method is invoked after the stream operations are completed.

Edit 2: Unwrapping the stream's UncheckedIOException to make it easier for the caller to handle exceptions.

After this method returns, then any subsequent I/O exception that occurs while reading from the file or when a malformed or unmappable byte sequence is read, is wrapped in an UncheckedIOException that will be thrown from the Stream method that caused the read to take place. In case an IOException is thrown when closing the file, it is also wrapped as an UncheckedIOException.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...