1
| Path path = FileSystems.getDefault().getPath(".", name); |
Want to read all of the bytes in that file? No more looping, here it is:
1
| byte[] filearray = Files.readAllBytes(path); |
Would you rather deal with the file line-by-line? No problem:
1
| List |
Is it a large file that you'd rather use buffered IO with? Here you go:
1
2
3
4
| BufferedReader reader = Files.newBufferedReader(path, Charset.defaultCharset() );String line = null;while ( (line = reader.readLine()) != null ) { /* … */ } |
Surely, it must take more than that to create and write to file, right? Wrong:
1
2
3
| String content = …Files.write( path, content.getBytes(), StandardOpenOption.CREATE); // create new, overwrite if exists |
You can buffer the output and write all of the bytes with one extra line of code:
1
2
3
4
| BufferedWriter writer = Files.newBufferedWriter( path, Charset.defaultCharset(), StandardOpenOption.CREATE);writer.write(content, 0, content.length()); |
No comments:
Post a Comment