Results 1 to 4 of 4

Thread: Java - reading text file to an array

  1. #1
    Ultimate Member bassman's Avatar
    Join Date
    Feb 2002
    Location
    Portugal
    Posts
    2,384

    Java - reading text file to an array

    Aloha!

    I can't find an example on how to read a text (sequential) file to an array. Anybody knows how to? By the wqay, if you can tell me how to do the reverse, it would be nice also

  2. #2
    Banned qball's Avatar
    Join Date
    Oct 1999
    Location
    Rockaway, NJ 07866
    Posts
    1,730
    well, if you can do one way, you can basically just reverse and do backwards, though you will need write file privileges to write file, whereas just read privileges to read file.

    to read a file line by line:

    Code:
    String line= "";
    BufferedReader textIn = new BufferedReader(new FileReader(new File("filename.ext")));
    line = textIn.readLine();
    while (line != null){
    	do something, like add array element
    	}
    which leads to a ? what type of array? Does it have to be array?

    And better yet of what size, as you will need to declare this size. Something like a Vector may be more useful.

    Anyway to write to a file is just as easy.

    [code]
    fo = new FileOutputStream (filename.ext, true );
    fo.write( "string");
    fo.write( "string");
    fo.write( 1234);
    fo.close();
    [\code]

    edit
    my bad, using my own java, lol, try:

    Code:
    			java.io.FileOutputStream fo;
    			fo = new FileOutputStream ("filename.ext", true );
    			fo.write( "output\n".getBytes());
    			fo.write( "output\n".getBytes());
    			fo.write( "1234\n".getBytes());
    			fo.close();
    edit
    Last edited by qball; 12-17-2002 at 10:35 PM.

  3. #3
    Ultimate Member bassman's Avatar
    Join Date
    Feb 2002
    Location
    Portugal
    Posts
    2,384
    Hum...I think I'm getting somewhere here. It's an entrance control application and it has to be done with an array of type StudentCard. Each student card contains several atributes: I'm attaching the text file with this post. Mainly, I'll have to load everything from the file to the array, work on it and resend it to the file. Each position of the array will have one student card.
    Attached Files Attached Files

  4. #4
    Banned qball's Avatar
    Join Date
    Oct 1999
    Location
    Rockaway, NJ 07866
    Posts
    1,730
    aloha is hawaii (an)?

    hola, is spanish.

    what's a:

    "StudentCard"

    do your own homework.

    what's a:

    "entrance control application"

    ?

    "'ll have to load everything from the file to the array, work on it and resend it to the file"

    Then, you'll need to read in the file, put into an array, work on it, and then, resend back to original file, where you got it from?

    or something like that?

Posting Permissions

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