CSC 1302: PRINCIPLES OF COMPUTER SCIENCE II Lab 7 How to Submit Please submit your java source files (answers) to the iCollege ‘assignment’ drop box for your lab, once you have completed. Failure to submit within your given time period will result in a ZERO FOR THIS LAB. NO EXCEPTIONS. Complete the below program that uses java.nio library and the ‘Files’ ‘Path’ and ‘Paths’ objects. 1. Create the necessary code in ‘writeUsingNIO’ method to put these strings “Soccer” “Tennis” “Badminton” “Hockey” in the ‘sampleFile.txt’ file using the ‘.write()’ and ‘.newLine()’ methods from the ‘BufferedWriter’ object. Since Java7, we are required to close the ‘BufferedWriter’ and ‘BufferedReader’ objects, manually. You can see this in the ‘catch()’ and ‘finally’ parts of exception handling in each method. 2. Run this code and ensure the program outputs the four strings from #1 above. TYPE CODE WHERE IT IS BOLDED BELOW import java.io.*; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class TestFileWriteRead { public static void main(String[] args) { try { Path path = Paths.get("sampleFile.txt"); writeUsingNIO(path); readUsingNIO(path); } catch (Exception e) { e.printStackTrace(); } } private static void writeUsingNIO(Path Xpath) throws IOException { BufferedWriter bufferedWriter = Files.newBufferedWriter(Xpath); try { System.out.println("Writing contents to file."); //Type your code here } catch (IOException ioe) { bufferedWriter.close(); ioe.printStackTrace(); } finally { bufferedWriter.close(); } } private static void readUsingNIO(Path Xpath) throws IOException { BufferedReader bufferedReader = Files.newBufferedReader(Xpath); try { String line; System.out.println("Reading file"+ Xpath.getFileName().toString()); while ((line = bufferedReader.readLine()) != null) { System.out.println(line); } catch (IOException ioe) { bufferedReader.close(); ioe.printStackTrace(); } finally { bufferedReader.close(); } } }
CSC 1302: PRINCIPLES OF COMPUTER SCIENCE II Lab 7
How to Submit Please submit your java source files (answers) to the iCollege ‘assignment’ drop box for your lab, once you
have completed. Failure to submit within your given time period will result in a ZERO FOR THIS LAB. NO EXCEPTIONS.
Complete the below program that uses java.nio library and the ‘Files’ ‘Path’ and ‘Paths’ objects.
1. Create the necessary code in ‘writeUsingNIO’ method to put these strings “Soccer” “Tennis” “Badminton” “Hockey” in the
‘sampleFile.txt’ file using the ‘.write()’ and ‘.newLine()’ methods from the ‘BufferedWriter’ object. Since Java7, we are required to
close the ‘BufferedWriter’ and ‘BufferedReader’ objects, manually. You can see this in the ‘catch()’ and ‘finally’ parts of exception
handling in each method.
2. Run this code and ensure the program outputs the four strings from #1 above.
TYPE CODE WHERE IT IS BOLDED BELOW
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class TestFileWriteRead {
public static void main(String[] args) {
try {
Path path = Paths.get("sampleFile.txt");
writeUsingNIO(path);
readUsingNIO(path);
}
catch (Exception e) {
e.printStackTrace();
}
}
private static void writeUsingNIO(Path Xpath) throws IOException {
BufferedWriter bufferedWriter = Files.newBufferedWriter(Xpath);
try {
System.out.println("Writing contents to file.");
//Type your code here
}
catch (IOException ioe) {
bufferedWriter.close();
ioe.printStackTrace();
}
finally {
bufferedWriter.close();
}
}
private static void readUsingNIO(Path Xpath) throws IOException {
BufferedReader bufferedReader = Files.newBufferedReader(Xpath);
try {
String line;
System.out.println("Reading file"+ Xpath.getFileName().toString());
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
catch (IOException ioe) {
bufferedReader.close();
ioe.printStackTrace();
}
finally {
bufferedReader.close();
}
}
}
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 3 images