This project is a Java-based application designed to manage and manipulate arrays stored in binary files. It was developed as part of a school project to demonstrate file handling, random number generation, and basic command-line interface (CLI) operations in Java.
The program allows users to create a binary file containing an array of random integers, increment all values in the
array, and display the array in a formatted manner. The application is composed of two main classes: Main and
FileArray.
-
Random Array Creation:
- Generates a binary file containing an array of random integers.
- The size of the array is randomly chosen between 1 and (2^5) (32).
-
Increment All Values:
- Increments all integers in the array by 1.
-
Formatted Array Printing:
- Displays the array in a formatted table with indices and values aligned for readability.
-
Command-Line Interface:
- Accepts commands via CLI to perform operations on the array.
The Main class is the entry point of the application. It:
- Accepts the file path and commands as arguments.
- Initializes the
FileArrayobject. - Processes commands (
ifor increment,pfor print) to manipulate or display the array.
The FileArray class handles all file-related operations:
- File Creation: Creates a binary file with random integers if the file does not already exist.
- Increment Operation: Reads the integers from the file, increments them, and writes them back to the file.
- Formatted Printing: Reads the integers and displays them in a structured format.
- If the file path is invalid or inaccessible, the program will display an error message.
- If an unrecognized command is provided, the program will print: unrecognized command.
The GZIPFileArray class extends the FileArray class to handle files compressed in GZIP format.
-
Reading Compressed Files:
- Overrides the
readmethod to read integer arrays from GZIP-compressed files usingGZIPInputStream.
- Overrides the
-
Writing Compressed Files:
- Overrides the
writemethod to write integer arrays to GZIP-compressed files usingGZIPOutputStream.
- Overrides the
- Uses compressed streams (
GZIPInputStreamandGZIPOutputStream) for reading and writing data. - Maintains the same interface and behavior as the base class, with added support for compression.
The GZIPFileArray class is ideal for reducing the storage space required for binary files while preserving the ability to read and write integer arrays transparently.
GZIPFileArray gzipFileArray = new GZIPFileArray("example.zip", 10);
gzipFileArray.incrementAll();
gzipFileArray.print();
-
File I/O:
- Reading and writing binary files using
DataInputStreamandDataOutputStream.
- Reading and writing binary files using
-
Random Number Generation:
- Generating random integers using
java.util.Random.
- Generating random integers using
-
Command-Line Arguments:
- Parsing and processing user input from the command line.
-
Error Handling:
- Managing exceptions such as
IOException.
- Managing exceptions such as
-
Formatted Output:
- Using
System.out.printffor structured and readable output.
- Using
This project is intended for educational purposes only and is not meant for commercial use.
- Name: [Filippo Spazzali]
- School: [Università degli Studi di Trieste]
- Course: [Computer Science]