Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialFumbi Fashanu
34 PointsReading and adding multiple files to an array in java
I have a java project where i'm meant to read 1000 text files and print the word frequency for each of them. I want to add the text files to an array so i can use threads to read the files since each file is not meant to be read more than once but i'm having problems adding them to the array. "file" is a subfolder containing text files
import java.io.; import java.util.; public class WordFrequency {
public static void main(String[] args) throws IOException{
FileInputStream file = new FileInputStream("files");
Scanner fileinput = new Scanner(file);
//create array list to hold text files
ArrayList<Object> txtfiles = new ArrayList<Object>();
//read through and find text files
while(fileinput.hasNext()){
//get the next file
Object nextfile = fileinput.next();
//Determine if file is in the array list
if (txtfiles.contains(nextfile)) {
int index = txtfiles.indexOf(nextfile);
}
else {
txtfiles.add(nextfile);
}
}
//close streams
fileinput.close();
file.close();
//print result
for (int i = 0; i < txtfiles.size(); i++){
System.out.println(Arrays.asList(txtfiles));
}
}
}