stream and file i/o Path File Reading File writing Exploring Folders

  • finally blocks
  • Multi-catch : using | (also consider catching a parent type)

    1
    2
    3
    4
    5
    6
    7
    String input = getSomeString();
    int num;
    try {
    num = Integer.parseInt(input)
    } catch (NumberFormatException | NullPointerException e) {
    num = defaultValue;
    }
  • try-with-resources (Java7) : Can declare variables that implements AutoCloseable in parents after try

    • Scope of variable is scope of try/catch block
    • The close method of each variable is called at the end (like a finally block)
    • Can declare multiple variables, separated by semicolon
1
2
3
4
5
try (BufferedReader reader = ....){

} catch (SomeExceptionType e) {
...
}

Path

  • Path is a simpler and more flexibable replacement for File class
  • Get a Path with Paths.get
  • Paths have convenients methods : toAbsoluteParth, startWith, getParent…

File Reading

Read lines
Stream<String> lines = Files.lines(somePath)

  • Charset Option : Default is UTF-8, but you can uses Files.lines(path, someCharset)
  • Throws IOException : use a try/catch block or throw and exception
  • Stream should be closed
  • Stream implements AutoCloseable

File writing

Base version

Write all entries of a List into a file in one method call

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.cyrgue.fileio;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;

public class {

public static void main(String[] args) throws IOException {
Charset charset = Charset.defaultCharset();
Path path = Paths.get("output-file-1.txt");
List<String> lines = Arrays.asList("Line One", "Line Two");

Files.write(path, lines, charset);
}
}

OpenOption
Files.write(somePath, lines, someCharset, someOption)
used to create, override a file…

Files.write is not flexible, not very efficient

Faster and more flexible File Writing

  • to be able to format String as you insert them into the file
  • to be able not to store everything in memory
  • to be able to buffered writing in blocks (faster for very large files)

    Use Files.newBufferedWriter(path, charset) and PrintWriter out = new PrintWriter(Files.newBufferedWriter(path, charset))

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    package com.cyrgue.fileio;

    import java.io.IOException;
    import java.io.PrintWriter;
    import java.nio.charset.Charset;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;

    public class BetterFileWritingPrg {

    public static void main(String[] args) throws IOException {
    Charset charset = Charset.defaultCharset();
    int numLines = 10;
    Path path = Paths.get("output-file-2.txt");

    try (PrintWriter out = new PrintWriter(Files.newBufferedWriter(path, charset))) {

    for (int i = 0; i < numLines; i++) {
    out.printf("Number is %5.2f%n", 100*Math.random());
    }
    } catch (IOException ex) {
    System.err.printf("IOException %s%n", ex);
    }

    }
    }

Exploring Folders

  • Get all files in a folder Files.list
  • Get all files in and below a folder Files.walk
  • GEt matching files in and below a folder Files.find (with a BiPredicate (Path and BasicFileAttributes))