Professional Documents
Culture Documents
Utility Classes of The JDK - Dealing With I - O PDF
Utility Classes of The JDK - Dealing With I - O PDF
Utility Classes of The JDK - Dealing With I - O PDF
You have 2 free stories left this month. Sign up and get an extra one for free.
Java NIO (“non-blocking I/O”) is a great feature set for dealing with I/O
operations, introduced by Java 1.4. It was further improved in Java 7
(JSR 203). This release also brought us the two utility classes of this
article: java.nio.file.Files and java.nio.file.Paths
At least Java 8 is assumed. All listed methods omit the static keyword, and
generic type information might be simplified to reduce visual clutter and
improve readability. Every method is linked to the Java Platform SE 8
documentation provided by Oracle.
. . .
. . .
java.nio.file.Paths
The java.nio.file.Paths isn’t a “full-fledged” utility class with lots of
different methods. It’s a mere helper for creating a java.nio.file.Path
instance:
. . .
java.nio.file.Files
The java.nio.file.Files utility class is a behemoth. In Java 8, it consists
of 65 static methods. The latest LTS, Java 11, upped the number to 69.
java.nio.file.OpenOption
We can influence how files are opened by providing one or more
java.nio.file.OpenOption . There are multiple options available, defined
in java.nio.file.StandardOpenOption :
APPEND
CREATE
DELETE_ON_CLOSE
READ
SPARSE
TRUNCATE_EXISTING
WRITE
There are also special methods for dealing with temporary objects. They
will create a file or directory in the default temporary-file directory, with
a randomized name. But we can provide a prefix/suffix for better
identification:
Reading content
Content reading can be categorized into two groups: byte-based and
character-based.
Both categories are filled with methods for reading “all-at-once”, or “on-
demand/lazy”.
Byte-based
Character-based
Writing
Like reading, writing content can be separated into two groups:
Byte-based
Character-based
Directory content
There are three different kinds of iterating/walking over directory
content:
Attributes
The usual file attributes can be read and written:
File operations
Last but not least, the routine file operations.
ATOMIC_MOVE
COPY_ATTRIBUTES
REPLACE_EXISTING
Java 11 additions
The release of Java 11 brought us four new methods for reading/writing
Strings:
. . .
Conclusion
Another excellent utility class that simplifies a lot of complicated I/O
operations, without the need for a third-party-library.
But be aware that almost all the methods throw various exceptions!
Never trust I/O operations blindly, and always catch (and handle) their
exceptions.
. . .
Resources
java.nio.file.Paths
java.nio.file.Files
71 claps
WRIT T EN BY
7 Working From Home Movie Recommendation: Medium Around T he Divorcing the Narcissist
T ips to Be More Computer Chess Block: #5 Dr. Jennifer B. Rhodes in T he
E ective Right Now Kitanya Harrison Mike Dudas in T he Block Startup
Dan Silvestre in T he Startup
Am I More than Just a Finding Room for Old Patrisse Cullors Is Anatomy Of An
Piece of Meat? Baggage in a New Reshaping America Ethereum Phishing Scam
Gillian Sisley in Fearless She Relationship Bioneers in Bioneers On Facebook
Wrote Tesia Blake Mark Muskardin in Coinmonks