Results 1 to 5 of 5
  1. #1
    fritz1474 is offline Member
    Join Date
    Sep 2008
    Posts
    8
    Rep Power
    0

    Default Help reading file

    I need to read an input file and print the first and last record and then run calculations on the last record. I think I know how to read the file (about 2000 records) and count the records, but how do you speficy the first or last record? I am new to this so any help is appreciated.

  2. #2
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    Append all records into the array,if you know the size of the records amount,or if you don't know,with the List,so then you will know the last and the first record

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.io.*;
    
    public class FirstAndLast {
        public static void main(String[] args) throws IOException {
            File file = new File("firstAndLast.txt");
            BufferedReader br = new BufferedReader(
                                new InputStreamReader(
                                new FileInputStream(file)));
            int lineCount = 0;
            String lastLine = null;
            String line;
            while((line = br.readLine()) != null) {
                if(lineCount == 0) {
                    System.out.printf("count = %d  first line = %s%n",
                                       lineCount, line);
                }
                lastLine = line;
                lineCount++;
            }
            br.close();
            // Finished reading file, "lastLine" has the last line.
            System.out.printf("count = %d  lastLine = %s%n",
                                       lineCount, lastLine);
        }
    }
    firstAndLast.txt
    Java Code:
    one
    two
    three
    four
    five
    six
    seven

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    The first record should be the easy one to save.
    Then in a loop read the rest of the file and save the last record read before reading the next one. When you get the EOF indicator, the last record saved will be the last record in the file.
    That's the description for the code that hardwired gave you.

  5. #5
    fritz1474 is offline Member
    Join Date
    Sep 2008
    Posts
    8
    Rep Power
    0

Similar Threads

  1. reading csv file help
    By fritz1474 in forum New To Java
    Replies: 5
    Last Post: 09-04-2008, 08:41 PM
  2. Right use of file reading ?
    By jurka in forum New To Java
    Replies: 3
    Last Post: 08-27-2008, 08:16 PM
  3. Reading a file
    By mew in forum New To Java
    Replies: 2
    Last Post: 12-30-2007, 12:23 PM
  4. Reading a file for use
    By peachyco in forum New To Java
    Replies: 2
    Last Post: 11-27-2007, 03:49 AM
  5. Reading from a file
    By leebee in forum New To Java
    Replies: 1
    Last Post: 07-23-2007, 12:02 PM

Posting Permissions

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