Current Page: NeXtMidas Training File Handlers Lab 1 Part 4
- - - | Part 4 - Listing: Now lets add the methods that will be used by commands like `DATALIST` when listing elements from the file. For the most part these methods look very similar when going from one file handler to the next. `listElements(..)` and `listElementsPerLine(..)` The `listElementsPerLine(..)` method tells datalist how many elements to list on each line. This allows allows `DATALIST` to conserve vertical space by listing multiple elements per line... we don't realy care about that right now, so just return 1 telling it to display only one element per line.
            public int listElementsPerLine (int lineWidth, String format, int flags) {
              return 1;
            }
    - The `listElements(..)` method actually lists the elements. Again we will go through it line-by-line.
            public String listElements (double start, int elements, String format, int flags) {
              Data data = getDataBuffer(elements);         // 1
              seek(start);                                 // 2
              int numRead = read(data, elements);          // 3
              if (numRead <= 0) return null;               // 4
              return data.toString(0, numRead, 10)+"\n";   // 5
            }
Line 1: Create a data buffer to use when reading in the data. Line 2: Seek to the start of the data we wantr to read. Line 3: Read in the data. Line 4: If there was nothing available to read (e.g. end-of-file), just return `null`. Line 5: Convert the data to a string. In this case we are converting to test starting at `0` with `numElements` total and using base-`10`. By convention, this always has a newline character added on to the end of the string. continued on next page