HomeSearch

Golang Read Text File: bufio, ioutil.ReadAll Examples

Use bufio, NewScanner, Scan and Text to read the lines of a file. Call ioutil.ReadAll.

File, bufio.

A file contains many lines. In Go we can use the bufio package to read in all the lines in this file inside a loop. With Scan and Text we get a string for each line.

With bufio,

we have many helpful methods for handling text files. Newlines are handled automatically. The classes are easy to use with some practice.

First example.

This program opens a file on the disk. Please change the path argument to os.Open to a file that exists on your computer. Errors are not handled here.

Open: This returns a *File descriptor. We can pass the result of Open() to the bufio.NewScanner method.

NewScanner: This creates a new *Scanner. The File we pass to this method is accessed through its Reader interface.

Scan: This advances to the next part of the file and returns true if there is more data. And text() creates a string from the current line.

Golang program that uses bufio, reads text file package main import ( "bufio" "fmt" "os" ) func main() { // Open the file. f, _ := os.Open("C:\\programs\\file.txt") // Create a new Scanner for the file. scanner := bufio.NewScanner(f) // Loop over all lines in the file and print them. for scanner.Scan() { line := scanner.Text() fmt.Println(line) } } Output Carrot Bird Fish Turnip Contents of file.txt Carrot Bird Fish Turnip

ScanWords.

A file sometimes contains words. We can get each word separately as a string with the Scan() and Text() methods. First we must call Split.

Tip: The Split method here just sets the splitting method for the scanner. It influences the behavior of Scan().

Golang program that uses bufio.ScanWords, gets words package main import ( "bufio" "fmt" "os" ) func main() { f, _ := os.Open("C:\\programs\\file.txt") scanner := bufio.NewScanner(f) // Set the Split method to ScanWords. scanner.Split(bufio.ScanWords) // Scan all words from the file. for scanner.Scan() { line := scanner.Text() fmt.Println(line) } } Output a commodius vicus of recirculation Contents of file.txt a commodius vicus of recirculation

Read entire file.

With ioutil.ReadAll we can get the entire contents of a file in a byte slice or a string. We must import "io/ioutil" and then call ioutil.ReadAll on a reader.

NewReader: We use bufio.NewReader to create a buffered text file reader. We can then pass this to ReadAll.

ReadAll: This func returns a byte slice. We can convert the byte slice into a string—this makes it easier to display.

Golang program that uses ioutil.ReadAll package main import ( "bufio" "fmt" "io/ioutil" "os" ) func main() { // Open a file. f, _ := os.Open("C:\\programs\\file.txt") // Use bufio.NewReader to get a Reader. // ... Then use ioutil.ReadAll to read the entire content. reader := bufio.NewReader(f) content, _ := ioutil.ReadAll(reader) // File content. fmt.Println(string(content)) } Output This is an example file. With two lines. Contents of file.txt: This is an example file. With two lines.

Write file.

With ioutil, we can call WriteFile to write a byte slice to a file. For a text file, we can also use NewWriter and WriteString.WriteFile

Rename file.

It is possible to use the os package to move a file from one location to another. The os.Rename func is available for this use.os.Rename

Delete file.

With the os.Remove() func we can delete a file. It is possible to use os.Remove in a for-loop to delete many files at once.os.Remove

Compression.

With the "compress" package we can compress byte data and strings to gzip files. We use the NewWriter method to begin writing compressed data.compress

CSV files.

With the "encoding/csv" package we can parse fields in a CSV file without using Split() calls. This makes reading comma-separated values easier.csv

In file handling,

errors are common and often unavoidable. We use the error return value from os.Open, and the recover() method, to handle these events.
Home
Dot Net Perls
© 2007-2019 Sam Allen. All rights reserved. Written by Sam Allen, info@dotnetperls.com.