I have 5 text files (.txt) and they all contain string content. Like one is a po
ID: 671851 • Letter: I
Question
I have 5 text files (.txt) and they all contain string content. Like one is a poem, one is a bunch of random English words etc. What I want to do, is write a method in Java, that reads the contents of these files, and puts them into Arrays/Array Lists. Like I want each Array to contain the words from a particular file. Basically, 5 string arrays containing the words from the 5 different string files respectively. Could someone write this method in Java? I have 5 text files (.txt) and they all contain string content. Like one is a poem, one is a bunch of random English words etc. What I want to do, is write a method in Java, that reads the contents of these files, and puts them into Arrays/Array Lists. Like I want each Array to contain the words from a particular file. Basically, 5 string arrays containing the words from the 5 different string files respectively. Could someone write this method in Java? What I want to do, is write a method in Java, that reads the contents of these files, and puts them into Arrays/Array Lists. Like I want each Array to contain the words from a particular file. Basically, 5 string arrays containing the words from the 5 different string files respectively. Could someone write this method in Java?Explanation / Answer
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class MontyHall {
public static void main(String[] Args) {
String filename="C:/test";
String[] fContent=new String[5];
String r;
BufferedReader br = null;
for(int i=0;i<5;i++){
fContent[i]="";
filename=filename+Integer.toString(i)+".txt";//file names are test0,test1......test4
try{
br = new BufferedReader(new FileReader(filename));
while (( r= br.readLine()) != null) {
fContent[i]=fContent[i]+r+" ";
}
}
catch(IOException e){
e.printStackTrace();
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.