CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2000
    Location
    San Antonio
    Posts
    51

    Reading file lines into an Array of Strings

    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


  2. #2
    Join Date
    Sep 2000
    Location
    Texas
    Posts
    22

    Re: Reading file lines into an Array of Strings

    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

  3. #3
    Join Date
    Sep 2000
    Location
    Melbourne --> Australia
    Posts
    68

    Re: Reading file lines into an Array of Strings

    Hi there
    What sort of file are you trying to read?
    Phill.


  4. #4
    Join Date
    Jul 2000
    Location
    San Antonio
    Posts
    51

    Re: Reading file lines into an Array of Strings

    just a flat text file



  5. #5
    Join Date
    Jul 2000
    Location
    San Antonio
    Posts
    51

    Re: Reading file lines into an Array of Strings

    I declared my buf in my public class Frame (where I declared all my components).



  6. #6
    Join Date
    Sep 2000
    Location
    Melbourne --> Australia
    Posts
    68

    Re: Reading file lines into an Array of Strings

    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.



  7. #7
    Join Date
    Jul 2000
    Location
    San Antonio
    Posts
    51

    Re: Reading file lines into an Array of Strings

    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


  8. #8
    Join Date
    Sep 2000
    Location
    Melbourne --> Australia
    Posts
    68

    Re: Reading file lines into an Array of Strings

    Your welcome Tami!
    Phill


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured