From d0735facef7bba13d317b021a91ec22b89231895 Mon Sep 17 00:00:00 2001 From: Mladen Savic Date: Sun, 16 Jan 2022 21:31:59 +0100 Subject: [PATCH] Difference between NIO and NIO.2? --- .../baeldung/niovsnio2/NioVsNio2UnitTest.java | 70 +++++++++++++++++++ .../src/test/resources/nio-vs-nio2.txt | 1 + 2 files changed, 71 insertions(+) create mode 100644 core-java-modules/core-java-nio-2/src/test/java/com/baeldung/niovsnio2/NioVsNio2UnitTest.java create mode 100644 core-java-modules/core-java-nio-2/src/test/resources/nio-vs-nio2.txt diff --git a/core-java-modules/core-java-nio-2/src/test/java/com/baeldung/niovsnio2/NioVsNio2UnitTest.java b/core-java-modules/core-java-nio-2/src/test/java/com/baeldung/niovsnio2/NioVsNio2UnitTest.java new file mode 100644 index 0000000000..7413ebda13 --- /dev/null +++ b/core-java-modules/core-java-nio-2/src/test/java/com/baeldung/niovsnio2/NioVsNio2UnitTest.java @@ -0,0 +1,70 @@ +package com.baeldung.niovsnio2; + +import org.junit.Test; + +import java.io.File; +import java.io.FileInputStream; +import java.io.RandomAccessFile; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; +import java.util.stream.Stream; + +import static org.assertj.core.api.Assertions.assertThat; + +public class NioVsNio2UnitTest { + + @Test + public void readFromFileUsingFileIO() throws Exception { + File file = new File("src/test/resources/nio-vs-nio2.txt"); + FileInputStream in = new FileInputStream(file); + StringBuilder content = new StringBuilder(); + int data = in.read(); + while (data != -1) { + content.append((char) data); + data = in.read(); + } + in.close(); + assertThat(content.toString()).isEqualTo("Hello from file!"); + } + + @Test + public void readFromFileUsingFileChannel() throws Exception { + RandomAccessFile file = new RandomAccessFile("src/test/resources/nio-vs-nio2.txt", "r"); + FileChannel channel = file.getChannel(); + StringBuilder content = new StringBuilder(); + + ByteBuffer buffer = ByteBuffer.allocate(256); + int bytesRead = channel.read(buffer); + while (bytesRead != -1) { + buffer.flip(); + + while (buffer.hasRemaining()) { + content.append((char) buffer.get()); + } + + buffer.clear(); + bytesRead = channel.read(buffer); + } + file.close(); + + assertThat(content.toString()).isEqualTo("Hello from file!"); + } + + @Test + public void readFromFileUsingNIO2() throws Exception { + List strings = Files.readAllLines(Paths.get("src/test/resources/nio-vs-nio2.txt")); + + assertThat(strings.get(0)).isEqualTo("Hello from file!"); + } + + @Test + public void listFilesUsingWalk() throws Exception { + Path path = Paths.get("src/test"); + Stream walk = Files.walk(path); + walk.forEach(System.out::println); + } +} diff --git a/core-java-modules/core-java-nio-2/src/test/resources/nio-vs-nio2.txt b/core-java-modules/core-java-nio-2/src/test/resources/nio-vs-nio2.txt new file mode 100644 index 0000000000..c12b07297d --- /dev/null +++ b/core-java-modules/core-java-nio-2/src/test/resources/nio-vs-nio2.txt @@ -0,0 +1 @@ +Hello from file! \ No newline at end of file