Turn on thread page Beta
    • Thread Starter
    Offline

    0
    ReputationRep:
    Whats the best method/class to read multiple lines of data in a extension of .dat? The reason i asked because there are so many methods to read files int the net that i got lost in it (some of it are already depreciated-as the compiler warns me )

    what i'm trying to do is that read the data below:

    20.0 15.0
    30.0 34.6
    40.0 56.9

    as you can see there are 3 row and 2 columns. Should i parse these data using the Double wrapper class?
    Offline

    0
    ReputationRep:
    Your best bet's using BufferedReader.

    Code:
    // Array list to hold file contents
    List<String> list = new ArrayList<String>();
    
    String line;
    
    FileReader reader = new FileReader("file.dat");
    BufferedReader bufrdr = new BufferedReader(reader);
    
    // Read first line into String variable 'line'
    line = bufrdr.readLine();
    
    // While
    while (line != null) {
       // Add preceding line to ArrayList
       list.add(line);
       // Read next line from file
       line = bufrdr.readLine();
    }
    
    // Close instances of readers
    bufrdr.close();
    reader.close();
    
    // Declare new array for content
    String[] array = new String[list.size()];
    
    // Populate new array with contents of ArrayList
    list.toArray(array);
    This will leave you with an array containing all the data from the file. Since you have multiple values per line you'll want to split those down further, potentially into a multidimensional array so you can access particular values easily. Taking your example data:

    array[0][0] = 20.0 array[0][1] = 15.0
    array[1][0] = 30.0 array[1][1] = 34.6
    array[2][0] = 40.0 array[2][1] = 56.9

    Split functions are well documented online but give me a shout if you need anything else clarifying.
    • Thread Starter
    Offline

    0
    ReputationRep:
    (Original post by MorningView)
    Your best bet's using BufferedReader.

    Code:
    FileReader reader = new FileReader("file.dat");
    BufferedReader bufrdr = new BufferedReader(reader);
    
    // Read first line into String variable 'line'
    line = bufrdr.readLine();
    
    // While
    while (line != null) {
       // Add preceding line to ArrayList
       list.add(line);
       // Read next line from file
       line = bufrdr.readLine();
    }
    
    // Close instances of readers
    bufrdr.close();
    reader.close();
    
    // Declare new array for content
    String[] array = new String[list.size()];
    
    // Populate new array with contents of ArrayList
    list.toArray(array);
    This will leave you with an array containing all the data from the file. Since you have multiple values per line you'll want to split those down further, potentially into a multidimensional array so you can access particular values easily. Taking your example data:

    array[0][0] = 20.0 array[0][1] = 15.0
    array[1][0] = 30.0 array[1][1] = 34.6
    array[2][0] = 40.0 array[2][1] = 56.9
    lol i just did the exact thing!-but i didn't use the list, i just assign the each data in the text file to a 2d array- Of course i've to parse it using the wrapper Double class if i want to do an arithmetic operation on the data.

    i'll try the split function. hay thanks
    Offline

    0
    ReputationRep:
    No worries.

    I guess you could always have an array of type double and cast the values to that as you go along. Depending what you're using the values for after that, it may be worthwhile.
    Offline

    1
    ReputationRep:
    (Original post by MorningView)
    No worries.

    I guess you could always have an array of type double and cast the values to that as you go along. Depending what you're using the values for after that, it may be worthwhile.
    You won't be able to cast. Like newman24x thought, you need to use something like the Double class to parse the Strings to doubles.
 
 
 
Poll
Were you ever put in isolation at school?
Useful resources

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Write a reply...
Reply
Hide
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.