PHP code example of stk2k / iostream

1. Go to this page and download the library: Download stk2k/iostream library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

stk2k / iostream example snippets


use stk2k\iostream\string\StringInputStream;

// foreach
$sis = new StringInputStream('Hello');
foreach ($sis as $c){
    echo $c . '.';    // H.e.l.l.o.
}

// read
$sis = new StringInputStream('Hello');
while($c = $sis->read(1)){
    echo $c . '.';    // H.e.l.l.o.
}

// read line
$sis = new StringInputStream("Foo\nBar\nBaz");
while($line = $sis->readLine()){
    echo $line . '.';    // Foo.Bar.Baz.
}

// read lines
$sis = new StringInputStream("Foo\nBar\nBaz");
$lines = $sis->readLines();
echo implode('.', $lines);    // Foo.Bar.Baz

use stk2k\iostream\string\PushBackStringInputStream;
use stk2k\xstring\xStringBuffer;

$sis = new PushBackStringInputStream(', World!');

$sis->unread(new xStringBuffer('olleH'));
echo $sis->readLine();          // Hello, World!

use stk2k\filesystem\File;
use stk2k\iostream\file\FileInputStream;

// foreach
$file = new File('test/_files/b.txt');
$fis = new FileInputStream($file);
foreach ($fis as $c){
    echo $c . '.';    // H.e.l.l.o.
}

// read
$file = new File('test/_files/b.txt');
$fis = new FileInputStream($file);
while($c = $fis->read(1)){
    echo $c . '.';    // H.e.l.l.o.
}

// read line
$file = new File('test/_files/c.txt');
$fis = new FileInputStream($file);
while($line = $fis->readLine()){
    echo $line . '.';    // Foo.Bar.Baz.
}

// read lines
$file = new File('test/_files/c.txt');
$fis = new FileInputStream($file);
$lines = $fis->readLines();
echo implode('.', $lines);    // Foo.Bar.Baz