[PHP] Back to the basics (IO flow) Basic knowledge of php for beginners php tutorial php hands-on

WBOY
Release: 2016-07-29 08:49:15
Original
1021 people have browsed it

IOStreams are used to handle data transmission between devices

javaThe operation of data is through streams

javaThe objects used to operate streams are in the IO package

stream According to the operation data, it is divided into two types: byte stream and character stream. The stream is divided into two types according to the flow direction: input stream and output stream.

FileWriter

object,

new

comes out, construction parameter:

String

file name; at this time, the file will be created in the specified directory, if it already exists, it will be overwritten; this method will throw

IOExceptionExceptionCall the write() method of the FileWriter object to write the string into the stream. Parameters: String

String

Call FileW riter Object's flush() method, refresh stream

        FileWriter fw=<span>new</span> FileWriter("test.txt"<span>);
        fw.write(</span>"hello3"<span>);
        fw.close();</span><span>//</span><span>刷新并关闭</span>
Copy after login

file operation, read get FileReader object, new

come out, construction parameters:

String

’s files The name

calls the read() method of the FileReader object to return the read length. If it reaches the end, it will return -1

. Parameter:

char[] character array whileLoop reading, condition: if the read length is not -1combination string

        FileReader fr=<span>new</span> FileReader("test.txt"<span>);
        </span><span>char</span>[] buf=<span>new</span><span>char</span>[2<span>];
        </span><span>int</span> len=0<span>;
        StringBuilder sb</span>=<span>new</span><span> StringBuilder();
        </span><span>while</span>((len=fr.read(buf))!=-1<span>){
            sb.append(</span><span>new</span> String(buf,0<span>,len));
        }
        System.out.println(sb.toString());</span>
Copy after login
PHP version, file operation, write

call function fopen(), Open the file to get the

file

object, parameters:

String

file name,

"w" to write, if the file does not exist, it will be created Call the fwrite() method to write directly to the file Inside, parameters: file object, String

string

call fclose() method to close the stream, parameters: file object

<span>$file</span>=<span>fopen</span>("test.txt","w"<span>);
</span><span>fwrite</span>(<span>$file</span>,"hello"<span>);
</span><span>fclose</span>(<span>$file</span>);
Copy after login

File operation, read call function fopen(), open the file to get

file

object, parameters:

String

file name, ”r” read call function fread(), get the string of String, parameters: file

object, read length

while loop reading, condition: not to the end of the file, feof($ file) is not concatenated string

<span>$file</span>=<span>fopen</span>("test.txt","r"<span>);
</span><span>$str</span>=""<span>;
</span><span>while</span>(!<span>feof</span>(<span>$file</span><span>)){
    </span><span>$str</span>.=<span>fread</span>(<span>$file</span>, 1<span>);
}

</span><span>echo</span><span>$str</span><span>;
</span><span>fclose</span>(<span>$file</span>);
Copy after login
fortrue

The above introduces [PHP] Back to the Basics (IO Flow), including PHP and basic aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!