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(); } } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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();
}
}
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education