[PHP] returning to the basics (IO stream) and io

Source: Internet
Author: User

[PHP] returning to the basics (IO stream) and io

IO streams are used to process data transmission between devices.

Java operates data through a stream.

Java objects used for stream operations are all in the IO package.

Streams are divided into byte streams and byte streams.

Streams are divided into input streams and output streams by flow.

 

Ghost stream

Abstract base class: Reader Writer

 

File Operations, write

Get the FileWriter object, new, and construct the parameter: String file name. The file will be created in the specified directory. if it already exists, it will be overwritten. This method throws an IOException.

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 refresh the stream.

FileWriter fw = new FileWriter ("test.txt"); fw. write ("hello3"); fw. close (); // refresh and close

 

 

File Operations, read

Get the FileReader object, which is new. The construction parameter is the name of the String file.

Call the read () method of the FileReader object to return the read length. If the read length ends,-1 is returned. Parameter: char [] character array.

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

String combination

        FileReader fr=new FileReader("test.txt");        char[] buf=new char[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, file operations, write

Call the fopen () function to open the file and obtain the file object. The parameter is the String file name and "w" is written. If the file does not exist, a file is created.

Call the fwrite () method and write it directly to the file. Parameter: file object, String

Call the fclose () method to close the stream. Parameter: file object

 

$file=fopen("test.txt","w");fwrite($file,"hello");fclose($file);

 

File Operations, read

Call the fopen () function to open the file and obtain the file object. The parameter is the String file name, which is read by "r ".

Call the function fread () to obtain the String, parameter: file object, read Length

While loop reading, condition: not to the end of the file, feof ($ file) is not true

Concatenated string

$file=fopen("test.txt","r");$str="";while(!feof($file)){    $str.=fread($file, 1);}echo $str;fclose($file);

 

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.