mardi 6 février 2018

Java 8 Streams IO - Tests

I've written a simple class whose reading and writing files by using a single stream.

Now I want to test it by using JUnit test or something like that but I have no clue where to start with this because as u can see this is only one stream and text is immediately printing to a new file.

public class OneStreamIOFile {

  public void fileReaderWriter(String fileReadPath, String filePrintName) {
    try (Stream<String> streamReader = Files.lines(Paths.get(fileReadPath));
        PrintWriter printWriter = new PrintWriter(filePrintName)) {

      streamReader
          .filter(line -> line.matches("[\\d\\s]+"))
          .map(line -> Arrays.stream(line.trim().split("[\\s]+"))
              .reduce((a, b) -> a + "+" + b).get() + "="
              + Arrays.stream(line.trim().split("[\\s]+"))
              .mapToInt(Integer::valueOf).sum())
          .forEachOrdered(printWriter::println);

    } catch (IOException e) {
      System.out.println("File not found");
      e.printStackTrace();
    }
  }
}

Main class

  public class Main {
        public static void main(String[] args) {
            String filePath = "src/test/java/resources/1000.txt";
            String filePrintName = "resultStream.txt";

            new OneStreamIOFile().fileReaderWriter(filePath, filePrintName);
        }
    }

Any idea how to deal with this?

Aucun commentaire:

Enregistrer un commentaire