Guys is there anyone who can tell me where my mistake is? I am getting requested output but solution doesn't verify. As you can see through code I have tried all kind of options and still I am getting this :
Be sure that the getFileContents method returns an empty string if the run method has not been started.
And I am getting empty String! Can someone help me? I am really getting crazy about this task.
package com.codegym.task.task16.task1630;
import java.io.*;
/*
Sequential file output
1. Figure out what the program does.
2. In a static block, read 2 filenames: firstFileName and secondFileName.
3. Inside the Solution class, create a public static ReadFileThread class that implements the
ReadFileInterface interface (Think about what is more appropriate: Thread or Runnable).
3.1. The setFileName method must set the name of the file whose contents will be read.
3.2. The getFileContents method must return the contents of the file.
3.3. In the run method, read the contents of the file and close the stream. Separate the lines of the file with spaces.
4. Think about where you need to wait for the thread to finish to ensure that the files are displayed sequentially.
4.1. To do this, add a call to the appropriate method.
Expected output:
[entire contents of the first file]
[entire contents of the second file]
Requirements:
1. 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.
2. In the Solution class, declare the public static ReadFileThread class.
3. The ReadFileThread class must implement the ReadFileInterface interface.
4. The ReadFileThread class must inherit the appropriate class.
5. 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.
6. The systemOutPrintln method must call the join method on the created object f.
7. The program's output should consist of 2 lines. Each line contains the contents of one file.
File path 1: /Users/stefanmaric/testFile.txt
File path 2: /Users/stefanmaric/testFile2.txt
*/
public class Solution {
public static String firstFileName;
public static String secondFileName;
static {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try {
firstFileName = reader.readLine();
secondFileName = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
}
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.setFullFileName(fileName);
f.start();
f.join();
System.out.println(f.getFileContents());
// ReadFileThread l = new ReadFileThread();
// System.out.println(l.getFileContents());
}
public interface ReadFileInterface {
void setFullFileName(String fullFileName);
String getFileContents();
void join() throws InterruptedException;
void start();
}
public static class ReadFileThread extends Thread implements ReadFileInterface {
// private static final String STAT = "";
private String fullFileName;
private String fileContents = "";
// private boolean isDownloaded = false;
@Override
public void setFullFileName(String fullFileName) {
this.fullFileName = fullFileName;
}
@Override
public String getFileContents() {
// if (isDownloaded) {
return fileContents;
// } else
// return "";
}
@Override
public void run() {
try {
BufferedReader reader = new BufferedReader(new FileReader(fullFileName));
String read;
while ((read = reader.readLine()) != null) {
fileContents += read + " ";
}
// isDownloaded = true;
reader.close();
} catch (Exception e) {
}
}
}
}