Go語言程式:複製一個檔案到另一個檔案


在Go語言中,我們可以使用`os`包和`io`包從一個檔案複製資料到另一個檔案。在第一種方法中,我們將使用`os`包中的`os.Open`、`os.Create`和`io.Copy`函式。而在第二種方法中,我們將使用`ioutil.ReadFile`和`ioutil.WriteFile`來複制檔案。

方法一:使用`os`包

在這個例子中,程式首先使用`os.Open`函式開啟原始檔`source.txt`。然後,它使用`os.Create`函式建立目標檔案`destinaton.txt`。然後,使用`io.Copy`函式將原始檔的內容複製到目標檔案。

語法

os.Open

用於開啟檔案以進行讀取。它接受一個輸入,即要開啟的檔名。

os.create

它有助於建立新檔案。檔名作為函式的輸入給出。

os.copy

它有助於將內容從一個檔案複製到另一個檔案。此函式需要兩個引數,即目標檔案和原始檔。

io.Copy

這是`io`包中用於將檔案從一個位置複製到另一個位置的函式。它接受兩個引數,即原始檔和目標檔案。

演算法

  • 步驟1 - 建立`main`包,並在程式中宣告`fmt`(格式化包)、`io`和`os`包,其中`main`生成可執行程式碼,`fmt`幫助格式化輸入和輸出。

  • 步驟2 - 建立`main`函式,並在該函式中建立`sourceFile`和`DestinationFile`作為變數,並將其賦值給特定的檔案。

  • 步驟3 - 在下一步中,使用內建函式`os.Open`開啟原始檔,如果開啟檔案時出現錯誤,則使用錯誤建立panic。

  • 步驟4 - 使用`defer`關鍵字和`close`函式關閉原始檔。

  • 步驟5 - 在此步驟中,使用`os.Create`函式建立目標檔案,如果未建立檔案,則使用錯誤建立panic。

  • 步驟6 - 然後使用`defer`關鍵字和`close`函式關閉目標檔案。

  • 步驟7 - 然後,使用`io.copy`函式將原始檔的內容複製到目標檔案。

  • 步驟8 - 如果出現任何錯誤,則使用錯誤建立panic。

示例

在這個示例中,我們將使用與`os`包關聯的函式。

package main import ( "io" "os" ) func main() { sourceFile := "src.txt" destinationFile := "dst.txt" source, err := os.Open(sourceFile) //open the source file if err != nil { panic(err) } defer source.Close() destination, err := os.Create(destinationFile) //create the destination file if err != nil { panic(err) } defer destination.Close() _, err = io.Copy(destination, source) //copy the contents of source to destination file if err != nil { panic(err) } }

輸出

If the file exists content will be copied to the file successfully but if the file is not present, file not found will be printed on the console.

方法二:使用`io/ioutil`包

在這種方法中,我們將使用`io/ioutil`包函式 - `ioutil.ReadFile`和`ioutil.WriteFile`,前者用於讀取原始檔的內容,而後者用於將內容寫入目標檔案。

語法

Ioutil.ReadFile

此函式在`ioutil`包中可用,用於讀取檔名作為函式輸入的檔案的內容。

ioutil.WriteFile

在Go語言中,`WriteFile`屬於`ioutil`包,包含三個引數:第一個是寫入資料的檔名,第二個是要寫入的資料,第三個是檔案許可權。如果函式成功執行,則資料將寫入檔案。

演算法

  • 步驟1 - 建立`main`包,並在程式中宣告`fmt`(格式化包)、`io/ioutil`包,其中`main`生成可執行程式碼,`fmt`幫助格式化輸入和輸出。

  • 步驟2 - 建立`main`函式,並在該函式中建立變數`sourceFile`和`destinationFile`,並將這些變數賦值給相應的檔案。

  • 步驟3 - 在此步驟中,使用`ioutil.ReadFile`讀取原始檔的內容,並檢查讀取檔案內容時是否出現任何錯誤,列印“錯誤讀取檔案”訊息並返回。

  • 步驟4 - 然後,使用`ioutil.WriteFile`函式將從原始檔讀取的內容寫入目標檔案,在這裡也檢查寫入檔案內容時是否出現任何錯誤,列印失敗訊息並返回。

  • 步驟5 - 最後,如果沒有錯誤,這意味著內容已成功複製到檔案,成功訊息將使用`fmt.Println()`函式列印到控制檯,其中`ln`表示換行。

示例

在這個示例中,將使用`io/ioutil`包的函式來執行程式。

package main import ( "fmt" "io/ioutil" ) func main() { sourceFile := "src.txt" destinationFile := "dst.txt" data, err := ioutil.ReadFile(sourceFile) //read the contents of source file if err != nil { fmt.Println("Error reading file:", err) return } err = ioutil.WriteFile(destinationFile, data, 0644) //write the content to destination file if err != nil { fmt.Println("Error writing file:", err) return } fmt.Println("File copied successfully") //success message will be printed when one file is copied into another }

輸出

If file is present the content of source file will be copied to the destination file and success message will be printed on the console but if the error appears failure message will be printed on the console i.e.

Error reading file: open src.txt: no such file or directory

結論

我們使用兩種方法執行了將一個檔案複製到另一個檔案的程式。在第一種方法中,我們使用了`os`包函式,在第二種方法中,我們使用了`io/ioutil`包函式。

更新於:2023年2月22日

3K+ 次瀏覽

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告