Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing to disk without using java.io

Tags:

java

groovy

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?

like image 550
kinbiko Avatar asked Sep 09 '14 09:09

kinbiko


1 Answers

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);
like image 84
icza Avatar answered Oct 04 '22 05:10

icza