Stream and FileStream

Hi All,

What is the use of Stream and FileStream variable types?

can someone explain with the example, please?

Thanks,
Pavan.

@Pavan_kumar15,

Stream and FileStream variable types are used to handle data streams, particularly for reading from and writing to files. They are part of the .NET framework and provide powerful ways to manage file input/output operations. Here’s a detailed explanation of each:

Stream

The Stream class is an abstract base class that provides a generic view of a sequence of bytes. It is designed for working with various types of input and output, including files, network communications, memory, and more.

Common Uses:

  1. Reading and Writing Data: You can read from and write to different types of storage.
  2. Chaining Streams: Streams can be chained together to perform complex operations (e.g., reading compressed data and decompressing it on the fly).
  3. Buffered Operations: Provides methods for buffered input and output, improving performance by minimizing direct interactions with the underlying storage.

Example:

  • Reading data from a network socket.
  • Writing data to a file.

FileStream

The FileStream class is a derived class of Stream specifically designed for reading from and writing to files. It provides a way to work with files at a lower level than typical file handling methods.

Common Uses:

  1. File Operations: Reading from and writing to files.
  2. Random Access: Allows seeking to specific positions within a file.
  3. Performance: Offers efficient handling of file operations through buffering.

Example:

  • Opening a file to read its contents.
  • Writing data to a specific position in a file.

Example Workflow

  • Reading a File Using FileStream:
    1. Create Variables:
    • fileStream of type System.IO.FileStream.
    • buffer of type Byte[].
    1. Assign Activity:
fileStream = new System.IO.FileStream("C:\path\to\file.txt", System.IO.FileMode.Open)
  1. Read File Content:
    * Use Invoke Method activity to read bytes from the fileStream into the buffer.
  2. Convert Bytes to String:
    * Use Assign activity to convert the byte array to a string.

Both types are useful for advanced file and data handling scenarios in UiPath Studio, enabling fine-grained control over reading and writing operations.

LLM helped me to write this but it’s validated by me.

Thanks,
Ashok :slight_smile:

With streams we have an abstract concept to describe the flow of bytes when we want to read / write a file.

This concept can be extended to more specialized implementations e.g.
a FileStream - Flowing the bytes from / to a file
a BufferedStream - a controlled flowing

Stream - Provides a generic view of a sequence of bytes.

FileStream - used for files: Provides a Streamfor a file, supporting both synchronous and asynchronous read and write operations.