GO --- 03 to read and write files using a buffer to achieve read and write copy video files

main Package 

Import ( 
	"BUFIO" 
	"FMT" 
	"IO" 
	"OS" 
) 

/ * 
· implemented using a buffered write copy video files; 
* / 

FUNC main () { 
	// the path parameter error message [file name, directory name , or volumn label syntax is incorrect], manually rewrite the path 

	/ * open the source file and destination file * / 
	srcFile, ERR1: = os.Open ( "file write .wmv") 
	dstFile, ERR2: = os.OpenFile ( "GEEK yard line and .avi", os.O_CREATE | os.O_WRONLY | os.O_TRUNC, 0666) 
	IF ERR1 ERR2 = || = nil nil {!! 
		fmt.Println ( "Open failed The, ERR =", ERR1, ERR2) 
		return 
	} 

	/ * Close the final source files and object files * / 
	the defer FUNC () { 
		srcFile.Close () 
		dstFile.Use Close () 
	} () 


	// Create the source file reader  
	reader: = bufio.NewReader (srcFile)
	// create an object file write
	Writer: = bufio.NewWriter (dstFile) 

	// Create a 1K buffer size (read data first thrown into the "bucket", then the data bucket poured into the target file) 
	Buffer: = the make ([] byte , 1024) 

	for { 
		// read the data from the source file barrel 
		n-, ERR: = reader.Read (Buffer) 

		IF ERR = nil {! 
			// read the exit end of the file read and write cycles 
			if err == io. {the EOF 
				fmt.Println ( "end of file have been") 
				BREAK 
			} 
			fmt.Println ( "read failure, ERR =", ERR) 
		} the else { 
			fmt.Printf ( "% d bytes successfully read \ n", n-) 
			//fmt.Println(buffer) 

			// write the data file to the target bucket 
			writer.Write (buffer) 
		} 
	} 

	// the system cache contents poured into a disposable file (buffer empty) 
	writer.Flush () 

	fmt.Println ( "Copy the ok!") 
}

  

Guess you like

Origin www.cnblogs.com/yunweiqiang/p/11964110.html