Java I/O: Files, Streams, NIO and Serialization
Java IO tutorial for Java 25 covering Path and Files, byte and character streams, NIO channels, how IO works in the OS, and every file task by topic.
Java IO is an API that comes with Java which is targeted at reading and writing data (input and output). Most applications need to process some input and produce some output based on that input. For instance, read data from a file or over network, and write to a file or write a response back over the network.
The Java IO API is located in the Java IO package (java.io).
Use these tutorials for some nice concepts aroung Java IO APIs.
Java IO tutorial for Java 25 covering Path and Files, byte and character streams, NIO channels, how IO works in the OS, and every file task by topic.
Find a file in a directory and its subdirectories in Java with Files.find(), Files.walk() and walkFileTree(), with depth limits and globs.
Copy an InputStream to OutputStream in Java with transferTo(), a custom loop, Files.copy(), Guava or Commons IO, plus the closing and flushing rules.
Convert a Reader to InputStream in Java with a String, OutputStreamWriter or Commons IO ReaderInputStream, and back with InputStreamReader and a charset.
Java create directory examples with Files.createDirectories() and createDirectory(), parent folders, permissions, mkdirs() pitfalls and race-free checks.
Count lines in a file in Java with Files.lines(), LineNumberReader, readAllLines() or a fast byte counter, with edge cases like a missing final newline.
Java check if directory is empty with Files.list() or newDirectoryStream(), closing the stream, files and missing paths, hidden files and subfolders.
Read the file creation date in Java with BasicFileAttributes, format the FileTime, and see which platforms and JDK versions return the real value.
Create, detect and read symbolic links in Java with Files, control link following with NOFOLLOW_LINKS, compare hard links and switch releases atomically.
Learn to delete the contents of a file in Java without deleting it, with Files.write, FileChannel.truncate and writers, plus partial truncation.
Write a byte array to file in Java with Files.write, FileOutputStream and Files.copy, covering overwrite, append, parent folders and atomic writes.
Read a specific line from a file in Java by line number with Files.lines(), readAllLines() or BufferedReader, handle missing lines and index large files.
Rename or move a file in Java with Files.move(), replace targets, use ATOMIC_MOVE, move folders across disks and avoid File.renameTo() pitfalls.
Get the file size in Java with Files.size(), compute a directory size with Files.walk(), convert bytes to KB, MB and GB, and check free disk space.
Java FileWriter explained with constructors, write and append methods, append mode, the UTF-8 default since Java 18, flush, close and the alternatives.
Java InputStreamReader decodes bytes from an InputStream into characters. Learn charsets, read methods, malformed input, closing rules and alternatives.
Learn the Java FileReader class with UTF-8 charset constructors, char[] and line reading, error handling and when to use Files.newBufferedReader instead.
Learn to read a file from the classpath in Java, how class loaders resolve names, Class vs ClassLoader, context class loader, JARs and modules.
Java append to file examples with StandardOpenOption.APPEND, BufferedWriter, FileWriter and FileOutputStream, plus new lines, missing files and threads.
Java write to file examples with Files.writeString, Files.write, BufferedWriter, PrintWriter and streams, plus append options, UTF-8 and atomic writes.
Learn to read a file to String in Java with Files.readString() (Java 11), Files.readAllBytes(), Files.readAllLines() and lines(), BufferedReader, Scanner and FileInputStream, plus how to read from the classpath, avoid charset and line separator mistakes and stream large files.
List all files in a directory in Java with Files.list, Files.walk, Files.find and DirectoryStream, incl. recursion, filters by extension and errors.
Java WatchService tutorial with register, poll, take, reset, OVERFLOW handling, recursive watching, single-file reloads and macOS and Linux caveats.
Convert OutputStream to InputStream in Java with ByteArrayOutputStream, piped streams in a second thread or a temp file, and avoid pipe deadlocks.
Unzip a file in Java with ZipFile or ZipInputStream, extract subdirectories, block zip slip and zip bombs, and read archives as a zip file system.
Get the current working directory in Java with user.dir, Path.of(“”) and File, plus normalize(), JAR location and why changing user.dir breaks paths.
Learn to read a file from the resources folder in Java with getResourceAsStream(), slash rules, why File fails in a JAR, and Spring ResourceLoader.
Create a password protected zip in Java with Zip4j 2.11 and AES-256, extract it with the password, handle a wrong password and pick AES over ZipCrypto.
Copy a directory recursively in Java with Files.walk() and Files.copy(), merge into existing folders, keep timestamps, filter files and copy links.
Make a file read-only in Java with setReadOnly(), POSIX permissions or the DOS attribute, make it writable again and see what read-only does not stop.
Learn to copy a file in Java with Files.copy(), overwrite with REPLACE_EXISTING, keep timestamps, copy streams and avoid half-written copies.
Java FilenameFilter examples to find files by extension, prefix or regex with list() and listFiles(), plus glob patterns with PathMatcher.
Java BufferedReader examples for readLine(), lines(), mark and reset, buffer size and console input, plus LineNumberReader for line numbers in errors.
Java create new file examples with Files.createFile(), parent folders, CREATE_NEW writes and permissions, and how to avoid overwriting existing files.
Create a temp file or folder in Java with Files.createTempFile() and Files.createTempDirectory(). See how the older File.createTempFile() differs, and delete temp files safely with finally, DELETE_ON_CLOSE, deleteOnExit() or JUnit @TempDir.
Java check if file exists with Files.exists(), notExists(), isRegularFile() and isDirectory(), plus symbolic links, the unknown case and the TOCTOU race.
Read file to byte array in Java with Files.readAllBytes, readNBytes for headers, chunked reading for large files, classpath resources and Commons IO.
Read and write UTF-8 files in Java with Files.readString, writeString and readers, the Java 18 UTF-8 default, MalformedInputException and the BOM.
Learn to read a properties file in Java with load(), write it with store(), load from the classpath, use defaults, update safely and handle UTF-8.
Read input from console in Java with Scanner, Java 25 IO.readln(), BufferedReader and Console, fix the nextLine() skip and validate numbers safely.
Java FileFilter examples with lambdas and listFiles(), filters by size, date and hidden flag, null-safe listing and the NIO DirectoryStream.Filter option.
Java delete directory recursively with Files.walk() or walkFileTree(), plus Files.delete() for single files, symbolic links, library options and failures.
Read a file line by line in Java with Files.lines, BufferedReader, readAllLines and Scanner, plus charsets, MalformedInputException and large files.
Java IO vs NIO compared for Java 25. Streams vs buffers, blocking vs selectors, NIO.2 Files and Path, virtual threads, and a table of which API to use.
Convert an InputStream to String in Java with readAllBytes, readAllAsString or BufferedReader, plus String to InputStream and StringReader examples.
Read a CSV file in Java the right way. See why split() fails on quoted commas and parse rows with Apache Commons CSV, OpenCSV and Jackson CSV records.
HowToDoInJava provides tutorials and how-to guides on Java and related technologies.
It also shares the best practices, algorithms & solutions and frequently asked interview questions.