I've tried a couple different versions.. some I built myself.. others with help from other questions/solutions here... I've left various versions in commented out.
I cannot for the life of me get the last condition to validate correctly. I suspect that the " " white space is being added to the end of the string which is causing it not to pass, yet when I changed the order in the run() method to add the white space prior to the read line it still doesn't validate.
I'm lost and my brain hurts.
package com.codegym.task.task16.task1630;
import java.io.*;
import java.util.Scanner;
public class Solution {
public static String firstFileName;
public static String secondFileName;
//write your code here
static {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
firstFileName = reader.readLine();
secondFileName = reader.readLine();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws InterruptedException, FileNotFoundException {
systemOutPrintln(firstFileName);
systemOutPrintln(secondFileName);
}
public static void systemOutPrintln(String fileName) throws InterruptedException, FileNotFoundException {
ReadFileInterface f = new ReadFileThread();
f.setFileName(fileName);
f.start();
//write your code here
f.join();
System.out.println(f.getFileContents());
}
public interface ReadFileInterface {
void setFileName(String fullFileName) throws FileNotFoundException;
String getFileContents();
void join() throws InterruptedException;
void start();
}
public static class ReadFileThread extends Thread implements ReadFileInterface {
String fullFileName;
String fileContents = "";
@Override
public void setFileName(String fullFileName) throws FileNotFoundException {
this.fullFileName = fullFileName;
}
@Override
public String getFileContents() {
return fileContents;
}
@Override
public void start() {
}
public void run() {
try {
/*
FileInputStream fileInputStream = new FileInputStream(fullFileName);
byte[] temp = new byte[fileInputStream.available()];
fileInputStream.read(temp);
fileContents = new String(temp).replaceAll("\r","").replaceAll("\n"," ");
*/
/*
BufferedReader reader = new BufferedReader(new FileReader(fullFileName));
String read;
fileContents = reader.readLine();
while ((read = reader.readLine()) != null) {
fileContents += " " + read;
*/
File myFile = new File(fullFileName);
StringBuilder builder = new StringBuilder();
Scanner myReader = new Scanner(myFile);
while (myReader.hasNextLine()) {
builder.append(myReader.nextLine());
if (myReader.hasNextLine()) builder.append(" ");
}
myReader.close();
fileContents = builder.toString();
} catch (Exception e) {
}
}
}
}