Java Create Directory: createDirectories() vs mkdirs()
Java create directory examples with Files.createDirectories() and createDirectory(), parent folders, permissions, mkdirs() pitfalls and race-free checks.
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.
JUnit 5 @TempDir creates temporary directories for tests. Learn parameter, field and static scope, CleanupMode, TempDirFactory and JUnit 6.1 deletion.
Java FileWriter explained with constructors, write and append methods, append mode, the UTF-8 default since Java 18, flush, close and the 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 write string to file with Files.writeString(), overwrite or append with open options, choose a charset, create folders, replace files safely, Java 8.
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.
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.
Learn to read a file from the resources folder in Java with getResourceAsStream(), slash rules, why File fails in a JAR, and Spring ResourceLoader.
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 create new file examples with Files.createFile(), parent folders, CREATE_NEW writes and permissions, and how to avoid overwriting existing files.
FileChannel transferTo() and transferFrom() examples. Copy and merge files, send a file to a socket, save streams, and loop past the 2 GB per-call limit.
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.
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.
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.
Java NIO read file examples. Use Files.readString(), read small and large files with FileChannel and ByteBuffer, decode UTF-8 safely and map large files.
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.