Java Core
    Java - Overview
    What is new in Java 21
    What is new in Java 17
    What is new in Java 11
    Java - JVM
    Java - Deprecated
    Java - Numbers
    Java - String
    Java - Time
    Java - Collections
    Java - Lambda
    Java - Exceptions
    Java - Switch
    Java - IO
    Java - Jar
    Java - Generics
    Java - Environment
    Java - Concurrency
    Java - Trouble Shooting
    Java - Install
    Java - Misc

Java - IO

Updated: 2021-11-19

New IO

Since Java 7.

Key new classes: Path, Paths, FileSystem, FileSystems

Path path = Paths.get("/path/to")

Path path = FileSystems.getDefault().getPath(".", name);

Read all of the bytes in the file:

byte[] filearray = Files.readAllBytes(path);

Read all of the lines in the file:

List<String> lines = Files.readAllLines(path, Charset.defaultCharset());

Read by buffered IO:

BufferedReader reader = Files.newBufferedReader(path, Charset.defaultCharset() );
String line = null;
while ((line = reader.readLine()) != null) {
    // ...

Write to file

String content =// create new, overwrite if exists
Files.write( path, content.getBytes(), StandardOpenOption.CREATE);

Buffered writer:

BufferedWriter writer = Files.newBufferedWriter(path, Charset.defaultCharset(),
writer.write(content, 0, content.length());


Files.write(Paths.get("/path/to/file"), Arrays.asList("content"));


Files.write(Paths.get("/path/to/file"), Arrays.asList("content"), StandardOpenOption.APPEND);

Byte Streams

  • InputStream: FileInputStream
  • OutputStream: FileOutputStream
try (FileInputStream in = new FileInputStream("input.txt");
    FileOutputStream out = new FileOutputStream("output.txt")) {
  int b;
  while ((b = != -1) {

read() will read the next byte of data.

Character Streams

  • Reader: FileReader
  • Writer: FileWriter

try (
    FileReader inputStream = new FileReader("input.txt");
    FileWriter outputStream = new FileWriter("output.txt")) {
    int c;
    while ((c = != -1) {

Byte to Character

  • InputStreamReader
  • OutputStreamWriter


  • BufferedReader
  • PrintWriter
BufferedReader in = new BufferedReader(new FileReader(filename));

PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file)));
PrintWriter out = new PrintWriter(file);

Buffered Streams

  • BufferedInputStream
  • BufferedOutputStream
  • BufferedReader
  • BufferedWriter


try (
  Scanner s = new Scanner(new BufferedReader(new FileReader("input.txt")))) {

  while (s.hasNext()) {


PrintStream console = System.out;

BufferedInputStream in = new BufferedInputStream(
    new FileInputStream(""));

PrintStream out = new PrintStream(
    new BufferedOutputStream(
    new FileOutputStream("test.out")));



  • Data written by DataOutputStream is guaranteed to be accurately recovered by DataInputStream
  • System.out is a PrintStream, which is an OutputStream

Convert system.out to PrintWriter. Set second param to true to enable automatic flushing.

PrintWriter out = new PrintWriter(System.out, true);

Work on Directories


File dir = new File(pathStr);
for (File file : dir.listFiles()) {
    if (file.getName().startsWith("part-")) {
        scanner = new Scanner(new BufferedReader(new FileReader(file)));
        // do something ...


Stream st = new Stream(new FileOutputStream("output.txt"));

Read Twice

in = Path(uri));
IOUtils.copyBytes(in, System.out, 4096, false);; // go back to the start of the file
IOUtils.copyBytes(in, System.out, 4096, false);

Why BufferedWriter is not Writing

There are a few ways to create a BufferedWriter, e.g. by Files.newBufferedWriter

BufferedWriter writer = Files.newBufferedWriter(Paths.get("filename"), StandardCharsets.UTF_8, StandardOpenOption.CREATE);

or by new

BufferedWriter writer = new BufferedWriter(new FileWriter("filename"));

you may try to call writer.write() to dump some text to the file

writer.write("a line of text");

the program executes fine, but there's nothing in the file.

The reason is that BufferedWriter needs a flush to dump the buffered text to file:


Or this may happen when you close the writer