JAVA: create a program that reads all text files in a directory, calculates the occurrences of words, and saves the result in one single output text file.
assume that you are in a directory called Desktop/programdirectory, where you have multiple text files.
textfile1.txt : "how are you"
textfile2.txt: "great thank you"
textfile3.txt: "great to see you. see you later"
Then, your output.txt should have:
how: 1
are: 1
you: 4
great: 2
thank: 1
to: 1
see: 2
later: 1
ANSWER:
import java.io.*;
import java.util.*;
public class Files2file
{
public static void main(String args[]) throws FileNotFoundException
//throws if any error occur while opening files
{
String content="";
File folder = new File("/home/ec2-user/java");//give the path of
directory
/*This loop gets the list of files from the given folder and reads
text using Scanner
stores in content variable then replaces with given string then
closes the
opened file */
for (File file : folder.listFiles()) {
Scanner s = new Scanner(file);
while(s.hasNextLine())
{
content=content+s.nextLine();
content=content.replace(".","");
content=content.replace("\n"," ");
}
content=content+" ";
s.close();
}
String words[]=content.split(" "); //splits text from files using "
"
/*this loop takes the words from the content gives the conunt of
each word*/
int wc=1;
for(int i=0;i<words.length;i++)
{
for(int j=i+1;j<words.length;j++)
{
if(words[i].equals(words[j]))
{
wc=wc+1;
words[j]="0";
}
}
if(words[i]!="0")
System.out.println(words[i]+":"+wc);
wc=1;
}
}
}
//if any syntactical errors with the code please verify the blelow pic

OUTPUT:

JAVA: create a program that reads all text files in a directory, calculates the occurrences of...
Reading and Writing Files in a Directory Create a program that provides a listing of all the files in a directory. The program should be a Java application that accepts a single argument into the args array of the main() method. This argument identifies the name of a directory. The application should make sure that this filename really does identify a directory and then list all of the files in the directory. For each file, list whether the file is...
Create a Mad Libs program that reads in text files and lets the user add their own text anywhere the word ADJECTIVE, NOUN, ADVERB, or VERB appears in the text file. For example, a text file may look like this: The ADJECTIVE panda walked to the NOUN and then VERB. A nearby NOUN was unaffected by these events. The program would find these occurrences and prompt the user to replace them. Enter an adjective: silly Enter a noun: chandelier Enter...
C++.Write a program that opens two text files and reads their contents into two separate queues. The program should then determine whether the files are identical by comparing the characters in the queues. When two nonidentical characters are encountered, the program should display a message indicating that the files are not the same. If both queues contain the same set of characters, a message should be displayed indicating that the files are identical. Sample Run Suppose we have two files...
a. Provide me with your code file, output file and the text file. 1. Create a file with a series of integers. Save it as numbers. txt. Write a program that reads all the numbers and calculates their sum . 2. Create a file having different integers than the first one. Save it as numbers1. txt . Write a program that reads all the number and calculates their average. Important: The two files that you are creating will contain different...
Write a Java program that reads words from a text file and displays all the non-duplicate words in ascending order. The text file is passed as a command-line argument. Command line argument: test2001.txt Correct output: Words in ascending order... 1mango Salami apple banana boat zebra
IN PYTHON Write a program that reads two text files, take one sentence from each text file and print them one after another. Example: If line1 is from text1.txt and line2 is from text2.txt, then print line1 line2 Repeat this for all the lines of the two files
JAVA
Write a program that
1. opens a text file
2. reads lines of text from the file
3. Converts each line to uppercase
4. saves them in an ArrayList
5. Writes the list of Strings to a new file named
"CAPSTEXT.txt"
Mainjavo ext.txt Instructions from your teacher 1 My mistress' eyes are nothing like the sun; 2 Coral is far more red than her lips' red; 3 If snow be white, why then her breasts are dun; 4 If...
Write a Java program that lists all the files in a directory and their subdirectories, that mimics the Unix ls command or the Windows dir command. Note that when a directory is encountered we do not immediately print its contents recursively. Rather, as we scan each directory, place any subdirectory in a List. After the directory entries are printed then process each subdirectory recursively. For each file that is listed, include the modification time, the file size, and if it...
Q22 Consider the following 2 text files and Java program. 8 Points Answer the following questions keeping in mind that: • All three files are in the same directory, and there are no other files. • The program may crash. If the program crashes, answer the question with: "The program crashes" standings1.txt: 1 Valtteri Bottas 25 2 Charles Leclerc 18 3 Lando Norris 16 4 Lewis Hamilton 12 standings2.txt: 1 Valtteri Bottas 43 2 Lewis Hamilton 37 3 Lando Norris...
In Python Provide me with your code file, output file and the text file Create a file having different integers than the first one. Save it as numbers1. txt . Write a program that reads all the number and calculates their average. Important: The two files that you are creating will contain different numbers.