top of page
Writer's pictureKlemens Morbe

different styles when coding #5

when it comes to reading in some file stored on disk, there are plenty of different approaches reading the content in. i tried to cover some of them using plain java without any dependencies to apache commons io or google guava. i skipped the methods to read files existing in classpath or inside java archives. all of my styles try using the try-with-resources mechanism, which is in my opinion a good way of handling the io close operation and keeping the code clean.


style 1:

public static Optional<String> scanner(final String path) {
    final var content = new StringBuilder();
    try (var scanner = new Scanner(new File(path))) {
        while (scanner.hasNextLine()) {
            content.append(scanner.nextLine());
            content.append(System.lineSeparator());
        }
        return Optional.of(content.toString());
    } catch (final IOException e) {
        return Optional.empty();
    }
}

style 2:

public static Optional<String> scanner2(final String path) {
    try (final var scanner = new Scanner(new FileReader(path))) {
        scanner.useDelimiter("\\Z");
        return Optional.of(scanner.tokens().collect(Collectors.joining()));
    } catch (FileNotFoundException e) {
        return Optional.empty();
    }
}

style 3:

public static Optional<String> scanner3(final String path) {
    try (final var scanner = new Scanner(new FileReader(path))) {
        scanner.useDelimiter("\\Z");
        return Optional.of(scanner.next());
    } catch (FileNotFoundException e) {
        return Optional.empty();
    }
}

style 4:

public static Optional<String> files(final String path) {
    try {
        return Optional.of(Files.readAllLines(Paths.get(path)).stream().reduce("",
                (s, str) -> s.concat(str).concat(System.lineSeparator())));
    } catch (IOException e) {
        return Optional.empty();
    }
}

style 5:

public static Optional<String> files2(final String path) {
    try {
        return Optional.of(Files.lines(Paths.get(path)).collect(Collectors.joining(System.lineSeparator())));
    } catch (IOException e) {
        return Optional.empty();
    }
}

style 6:

public static Optional<String> oldschool(final String filename) {
    final var file = new File(filename);
    try (final var reader = new FileReader(file)) {
        final var chars = new char[(int) file.length()];
        reader.read(chars);
        return Optional.of(new String(chars));
    } catch (IOException e) {
        return Optional.empty();
    }
}

style 7:

public static Optional<String> oldschool2(final String filename) {
    final var file = new File(filename);
    final var lineBuffer = new StringBuilder();
    try (final var fin = new FileInputStream(file); final var bin = new BufferedInputStream(fin)) {
        int character;
        while ((character = bin.read()) != -1) {
            lineBuffer.append((char) character);
        }
        return Optional.of(lineBuffer.toString());
    } catch (final IOException e) {
        return Optional.empty();
    }
}

this time there a lot more styles, so which style are you, 1, 2, 3, 4, 5, 6, 7 or even something else, tell me about it in the comments. 😄


you can download the source code on github: https://github.com/KlemensM/styles-5


Recent Posts

See All

Comments


bottom of page