[PHP] Back to basics (IO stream) PHP Beginner Basics php Tutorial php hand

Source: Internet
Author: User
Tags fread
Io flow is used to process data transfer between devices

The Java operation of the data is streamed through the way

The objects that Java uses to manipulate the flow are in the IO Package

There are two types of flow by operation data: byte stream and character stream

Flow is divided into: input stream, output stream

Character Stream

Abstract base class:Reader Writer

File operations, writing

Gets the FileWriter object,new , constructs the parameter: The file name of the String, and, if it already exists, creates it under the specified directory. will be overwritten; This method throws a ioexception Exception

Call the write () method of the FileWriter object to write the string to the stream, parameter: string

Call the flush () method of the FileWriter object to flush the stream

        FileWriter fw=New FileWriter ("test.txt");        Fw.write ("Hello3");        Fw.close (); // Refresh and Close

File operation, read

Gets the filereader object,new , constructs the parameter: The file name of the String

Call the Read () method of the filereader object , return the length of the reading, or 1, if it returns to the end of theparameter: char[] character array

while loop read, condition: If the length of the read is not -1

Combining strings

        FileReader fr=New filereader ("test.txt");         Char [] buf=newchar[2];         int len=0;        StringBuilder SB=new  StringBuilder ();          while ((Len=fr.read (BUF))!=-1) {            sb.append (new String (buf,0, Len));        }        System.out.println (Sb.tostring ());

PHP version, file operation, write

Call the function fopen (), open the file to obtain the file object, parameter:String filename,"W" Write is created if the file does not exist

Call the fwrite () method and write it directly to the file, parameter: string

Call the fclose () method to close the stream, parameter:file object

$file=fopen("Test.txt", "W"); fwrite ($file, "Hello"); fclose ($file);

File operation, read

calling Functions fopen () , open file Get file object, Parameters: String the file name, "R" Read

Call function fread (), get string strings, arguments:file object, read length

while loop read, condition: No to end of file,feof ($file) not true

Stitching strings

$file=fopen("Test.txt", "R"); $str= "";  while (! feof ($file)) {    $str. =fread($file, 1);} Echo $str ; fclose ($file);

The above describes [PHP] back to the basics (IO stream), including PHP, the basic aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.