Hi, I am trying to make an array to read txt files. I have hard coded it to 3 for the array but I would prefer for it to be able to read the txt files to see how big the array should be. I have implemented some code to this but I am having trouble getting it to work.
package searchengine2;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
/**
*
* @author Alex
*/
public class ReadFile {
private String path;
int readLines() throws IOException {
FileReader file_to_read = new FileReader(path);
BufferedReader textReader = new BufferedReader(file_to_read);
String aLine;
int numberOfLines = 0;
while (( aLine = bf.readLine()) !=null) {
numberOfLines++;
}
bf.close();
return numberOfLines;
}
public ReadFile(String file_path) {
path = file_path;
}
public String[] OpenFile() throws IOException {
FileReader fr = new FileReader(path); //reads characters
BufferedReader textReader = new BufferedReader(fr); //used to store characters
int numberOfLines = readLines(); //number of lines in array
String[] textData = new String[numberOfLines]; //set up array
int i;
for (i=0; i < numberOfLines; i++) { //loop to put data into array
textData[i] = textReader.readLine(); //access the data, textReader holding data in memory (buffer)
}
textReader.close(); //removes data from buffer
return textData;
}
}
Many Thanks if you can help!