Activity 2: Read a file using FileReader() (20 mins) // • Read lines of a file n
ID: 3827672 • Letter: A
Question
Activity 2: Read a file using FileReader() (20 mins)
// • Read lines of a file name joke in Data folder
// • For every line of the file:
// o Print out the line to the console
// o Call textToSpeech(<line>,voice) where line is a String
// • Answer question 1, Activity 2
// • Demonstrate your program to your TA and get a signature
// Hint:
// • You will need buffering to read line by line, you can either use Scanner
// class or BufferedReader to wrap around a FileReader object.
// • BufferedReader has a convenience method named readLine() that makes it
// easier to read text from input streams line by line.
//TODO 1: CHECK if "Data/joke" is exist and readable, writable
//TODO 2: Read the file,
//Print the content of the file to console
//call textToSpeech(sentence,voice) to convert to speech line by line
Explanation / Answer
Hi, Please find my implementation.
Please let me know in case of any issue.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class FileReaderDemo {
public static void textToSpeech(String sentence, String voice){
//
}
public static void main(String[] args) throws IOException {
File file = new File("Data/joke");
if(file.exists() && file.canRead()){
System.out.println("Data/joke exist and redable");
}else if(file.exists()){
System.out.println("Data/joke exist but not redable");
}else{
System.out.println("Data/joke does not exist");
}
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line;
while((line = br.readLine()) != null){
// printing on console
System.out.println(line);
}
br.close();
fr.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.