I need to use thread to run a SlowFileStringifier.display()
method. I have created a Proxy class to run public void display(PrintWriter out)
method present in SlowFileStringifier
class.
The program reads files present inside data folder and displays the output.
The proxy display(PrintWriter out)
should output some text right away, then it should start a thread to run the SlowFileStringifier.display()
method.
Below is the output I want:
Reading data\anaisnin.txt
Reading data\cree.txt
Reading data\maewest.txt
Reading data\marktwain.txt
Reading data\rumi.txt
"Whenever you find yourself on the side of the majority, it is time to pause and reflect."
- Mark Twain
"You only live once, but if you do it right, once is enough."- Mae West
"If you are irritated by every rub, how will your mirror be polished?"
- Jalaluddin Mevlana Rumi
"There came a time when the risk to remain tight in the bud was more painful than the risk it took to blossom."
- Anais Nin
Below are my classes:
import java.io.PrintWriter;
public class FileStringifierProxy implements FileStringifier{
private final String fileName;
public FileStringifierProxy(String filename) {
this.fileName = filename;
}
@Override
public void display(PrintWriter out) {
System.out.println("Reading "+fileName);
SlowFileStringifier slowfilestringifier = new SlowFileStringifier(fileName);
slowfilestringifier.display(out);
}
@Override
public String stringify() {
// TODO Auto-generated method stub
return null;
}
}
import java.io.*;
import java.util.ArrayList;
/**
* Simple file reader that turns a file into a string
*/
public class SlowFileStringifier implements FileStringifier {
/**
* Create a file stringifier with a file name
*
* @param filename
* file name of file
*/
public SlowFileStringifier(String filename) {
this.fileName = filename;
}
/**
* Writes out the file contents to the print writer
*
* @param out
* the output print writer
*/
public void display(PrintWriter out) {
out.println(this.stringify());
out.flush();
}
/**
* Returns a string with all the files contents
*/
public String stringify() {
FileReader f;
try {
f = new FileReader(this.fileName);
BufferedReader bf = new BufferedReader(f);
ArrayList<String> lines = new ArrayList<String>();
String line = null;
while ((line = bf.readLine()) != null) {
Thread.sleep(2000);
lines.add(line);
}
bf.close();
return String.join("\n", lines);
} catch (FileNotFoundException e) {
e.printStackTrace();
System.err.println("File " + this.fileName + " was not found.");
} catch (IOException e) {
e.printStackTrace();
System.err.println("Problem reading file " + this.fileName + ".");
} catch (InterruptedException e) {
e.printStackTrace();
System.err.println("File reading interrupted: " + this.fileName);
}
return "";
}
private final String fileName;
}
public class ShowFileContents {
/**
* Accepts a file directory and then prints out the contents of all the
* files in that directory
*
* @param args
* single arg with directory path
*/
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Usage: java ShowFileContents <directory>");
}
String dirname = args[0];
File dir = new File(dirname);
if (!dir.exists()) {
System.err.println(dirname + " does not exist");
return;
}
if (!dir.isDirectory()) {
System.err.println(dirname + " is not a directory");
return;
}
PrintWriter outWriter = new PrintWriter(System.out);
for (File file : dir.listFiles()) {
if (file.isDirectory())
continue;
FileStringifier fd = new FileStringifierProxy(file.getPath());
fd.display(outWriter);
}
}
}
Following is the output I want:
Aucun commentaire:
Enregistrer un commentaire