Could you explain what the following recommendation from mentor means: "Be sure that the getFileContents method returns an empty string if the run method has not been started."?
What should I change in my code? I've tried this, but it doesn't work:
public String getFileContents(){
if (!isAlive()){
return "";
} else{
return fileContents;
}
}
package com.codegym.task.task16.task1630;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Solution {
public static String firstFileName;
public static String secondFileName;
static {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try{
firstFileName = br.readLine();
secondFileName = br.readLine();
br.close();
} catch (IOException e){
e.getMessage();
}
}
public static void main(String[] args) throws InterruptedException {
systemOutPrintln(firstFileName);
systemOutPrintln(secondFileName);
}
public static void systemOutPrintln(String fileName) throws InterruptedException {
ReadFileInterface f = new ReadFileThread();
f.setFileName(fileName);
f.start();
f.join();
System.out.println(f.getFileContents());
}
public interface ReadFileInterface {
void setFileName(String fullFileName);
String getFileContents();
void join() throws InterruptedException;
void start();
}
public static class ReadFileThread extends Thread implements ReadFileInterface{
private String fileName;
private String fileContents;
public void setFileName(String fullFileName) {
this.fileName = fullFileName;
}
public String getFileContents(){
return fileContents;
}
public void run(){
try {
BufferedReader br = new BufferedReader(new FileReader(this.fileName));
while (br.ready()) {
String line = br.readLine();
fileContents += line + " ";
}
br.close();
} catch (IOException e){
e.getMessage();
}
}
}
}