java.nio package came available in java 1.4 to read and write data to a file
If we compare java.nio with our old java.io methods we see that to create constructor of BufferedWriter we use Files.newBufferedWriter and it accepts an instant of path object.
The old methods using java.io packages used an object of file class.
Basically java.nio packages use Path instance and not File instance when reading and writing data to files
Path instance - Path locPath = FileSystems.getDefault().getPath("locations.txt");
Creating BufferedWriter objects using path instances
try(BufferedWriter locFile = Files.newBufferedWriter(locPath);
BufferedWriter dirFile = Files.newBufferedWriter(dirPath)){
Old way of doing things usings io methods and using instances of file class instead of path class
try(BufferedWriter locfile = new BufferedWriter(new FileWriter("locations2.txt"));
BufferedWriter directionfile = new BufferedWriter(new FileWriter("directions2.txt"));)
Please note old methods of reading and writng are still preferred way and new java.nio package is if we want to use it or for knowledge purpose.
Same way for reading data from a file instead of using FileReader or BufferedReader classes directly we will use java nio package to first create Path objects of files are reading and then use method Files.newBufferedReader to to read that data. ``` Path locPath = FileSystems.getDefault().getPath("locations.txt"); Path dirPath = FileSystems.getDefault().getPath("directions.txt");
try(Scanner scanner = new Scanner(Files.newBufferedReader(locPath)))
try(BufferedReader dirFile = Files.newBufferedReader(dirPath)){
Complete code to write data to file is below. Please note the location is a map that contaisn Location class objects as values as seen in previous examples
public static void main(String[] args) throws IOException {
Path locPath = FileSystems.getDefault().getPath("locations.txt");
Path dirPath = FileSystems.getDefault().getPath("directions.txt");
try(BufferedWriter locFile = Files.newBufferedWriter(locPath);
BufferedWriter dirFile = Files.newBufferedWriter(dirPath)){
for(Location location:locations.values()){
locFile.write(location.getLocationId()+","+location.getDescription()+"\n");
}
}
catch(IOException e){
e.printStackTrace();
}
}
And once the data is wriiten to a file we can use static initialisation block in next example to read data from file using java nio package
static {
Path locPath = FileSystems.getDefault().getPath("locations.txt");
Path dirPath = FileSystems.getDefault().getPath("directions.txt");
try(Scanner scanner = new Scanner(Files.newBufferedReader(locPath))) {
scanner.useDelimiter(",");
while(scanner.hasNext()){
int loc = scanner.nextInt();
scanner.skip(scanner.delimiter());
String description = scanner.nextLine();
System.out.println("Imported description =============> "+loc+" , "+description);
}
} catch (IOException e) {
e.printStackTrace();
}
We can even read using the below method from a different file. Note here we are using a buffered reader instead of using a scanner and instead of using scanner.hasNext in while loop we are using dirFile.readLine!=null for the buffered reader object
try(BufferedReader dirFile = Files.newBufferedReader(dirPath)){
String input;
while((input=dirFile.readLine())!=null){
String[] data = input.split(",");
int loc = Integer.parseInt(data[0]);
String direction = data[1];
int destination = Integer.parseInt(data[2]);
System.out.println(loc+":"+direction+":"+destination);
Location location = locations.get(loc);
location.addExit(direction,destination);
}
}
catch (IOException e) {
e.printStackTrace();
}
}