I am really stuck on this one. The code just displays the contents of one file. I tried different ways of reading the contents but it doesn't make any difference. I didn't find anything helpful in the help section so far...
Any hints?
public class Solution {
public static String firstFileName;
public static String secondFileName;
public static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static{
try {
firstFileName = reader.readLine();
secondFileName = reader.readLine();
reader.close();
}
catch(IOException e) {
System.out.println("Inside Solution.static: Enter two valid lines");
}
}
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 StringBuffer fileContents = new StringBuffer();
private String fileName = "";
@Override
public void setFileName(String fullFileName) {
this.fileName = fullFileName;
}
@Override
public String getFileContents() {
return this.fileContents.toString().trim();
}
@Override
public void run() {
try {
FileReader fileReader = new FileReader(fileName);
BufferedReader reader1 = new BufferedReader(fileReader);
while ((reader1.readLine() != null)) {
fileContents.append(reader1.readLine() + " ");
}
}
catch (IOException e) {
System.out.println("Inside ReadFileThread.run(): IOException");
}
}
}
}
Thanks in advance!