In my below code it reads .txt files from a folder (say the folder has 2000+ text files) and displays the total number of words present in a text document.
If I read 10-30 text files only from the directory the output is displaying correctly in an order for each text files.
But when I add 2000+ text files and read at once from that folder the output arrangement is collapsed.(it displays in random order).
can anyone suggest me to solve this?
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.io.FileUtils;
public class duplicatestrings
{
public static void main(String[] args)
{
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(".txt");
}
};
File folder = new File("E:\\testfolder");
File[] listOfFiles = folder.listFiles(filter);
for (int i = 0; i < listOfFiles.length; i++) {
File file1 = listOfFiles[i];
try {
String content = FileUtils.readFileToString(file1);
// System.out.println("asssdffsssssssssss = " + content);
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader ins = null;
try {
ins = new BufferedReader (
new InputStreamReader(
new FileInputStream(file1)));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String line = "", str = "";
int a = 0;
int b = 0;
try {
while ((line = ins.readLine()) != null) {
str += line + " ";
b++;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// System.out.println("Total number of lines " +b);
//System.out.println(str);
/* int count =0;
try {
String input = ins.readLine();
String[] array = input.split(" ");
System.out.print("\nPlease enter word to be counted :");
String key = ins.readLine();
for(int s=0;i < array.length;i++){
if(array[s].equals(key))
count++;
}
System.out.print("\n The given word occured " + count + " times");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
StringTokenizer st = new StringTokenizer(str);
while (st.hasMoreTokens()) {
String s = st.nextToken();
a++;
}
// List<String> list = Arrays.asList(str.split(" "));
// Set<String> uniqueWords = new HashSet<String>(list);
// for (String word : uniqueWords) {
// System.out.println(word + a+ "\n" + Collections.frequency(list, word));}
System.out.println(" Total no of words=" + a );
}
}
}
And I have to get distinct and repeated word "no of counts(only)" from all text files/folder(directory).
suggestions welcomed.