Hi,
I deleted last request because I didn't knew how to edit it.
AzjaLevel 30 , Krakow
36 minutes ago
Your code is working fine and "end" variable is updated but it is recommended to use "StringBuilder class" for string concatenation and it should use "getFileContents method" in "run method" because now this code doesn't use created thread. Also you don't have to override "start method" because it's already overridden from inheriting "Thread class".
P.S: There is useful method with working with String when you have additional spaces at the end of the line called "trim". It just remove additional spaces from the end and from the beginning. There is how it works
https://www.tutorialspoint.com/java/java_string_trim.htm
Reply
gmb55Level 18 , Krakow
3 minutes ago
Ok, but how about now? When I put it in run() to get Threads it dosen't work.
"C:\Program Files\Java\jre\bin\java.exe" -Dfile.encoding=windows-1250 -jar D:\Programowanie\Aplikacje\test_java\out\artifacts\test_java_jar\test_java.jar
null
null
Process finished with exit code 0
There is null, because this doesn't update end variable from while loop in run() method. And I don't know why :/ package com.codegym.task.task16.task1630;
import java.io.*;
public class Solution {
public static String firstFileName; // = "C:\\Users\\micha\\Documents\\1.txt";
public static String secondFileName; // = "C:\\Users\\micha\\Documents\\2.txt";
//write your code here
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, 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();
//write your code here
System.out.println(f.getFileContents());
}
public interface ReadFileInterface {
void setFileName(String fullFileName);
String getFileContents() throws IOException;
void join() throws InterruptedException;
void start();
}
//write your code here
public static class ReadFileThread extends Thread implements ReadFileInterface {
String fullFileName;
private String end;
@Override
public void setFileName(String fullFileName) {
this.fullFileName = fullFileName;
}
@Override
public void start() {
}
@Override
public void run() {
try {
FileReader file = new FileReader(fullFileName);
BufferedReader reader = new BufferedReader(file);
while (reader.ready()) {
String print = reader.readLine();
end += print + " ";
}
reader.close();
} catch (Exception e) {
}
}
@Override
public String getFileContents() throws IOException {
return end;
}
}
}