I started from the first solution, where the getFileContents function got the whole logic of reading content from file. But of course something was wrong. So as in one of the problem solution somebody sugests I move the full logic of the getFileContent into the run method. But after this the code is broken down and show nothing. As I see the run method does.nt start. But why?
package pl.codegym.task.task16.task1630;
import java.io.*;
public class Solution {
public static String firstFileName;
public static String secondFileName;
static {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
try {
firstFileName = bufferedReader.readLine();
secondFileName = bufferedReader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
//tutaj wpisz swój kod
public static void main(String[] args) throws InterruptedException, IOException {
systemOutPrintln(firstFileName);
systemOutPrintln(secondFileName);
}
public static void systemOutPrintln(String fileName) throws InterruptedException, IOException {
ReadFileInterface f = new ReadFileThread();
f.setFileName(fileName);
f.start();
f.join();
//tutaj wpisz swój kod
System.out.println(f.getFileContents());
}
public interface ReadFileInterface {
void setFileName(String fullFileName);
String getFileContents() throws IOException;
void join() throws InterruptedException;
void start();
}
public static class ReadFileThread extends Thread implements ReadFileInterface{
public String fileName;
public String filecontent ="";
public void run(){
try {
String filecontentTemp ="";
FileReader fileReader =new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while (bufferedReader.ready()){
String line = bufferedReader.readLine()+" ";
filecontentTemp+= line;
}
System.out.println(filecontentTemp);
filecontent = filecontentTemp;
bufferedReader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public void setFileName(String fullFileName) {
fileName = fullFileName;
}
@Override
public String getFileContents() throws IOException {
try {
String filecontentTemp ="";
FileReader fileReader =new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while (bufferedReader.ready()){
String line = bufferedReader.readLine()+" ";
filecontentTemp+= line;
}
System.out.println(filecontentTemp);
filecontent = filecontentTemp;
bufferedReader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
return filecontent;
}
@Override
public void start() {
}
}
//tutaj wpisz swój kod
}