First wrong thing you are doing is " Using array for this purpose ".
java does not allow variable-sized arrays so it would be a wastage of memory. Since your file size would not be fixed.
Instead of that you should you arraylist or vector.
I will give you an example using arraylist
Suppose this is your text file
Name Jack Williams Sam
Chemistry 10 9 8
Physics 11 12 13
Maths 14 15 16
To read this and store this in 4 arraylist named (name, chem , phy , maths) you can do like this
class FileRead {
public static void main(String[] args) {
try {
[INDENT]File f = new File("c://hello.txt");[/INDENT][INDENT]FileReader file = new FileReader(f);[/INDENT][INDENT]char[] c = new char[200];[/INDENT][INDENT]ArrayList<String> name = new ArrayList<String>();[/INDENT][INDENT]ArrayList<String> chem = new ArrayList<String>();[/INDENT][INDENT]ArrayList<String> phy = new ArrayList<String>();[/INDENT][INDENT]ArrayList<String> maths = new ArrayList<String>();[/INDENT][INDENT]int k = file.read(c);[/INDENT][INDENT]StringBuffer b = new StringBuffer();[/INDENT][INDENT]int counterNxArray = 0;[/INDENT][INDENT]for (int i = 0; i < c.length; i++) {[/INDENT][INDENT] int counter = 0;[INDENT] if (c[i] == ' ' || c[i] == '\n' || i == k) {[/INDENT][INDENT] switch (counterNxArray) {[/INDENT][INDENT][INDENT]case 0:[/INDENT][INDENT] name.add(b.toString());[/INDENT][INDENT] System.out.println(b.toString());[/INDENT][INDENT] break;[/INDENT][INDENT] case 1:[/INDENT][INDENT] chem.add(b.toString());[/INDENT][INDENT] System.out.println(b.toString());[/INDENT][INDENT] break;[/INDENT][INDENT] case 2:[/INDENT][INDENT] phy.add(b.toString());[/INDENT][INDENT] System.out.println(b.toString());[/INDENT][INDENT] break;[/INDENT][INDENT] case 3:[/INDENT][INDENT] maths.add(b.toString());[/INDENT][INDENT] System.out.println(b.toString());[/INDENT][INDENT] break;[/INDENT]}
b.replace(0, b.length(), "");
counter = counter + 1;
if (i == k)
break;
}
else
{
b.append(c[i]);
}
if (c[i] == '\n')
{
counterNxArray = counterNxArray + 1;
}
[/INDENT]}
System.out.println("*** The array contains following values ****");
for (int i = 0; i < name.size(); i++)
System.out.println(name.get(i) + " \t " + chem.get(i) + "\t "[INDENT] + phy.get(i) + "\t " + maths.get(i) + "\t ");[/INDENT]
[/INDENT]} catch (FileNotFoundException e) {[INDENT]e.printStackTrace();[/INDENT]
} catch (Exception e) {
[INDENT][INDENT] e.printStackTrace();[/INDENT]
[/INDENT][INDENT]}[/INDENT]
}
}