Hi there,
All i want to do is read the jokes from a text file (using Scanner) and then write them out to three different files with three different extensions (fileName1.obj, fileName2.dat, fileName3.txt), that's part A.
Part B,
Create a class called FileWatcher.
• This class can be given several filenames that may or may not exist corresponding to .dat, .txt and .obj files.
• Include all the filenames which corresponds to the associated file type from part A above.
• For example FileWatcherObj should include all filenames created with the suffix .obj from part A.
A thread of execution should be started for each filename.
Each thread will periodically check for the existence of its file. Enter in “exit” when all the file names you are watching have been entered. Be sure to enter a file that does not exist.
If the file appears, the thread will write a message indicating the file that was found to the console.
• Then, it will randomly display a joke in its separate JFrame thread properly titled for the classification.
• Each joke should display for two second for every 5 words in the joke. Jokes should be only less than 3 sentences.
Also create another thread that checks to see if all the jokes are displayed at least twice.
When all have been displayed, terminate the execution.
Hint: Create static ThreadGroup object in a class (ReadFile) so you can refer to the thread group object with the class name for all the different treads.
static ThreadGroup fileWatchers=new ThreadGroup("File Watchers");
Put all the FileWatcher threads in a thread group so you can terminate the group of threads as a whole by calling their interrupt() method that is a condition in the run() methods loop.
ReadFile.fileWatchers.interrupt();
Help me please
import java.util.*;
import java.io.*;
public class Joking{
public static void main(String[] args) throws IOException{
File jokesDat = new File ("jokesDat.dat");
File jokesChar = new File ("jokesChar.txt");
File jokesObj = new File ("jokesObj.obj");
int i = 0;
String[] joke = new String[100];
Scanner scanner = new Scanner(new File("jokes.txt"));
try{
while (scanner.hasNext()){
joke [i] = scanner.nextLine();
i++;
//System.out.println("Joke #" + i + " is " + joke[i]);
}
}catch(Exception e){
System.out.println("Reading failed");
System.exit(0);
}
DataOutputStream outDat = null;
DataOutputStream outTxt = null;
DataOutputStream outObj = null;
try{
jokesDat.createNewFile();
jokesChar.createNewFile();
jokesObj.createNewFile();
outDat = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(jokesDat)));
outTxt = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(jokesChar)));
outObj = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(jokesObj)));
}catch(Exception e){
System.out.println("Creation failed");
System.exit(0);
}
try{
for (i = 0; i < joke.length; i++){
outDat.writeUTF(joke[i]);
outTxt.writeUTF(joke[i]);
outObj.writeUTF(joke[i]);
}
}catch(Exception e){
System.out.println("Writing failed");
}
finally{
outDat.close();
outTxt.close();
outObj.close();
}
}
}