[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);