1) // ReadFileInterface f = new ReadFileThread();// why do we have ReadFileInterface instead of ReadFileThread?
ReadFileThread f = new ReadFileThread();//this makes more sense to me to use the reference datatype as ReadFileTRead instead of the interface.
2) This gives the same output as the suggested solution so why do I fail he test?
@Override
public void run() {
Scanner scanner = new Scanner((filename));
while (scanner.hasNextLine()) {
content += scanner.nextLine() + " ";
}
scanner.close();
}
3) I use a bufferedreader to read the names of the files. from the console. Why does the scanner read them again and add them to the content of the file? How to prevent his?
static {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try {
firstFileName = reader.readLine();
secondFileName = reader.readLine();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
3 questions about this task
Under discussion
Comments (8)
- Popular
- New
- Old
You must be signed in to leave a comment
John
26 July 2021, 02:33
excuse me everyone, why you didn`t override the getFileContents here?
0
Vincenzo Seggio
26 December 2020, 22:27
It is easier to help if you post the whole code.
0
Liliane Top Backend Developer at Procura
27 December 2020, 08:54
0
Liliane Top Backend Developer at Procura
27 December 2020, 08:58
Question 2 I resolved. I forgot to create a File or Filereader as a Scanner can't read the String from filename.
But question 1 I still haven't figured that out.
Question 3 really bugs me... How to stop the program, its mind boggling....
0
Vincenzo Seggio
27 December 2020, 11:44
Question 1: There is a difference. If you use ReadFileInterface f = new ReadFileThread(); the f.join()-method belongs to the interface join()-method. If you use ReadFileThread f = new ReadFileThread(); the f.join()-method belongs to the Thread - Superclass.
Question 3: I used BufferedReader and FileReader in the run-method. Perhaps if you write
it solves your problem. 0
Nouser
27 December 2020, 13:21
I'd answer question 1 with a simple 'cause you should see that this is possible and get used to it'.
And the difference Vincenzo tries to describe lies somewhere else. The join method is for both possibilities the same, the overridden join method of the ReadFileThread class. That's cause the object itself is always the same, it's a ReadFileThread. Just the type of the reference variable changes. If the reference variable is of the type ReadFileInterface you just can chose from the methods this class offers.
Here ReadFileThread has to implement all methods of ReadFileInterface. join and start are already implemented in the parent class Thread.
And if the code would use eg. the join method of the interface directly, how should that work? It has no method body, no implementation. That's why you have to override these interface methods.
Question 3... I do not really get what you're asking. If you give a Scanner constructor a string, then it is reading from that string (compare to the constructor description in Oracles docs). So when you call nextLine it reads the first line from the string you passed to the constructor.
FileInputStream or FileReader on the other hand try to get a path to a file out of a string that you pass to the constructor.
0
Liliane Top Backend Developer at Procura
27 December 2020, 14:12
First thank you both for taking the time to educate me ;-)!
question 1) If I understand correctly is that the datatype of the reference to the object wil have access to different methods. In this case either from the ReadFileThread class or from the ReadFileInterface. And the ReadFileThread implements the ReadFileInterface and has to implements its methods so it makes no difference. (Is there an advantage of using the ReadFileInterface datatype for reference?)
question 3) For the filenames (2 strings) I use a BufferedReader and to read the content of the files I used a Scanner. However the Scanner also picks up the filenames and the program doesn't stop running.... Eventhough I use scanner.close. I think there is a misunderstanding in my thinking somewhere.....
0
Nouser
27 December 2020, 14:28
to 1. yes and the advantages are not visible yet. But imagine a lot of classes that all implement one interface SampleInterface. Then you could do eg. smth. like List<SampleInterface> or someMethod(SampleInterface si) and you know that all of them have a set of methods with the same header.
to 3. I've tried that code (thought that's your problem ;))
I input eg.
c:\Temp\out.txt
c:\Temp\out2.txt
(that's read by the BufferedReader)
then these strings are the source from where the scanner will read (not the files). So the scanner reads one line and that's c:\Temp\out.txt, then it prints that string. No next line, the thread stops. Same for the next line. So the output is in my case
c:\Temp\out.txt
c:\Temp\out2.txt
The program stops as expected
0