There's several layers to this question:
1) This sounds ignorant to ask at this point, but how in the heck does one use the console interface in IntelliJ? That would seem useful here. (As opposed to CodeGym where it's just "there".)
2) Since I can't play with any file names on the console interface, it makes it difficult for me to check my work. Any insights wiser minds than my own can contribute here would be greatly appreciated! : )
package com.codegym.task.task16.task1630;
import java.io.*;
public class Solution {
public static String firstFileName;
public static String secondFileName;
//write your code here
//The Solution class's static block should read from the console the names of two files and store them in the variables firstFileName and secondFileName.
static{
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
firstFileName = reader.readLine();
secondFileName = reader.readLine();
}catch(IOException e){
}
}
public static void main(String[] args) throws InterruptedException {
//The program's output should consist of 2 lines. Each line contains the contents of one file.
systemOutPrintln(firstFileName);
systemOutPrintln(secondFileName);
}
public static void systemOutPrintln(String fileName) throws InterruptedException {
//The systemOutPrintln method must call the join method on the created object f.
ReadFileInterface f = new ReadFileThread();
f.setFileName(fileName);
f.start();
//write your code here
System.out.println(f.getFileContents());
f.join();
}
public interface ReadFileInterface {
void setFileName(String fullFileName);
String getFileContents();
void join() throws InterruptedException;
void start();
}
//write your code here
//In the Solution class, declare the public static ReadFileThread class.
//The ReadFileThread class must implement the ReadFileInterface interface.
//The ReadFileThread class must inherit the appropriate class.
public static class ReadFileThread extends Thread implements ReadFileInterface {
String fullFileName;
StringBuffer fileContents = new StringBuffer("");
//The ReadFileThread class's run method should read lines from the file set by the setFileName method. And the getFileContents method of this same class must return the file contents. The return value is one string consisting of the lines of the file, separated by spaces.
@Override
public void run(){
//The run() method does the work of getting the data from the file and constructing the string line by line. The getFileContents just returns the string or an empty string if it's null.
FileInputStream fileReader = null;
String fileLineReader = "";
try {
fileReader = new FileInputStream(fullFileName);
}catch(FileNotFoundException e){
}
BufferedReader fileBufferedReader = new BufferedReader(new InputStreamReader(fileReader));
try {
while((fileLineReader = fileBufferedReader.readLine()) != null){
fileContents.append(fileLineReader + " ");
}
}catch(IOException e){
}
try {
fileReader.close();
fileBufferedReader.close();
}catch(IOException e){
}
}
@Override
public void setFileName(String fullFileName) {
//Not sure about this, but I'm not sure what else would be going on here.
this.fullFileName = fullFileName;
}
@Override
public String getFileContents() {
return fileContents.toString();
}
/*@Override
//I'm not sure about the join(). It won't let me write new implementation for it if I inherit the Thread class since it's
//final in the Thread class. But I'm confused why it's declared in the ReadFileInterface then.
public void join() throws InterruptedException {
}*/
}
}