Last week we looked at how to manipulate files using a bunch of classes in Java.nio.file. This week we will learn how to read and write text files using classes in Java.nio.file package. Create a directory called “files” in the root folder of the project. Create a text file called “source.txt” in the files folder. Create two Path object to hold the source path (“files/source.txt”) and the target path (“files/target.txt”) Create a Charset object as shown below: Charset charset = Charset.forName(“US-ASCII”); Within a try-with-resources block, create a BufferedReader object using the newBufferedReader method of the Files class passing source path and charset as parameters. In the try-with-resources block that you created before, add a BufferedWriter object using Files.newBufferedWriter method passing target path and charset as parameter. Read using the BufferedReader’s readLine method and writ...
Education for everyone!