How to use the Directory class

#1

I’m trying to create a directory whose path is designated by a given string such as "/tmp/my/directory".

I first tried using better.files, but that website is apparently no longer reachable.

[warn] 	module not found: com.github.pathikrit#better-files_2.12;2.16.0
[warn] ==== local: tried
[warn]   /Users/jnewton/.ivy2/local/com.github.pathikrit/better-files_2.12/2.16.0/ivys/ivy.xml
[warn] ==== public: tried
[warn]   https://repo1.maven.org/maven2/com/github/pathikrit/better-files_2.12/2.16.0/better-files_2.12-2.16.0.pom
[warn] ==== local-preloaded-ivy: tried
[warn]   /Users/jnewton/.sbt/preloaded/com.github.pathikrit/better-files_2.12/2.16.0/ivys/ivy.xml
[warn] ==== local-preloaded: tried
[warn]   file:////Users/jnewton/.sbt/preloaded/com/github/pathikrit/better-files_2.12/2.16.0/better-files_2.12-2.16.0.pom
[warn] 	::::::::::::::::::::::::::::::::::::::::::::::
[warn] 	::          UNRESOLVED DEPENDENCIES         ::
[warn] 	::::::::::::::::::::::::::::::::::::::::::::::
[warn] 	:: com.github.pathikrit#better-files_2.12;2.16.0: not found

Then I found this Directory class which has a method createDirectory. To create a new object of type Directory I need an object of type JFile.

Can someone explain to me how to get an object of type JFile from a string such as "/tmp/my/directory"

#2

That Directory class seems to be some internal helper class of the Scala compiler. I wouldn’t recommend it. Apparently 2.16.0 is a pretty old version of better-files which doesn’t appear to be released for Scala 2.12. You can find more recent versions here.
I can also recommend https://github.com/lihaoyi/os-lib. It makes doing something simple like creating a directory actually simple.

#3

I haven’t come across os-lib, yet, but given my exceptionally good experience with other libraries created by its maintainer, I’d certainly recommend to give it a try.

Usually I just make do with the Java nio library. It comes with the usual Java yak shaving, but with a few thin makeshift helpers/wrappers it’s bearable, and, quite important to me personally, with jimfs there’s a (sadly not full-featured) in-memory implementation for use in test cases, etc.

#4

Isn’t that what the standard Java File class is for?

https://docs.oracle.com/javase/7/docs/api/java/io/File.html

#5

java.io has been kind of deprecated in favor of java.nio (although it’s not officially marked as such in the API docs).

#6

OK, then which class in java.nio replaces the File class in java.io?

#7

java.nio.file.Path is replacement for java.io.File. Path misses some functionality that File has, but there are extra utility methods in java.nio.file.Paths and java.nio.file.Files that operate on Paths.

#8

Looks like I can more easily create the directory as follows.

new java.io.File("/full/path/to/mydir").mkdirs()

Found that idiom on Rosetta Code.

#9

With Java NIO that would be something like:

Files.createDirectories(Paths.get("/full/path/to/mydir"))
#10

How do PrintStream and PrintWriter fare in this transition? Does java.nio have equivalent classes? Also, will java.io eventually be deprecated, or will it continue indefinitely? Thanks.

#11

I doubt that the whole java.io package will be deprecated, more likely only parts with direct replacements, mostly the File class, will be.

Even with nio, the usual way to open files is with streams or readers / writers, the java.nio.Files class provides methods to open a new InputStream or OutputStream as well as buffered readers/writers. So you can still create an OutputStream from a Path to use with a PrintWriter.