satooshi / file-client
文件读取器/写入器
0.1.0
2013-02-12 06:22 UTC
Requires
- php: >=5.3.0
- satooshi/ltsv-encoder: dev-master
This package is not auto-updated.
Last update: 2024-09-14 13:59:03 UTC
README
FileClient对象可以从文件中读取或写入内容。您可以通过向walk()方法传递回调或闭包来处理每行的文件读取过程。此方法旨在用于数据库操作。例如,您可以从读取的行中插入或更新记录。
安装
要使用Composer安装FileClient,只需将以下内容添加到您的composer.json文件中
// composer.json { // ... require: { // ... "satooshi/file-client": "dev-master" } }
然后,您可以通过从您的composer.json文件所在的目录运行Composer的update命令来安装新的依赖项
# install $ php composer.phar install # update $ php composer.phar update satooshi/file-client # or you can simply execute composer command if you set composer command to # your PATH environment variable $ composer install $ composer update satooshi/file-client
此库的Packagist页面为 https://packagist.org.cn/packages/satooshi/file-client
或者您可以使用git clone
# HTTP $ git clone https://github.com/satooshi/FileClient.git # SSH $ git clone git@github.com:satooshi/FileClient.git
使用方法
纯文本文件
构造
<?php use Contrib\Component\File\Client\Plain\FileReader; // construction $path = '/path/to/file'; $client = new FileReader($path);
<?php use Contrib\Component\File\Client\Plain\FileReader; // default options $options = array( 'newLine' => PHP_EOL, 'throwException' => true, // throw exception on runtime error 'autoDetectLineEnding' => true, // better line ending handling on Mac ); // construct with options $client = new FileReader($path, $options);
读取
<?php use Contrib\Component\File\Client\Plain\FileReader; $path = '/path/to/file'; $client = new FileReader($path); // read $content = $client->read(); $lines = $client->readLines();
写入
<?php use Contrib\Component\File\Client\Plain\FileWriter; $path = '/path/to/file'; $client = new FileWriter($path); // write $content = 'hello world!'; $client->write($content); $lines = array( 'line1', 'line2', ); $client->writeLines($lines);
追加
<?php use Contrib\Component\File\Client\Plain\FileAppender; $path = '/path/to/file'; $client = new FileAppender($path); // append $content = 'hello world!'; $client->write($content); $lines = array( 'line1', 'line2', ); $client->writeLines($lines);
遍历
FileReaderIterator对象可以遍历读取的文件。
<?php use Contrib\Component\File\Client\Plain\FileReaderIterator; // construction $path = '/path/to/file'; $client = new FileReaderIterator($path);
<?php use Contrib\Component\File\Client\Plain\FileReaderIterator; // default options $options = array( 'newLine' => PHP_EOL, 'throwException' => true, // throw exception on runtime error 'autoDetectLineEnding' => true, // better line ending handling on Mac 'skipEmptyCount' => true, 'limit' => 0, 'offset' => 0, ); // construct with options $client = new FileReaderIterator($path, $options);
<?php use Contrib\Component\File\Client\Plain\FileReaderIterator; // construction $path = '/path/to/file'; $client = new FileReaderIterator($path); // walk $client->walk( funtion ($line, $numLine) { if ($numLine === 1) { // do something at line 1 } } );
文件格式
目前支持json、xml、ltsv文件格式。由Symfony Serializer组件支持对象序列化。
构造
<?php use Contrib\Component\File\Client\Generic\GenericFileReader; // construction $path = '/path/to/file'; $client = new GenericFileReader($path);
<?php use Contrib\Component\File\Client\Generic\GenericFileReader; // default options $options = array( 'newLine' => PHP_EOL, 'throwException' => true, // throw exception on runtime error 'autoDetectLineEnding' => true, // better line ending handling on Mac ); // construct with options $client = new GenericFileReader($path, $options);
读取
<?php use Contrib\Component\File\Client\Generic\GenericFileReader; $path = '/path/to/file'; $client = new GenericFileReader($path); // read as json $content = $client->readAs('json'); $lines = $client->readLinesAs('ltsv'); // read as json to object $content = $client->readAs('json', 'Entity'); $lines = $client->readLinesAs('ltsv', 'Entity');
写入
<?php use Contrib\Component\File\Client\Generic\GenericFileWriter; $path = '/path/to/file'; $client = new GenericFileWriter($path); // write Entity $content = new Entity(); $client->writeAs($content); $lines = array( new Entity(), new Entity(), ); $client->writeLinesAs($lines);
追加
<?php use Contrib\Component\File\Client\Generic\GenericFileAppender; $path = '/path/to/file'; $client = new GenericFileAppender($path); // append Entity $content = new Entity(); $client->writeAs($content); $lines = array( new Entity(), new Entity(), ); $client->writeLinesAs($lines);
遍历
<?php use Contrib\Component\File\Client\Plain\GenericFileReaderIterator; // construction $path = '/path/to/file'; $client = new GenericFileReaderIterator($path);
<?php use Contrib\Component\File\Client\Plain\GenericFileReaderIterator; // default options $options = array( 'newLine' => PHP_EOL, 'throwException' => true, // throw exception on runtime error 'autoDetectLineEnding' => true, // better line ending handling on Mac 'skipEmptyCount' => true, 'limit' => 0, 'offset' => 0, ); // construct with options $client = new GenericFileReaderIterator($path, $options);
<?php use Contrib\Component\File\Client\Plain\GenericFileReaderIterator; // construction $path = '/path/to/file'; $client = new GenericFileReaderIterator($path); // walk as json $client->walkAs( funtion ($line, $numLine) { if ($numLine === 1) { // do something at line 1 } }, 'json' ); // walk as json to object $client->walkAs( funtion ($line, $numLine) { if ($numLine === 1) { // do something at line 1 } }, 'json', 'Entity' );