The first thing is I do not use an array but a class called ArrayList which lets you add elements without first identifying the length (we do not know a prior how many lines you have the file).
Code:
import java.util.ArrayList;
{Leef class
public static void main (String [] arg) {
File file = null;
FileReader fr = null;
ArrayList lines = new ArrayList ();
try {
file = new File ("C: / file.txt");
String line;
fr = new FileReader (file);
BufferedReader br = new BufferedReader (fr)
while ((line = br.readLine ())!= null) {
lineas.add (line);
}
System.out.print ("The Vector is: \ n ");// UNDER THE DATA SHOW
for (int j = 0, j <lineas.size () j + +) {
line = (String) lineas.get (j);
System.out.print (line + "\ n");
}
} / / END TRY
catch (IOException e) {
System.out.println (e);
}
finally {
try {/ / finally block is always executed, so if the file is closed
if (fr! = null) {/ / at the end of the first try, and has an error before, would
fr.close (), / / the 1st catch and then would leave, leaving it open. It is convenient
} / / Close here, proving that not-for a previous error, as
} Catch (IOException e) {/ / not be read or does not exist - null value.
}
}
System.exit (0);}
}
And if you are trying to read an ASCII file into an arraylist, then following code will help you :
Code:
FileReader myFile = new FileReader ("path");
BufferedReader buffer = new BufferedReader (myFile);
String string = file buffer.readLine ();
dateString.getBytes ("ASCII");
or
Code:
FileInputStream my file = new FileInputStream ("path");
BufferedReader buffer = new BufferedReader (new InputStreamReader (myFile, "ASCII"));
<Integer> ArrayList list = new ArrayList <Integer> ();
list.add (buffer.read ());
Bookmarks