Click to See Complete Forum and Search --> : Reading file lines into an Array of Strings


tpoupko
September 22nd, 2000, 04:26 PM
I need to be able to read a text file into an array of strings where each line has to go into a separate string.
I need to be able to read each line manipulate it and save it back to the same location in the file.
I wrote this code, but it does not read the line into the buf[]

public void updateScreen() {

DataInputStream dis = null;
try {
File trailFile = new File("D:\\RT\\CARRIER\\" + choiceControl1.getSelectedItem() + "\\TRAILER" + choiceControl2.getSelectedItem());//jTextField2.getText());
FileInputStream fis = new FileInputStream(trailFile);
BufferedInputStream bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
buf = new String[25]; //25 lines in the file

for(int j = 0; j < trailFile.length(); j++)
{
try {
for(int i = 0; i < 25; i++)
buf[i] = dis.readLine();
}
catch (Exception rr) {}
}

dis.close();
bis.close();
fis.close();
}
catch (Exception ee) {}

jTextField6.setText(buf[12]);

What am I doing wrong???

Thanks so much for any help.

Tami

laneh
September 22nd, 2000, 04:49 PM
you have:
buf = new String[25];

where do you declare buf to be of type String array?
doesn't it need to be like this: String[] buf = new String[25];

and here..
buf = dis.readLine();

this needs to be:
buf[i] = dis.readLine();

personally , i'd make buf an ArrayList like this:
ArrayList buf = new ArrayList();

and then when you read in the file in the loop you can just do this:
buf.add(dis.readLine());

and when you get it out of buf just typecast the output to String.

"There are no facts, only interpretations."
-Friedrich Nietzsche

Phill
September 22nd, 2000, 08:43 PM
Hi there
What sort of file are you trying to read?
Phill.

tpoupko
September 25th, 2000, 08:55 AM
just a flat text file

tpoupko
September 25th, 2000, 09:21 AM
I declared my buf in my public class Frame (where I declared all my components).

Phill
September 26th, 2000, 02:22 AM
Hi
If you have to use a String[] , then it
becomes difficult to declare the length
of the array. The length() method that
you used to make the array size, returns the
amount of bytes in the file, not the ammount
of lines, so this isnt a good method to use.
Anyway to place the lines in an individual
array index, I have first put them in a
Vector(because you dont have to specify, the
length of a Vector) and then, I have used
the "toArray()" method which places the
elements into an object array and then
used a for loop to cast each index of the
Object array to a String and place it into
a String[]. Which is a lot of work, when
you could just leave it in the Vector and
get the same results. But since you want
it in the String[], this is one way to
get this result:

Vector v = new Vector();
try{
BufferedReader br = new BufferedReader(new FileReader("SearchSys.java"));
String line = br.readLine();
while(line != null){
v.add(line); // add the line to a Vector
line = br.readLine(); // read the next line
}
br.close();
}catch(IOException e){
System.out.println("Error " + e);
}
Object[] o = v.toArray(); // from a Vect to Object array
String[] stray = new String[o.length];
for(int i = 0; i < o.length; i++){
stray[i] = (String)o[i]; // cast from Object to String
System.out.println(stray[i]);
}



Another way would be to read the file twice,
the first time to count the lines,
then declare the String array and
the second time to place them into
the String array. And there are
no doubt many other way to do it.
Good luck
Phill.

tpoupko
September 26th, 2000, 09:03 AM
Your suggestion is great. I managed to do the reading with the string array since I know the number of lines in the file, so I set that number in my 'for' statement.
I do appriciate your response. Thank you so very much.
If I can be of any help to you, please contact me.

Tami

Phill
September 27th, 2000, 02:48 AM
Your welcome Tami!
Phill