Reading a file into an array?
Hello,
I need to read a file into an array so I am able to manipulate it later. Among many things, taking a count of all the words in the file is something I need to do. My program is reading a file and storing as an array. It gives me a wrong number though. It only reads words up until a return/paragraph in the file. Any help on this would be much appreciated.
Here is the code:
import java.util.*;
import java.io.*;
import java.lang.*;
import java.text.*;
public class fileReader
{
public static String my_fName, str1 = null;
public static String[] words;
public fileReader(String fName)
{
try{
my_fName = fName;
FileInputStream fstream = new FileInputStream(my_fName);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader (new InputStreamReader(in));
(str1 = br.readLine()
String [] tokens = str1.split(" ");
words = tokens;
}
catch (Exception e){}
}
public static int numWords() throws NullPointerException
{
//String [] tokens = str1.split(" ");
int num1 = words.length;
return num1;
}
public static void main (String []args)
{
fileReader fr1 = new fileReader (args[0]);
System.out.println(fr1.numWords());
}
}