TextFile

Script: textfile.sh Package: fileIO Class: TextFile.java

Displays contents of a text file. Start line and stop line are zero-based. Start is inclusive, stop is exclusive.

Basic Usage

textfile.sh <file> <start line> <stop line>

This tool reads and displays specific line ranges from text files. The line numbering is zero-based, where the start line is inclusive and the stop line is exclusive.

Parameters

TextFile uses positional arguments rather than named parameters:

Positional Arguments

<file>
Input text file to read. Use "stdin" to read from standard input.
<start line>
Zero-based line number to start reading from (inclusive). Optional - defaults to 0 if not specified.
<stop line>
Zero-based line number to stop reading at (exclusive). Optional - defaults to start + 100 lines if not specified.

Special Modes

speedtest
When used as the second argument, runs in speed test mode to benchmark file reading performance without printing content.

Examples

Display First 100 Lines

textfile.sh myfile.txt

Displays lines 0-99 (first 100 lines) from myfile.txt.

Display Specific Line Range

textfile.sh myfile.txt 50 75

Displays lines 50-74 (25 lines total) from myfile.txt.

Read from Standard Input

cat largefile.txt | textfile.sh stdin 1000 1010

Reads from stdin and displays lines 1000-1009.

Speed Test Mode

textfile.sh largefile.txt speedtest

Benchmarks reading performance of the entire file without displaying content. Reports processing statistics including lines processed, bytes read, and throughput.

Single Line Display

textfile.sh config.txt 10 11

Displays only line 10 (zero-based) from config.txt.

Algorithm Details

File Reading Strategy

TextFile implements buffered reading using Java's BufferedReader with specific buffer configuration:

Performance Characteristics

The tool handles large text files using specific memory management techniques:

Line Indexing Convention

TextFile uses zero-based line indexing with inclusive start and exclusive stop boundaries:

Error Handling

The implementation includes specific error handling mechanisms:

Utility Methods

The TextFile class provides additional utility methods for specialized text processing:

Technical Implementation

Core Reading Loop

The main reading algorithm follows this pattern:

  1. Stream Initialization: Opens BufferedReader with 32,768-byte buffer via open() method
  2. Line Skipping: Advances to start line using nextLine() calls
  3. Content Reading: Reads and outputs lines from start to stop
  4. Statistics Tracking: Counts lines and bytes processed
  5. Resource Cleanup: Closes streams and releases resources

Speed Test Implementation

Speed test mode measures raw I/O performance by:

Input Source Flexibility

TextFile supports multiple input sources using ReadWrite.getInputStream():

Support

For questions and support: