Suppose the use of java.io
has been blocked. What are some alternative ways of writing, say a simple "Hello World!"
text file to disk, using Java/Groovy language features?
If only java.io
has been blocked (you said java.io
imports are blocked), you can use the java.nio
to write to files.
Look at the central Files
class. In java.nio
files/folders are represented with java.nio.Path
objects which is also part of the java.nio
package (and not java.io
).
Example writing "Hello World!"
into a text file to the disk:
Files.write(Paths.get("/your/folder/text.txt"),
"Hello World!".getBytes(StandardCharsets.UTF_8));
// Or
Files.write(Paths.get("/your/folder/text.txt"),
Arrays.asList("Hello World!"), StandardCharsets.UTF_8);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With