Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

T WO FILES NEEDED TO BE READ ! data1.txt contains the following three lines . An

ID: 3705521 • Letter: T

Question

TWO FILES NEEDED TO BE READ !

data1.txt contains the following three lines.

And data2 .txt

Use Callables ?

Use Thread pools ?

Use Futures ?

Design and develop a program that accepts the name of a directory that contains a set of text-only files. The program must be executable as below. ?java PA2 <input-folder-name> <output-folder-name>

?The program ensures the following: ?

a) there are two parameters to the main method ?

b) <input-folder-name> is a valid, readable directory ?

c) the directory <output-folder-name> does not exist. ?

If there is a violation of these requirements, the program prints an error message and stops.

The program must read the files in <input-folder-name> in parallel using multiple threads and create an index of every token (a sequence of characters delineated by white space) in the input files: the token, the file name in which it occurs and the line number or offset in the file must all be output to a file in <output-folder-name>. All file names and line numbers associated with a token must be output before outputting the next token and its related information.

Suppose the files data1 and data2 are files in the folder C:ClassesICS 440PA2 Mapdata. The file data1 contains the following three lines.

one two

three four

four

And data2 contains the following three lines.

one two three

two three four five six seven

eight

An output would be as follows. (The order of the tokens is unimportant.)

six: C:ClassesICS 440PA2 Mapdata ile2.txt:2

1

four: C:ClassesICS 440PA2 Mapdata ile1.txt:2 C:ClassesICS 440PA2

Mapdata ile1.txt:3 C:ClassesICS 440PA2 Mapdata ile2.txt:2

one: C:ClassesICS 440PA2 Mapdata ile1.txt:1 C:ClassesICS 440PA2

Mapdata ile2.txt:1

seven: C:ClassesICS 440PA2 Mapdata ile2.txt:2

two: C:ClassesICS 440PA2 Mapdata ile1.txt:1 C:ClassesICS 440PA2

Mapdata ile2.txt:1 C:ClassesICS 440PA2 Mapdata ile2.txt:2

three: C:ClassesICS 440PA2 Mapdata ile1.txt:2 C:ClassesICS 440PA2

Mapdata ile2.txt:1 C:ClassesICS 440PA2 Mapdata ile2.txt:2

five: C:ClassesICS 440PA2 Mapdata ile2.txt:2

eight: C:ClassesICS 440PA2 Mapdata ile2.txt:3

It is fine to output just the file name relative to the folder as opposed to the absolute path name.

Use a combination of thread pools, Callables, and Futures. All files must be processed in parallel to update a concurrent map implementation from java.util.

Explanation / Answer

CODING:

--------------

// Define a necessary packages

import java.io.File;

import java.io.IOException;

import java.nio.file.Files;

import java.nio.file.Paths;

import java.util.ArrayList;

import java.util.List;

import java.util.Map;

import java.util.concurrent.Callable;

import java.util.concurrent.ConcurrentHashMap;

import java.util.concurrent.ExecutionException;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.Future;

import java.util.concurrent.atomic.AtomicInteger;

// Implement a class

public class Token implements Callable<Map<String, String>> {

// create a map

final Map<String, String> InformToken;

// declare string variable

private final String[] FileLocation;

AtomicInteger counter=new AtomicInteger(0);

public Token(String[] Stringlocation)

{

// assign the string

this.FileLocation=Stringlocation;

this.InformToken=new ConcurrentHashMap<String, String>();

}

public Map<String, String> GetLocation(String FPath)

{

try {

// create a list

List<String> Ls = Files.readAllLines(Paths.get(FPath));

int num=1;

// use loop

for (String Line : Ls) {

// assign method to split

String[] strg=Line.split(" ");

for (String string : strg) {

String find=InformToken.putIfAbsent(string, FPath+":"+num);

if(find==null){

}

else{

// assigning

String Location=find;

Location+=" "+FPath+":"+num;

InformToken.replace(string, Location);

}

}

num++;

}

} catch (IOException e) {

e.printStackTrace();

}

return InformToken;

}

@Override

// implement a method

public Map<String, String> call() throws Exception

{

// assign and increment

int ct=counter.getAndIncrement();

return GetLocation(FileLocation[ct]);

}

// main function

public static void main(String[] args)

{

// declare the variable

int num_Threads=args.length;

if (num_Threads==0)

{

System.out.println("Sorry no File is found...Please input");

return;

}

for (int i = 0; i < args.length; i++) {

// read a file

File Fi = new File(args[i]);

if (!Fi.exists()) {

System.out.println(args[i] + " Not Exist.");

return;

}

// checks

if (!(Fi.isFile() && Fi.canRead())) {

// display

System.out.println(Fi.getName() + " It Cannot be Read from.");

return;

}

}

ExecutorService Exe = Executors.newFixedThreadPool(num_Threads);

// creating list string

List<Future<Map<String, String>>> Lst = new ArrayList<Future<Map<String, String>>>();

Callable<Map<String, String>> call = new Token(args);

for(int i=0; i< num_Threads; i++){

Future<Map<String, String>> Ft = Exe.submit(call);

Lst.add(Ft);

}

Map<String, String> tks=new ConcurrentHashMap<String, String>();

for(Future<Map<String, String>> fut : Lst){

try {

tks=fut.get();

} catch (InterruptedException | ExecutionException e) {

e.printStackTrace();

}

}

Exe.shutdown();

// using loop to get map

for (Map.Entry<String, String> token : tks.entrySet()) {

System.out.println("Token Name-"+token.getKey()+" Path-"+token.getValue());

}

}

}