Reading specific part of a file


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Reading specific part of a file

  1. #1
    Join Date
    Aug 2004
    Posts
    1

    Reading specific part of a file

    Hi
    Im trying to design a java program that reads a txt file such as the one below:

    0 400 450 510 670 493 958
    1 500 560 630 5633
    2 625 676 740
    3 1000 1250 1600
    4 432
    5 2435 235 2654

    Then through a user prompt, outputs a specific number from a specific line to the user, i.e.
    Line 1
    Column 3

    Would print 450

    i have little knowledge of java, and im not sure if i need to read the whole file into arrays or i can get java just to read a specific line and column from it
    i have tried using arrays but as the txt file can be of any size it got confusing!

    Please help!

    Thanks to you all

  2. #2
    Join Date
    Feb 2004
    Posts
    808
    erm, you cant consider files as columns and rows, because thye are just a stream of data. your file is hence looking like:

    0 400 450 510 670 493 958CRLF1 500 560 630 5633CRLF2 625 676 740CRLF3 1000 1250 1600CRLF4 432CRLF5 2435 235 2654

    and hence it is impossible to calculate where to go for row 4 col 1 etc

    you shoudl read the whole thing into an array of arrays. the basic logic goes like this:

    define an array called theFile[][] (yes, two sets of [])
    define an ArrayList object
    define a BufferedReader, wrapped around a FileReader, pointing to your file
    repeatedly bufferedreader.readLine() a string in, and add it to the arrayList
    at the end of the file, ask the arraylist how big it is.
    make theFile[][] = new String[howBigItIs][] (yes, leave the second one blank
    step through the arraylist, pulling each string out.
    split() the string based on space, and you will get a String[] array out of the split operation
    assign the String[] to theFile[incrementingIndex]

    now you have your whole file represented as:

    theFile[line][column]

    be sure to check the bounds before attempting to access the array

    eg:

    if(theLineNumber < theFile.length && theColumn < theFile[theLineNumber].length)

    if theLineNumber is out of range, then the second part of the test wont run (first is false, and theres no way a FALSE AND <anything> can be true)
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  3. #3
    Join Date
    Feb 2004
    Posts
    808
    note: arrayList are like arrays but they grow automatically.. hence you can have them grow,grow,grow.. and then ask them for their size and make a fixed size array based on that

    for examples of BufferedReader and readLine, search for posts by me that cotain these words
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center