I have a flat file that has 339276 lines of text with a size of 62.1 MB. I try to read in all the lines, analyze them based on some conditions that I have, and then insert them into the database.
At first I tried using the bufio.Scan () and bufio.Text () loops to get the string, but I ran out of free space. I switched to using bufio.ReadLine / ReadString / ReadByte (I tried each) and had the same problem with each. I did not have enough buffer space.
I tried to use reading and setting the buffer size, but since the document says that it is actually a constant that can be reduced, but not more than 64 * 1024 bytes. Then I tried to use File.ReadAt, where I set the initial mail item and moved it when I brought to each section to no avail. I looked at the following examples and explanations (not an exhaustive list):
Read a text file into an array of lines (and write) How to read the last lines from a large file with Go every 10 seconds reading a file line by line in go
How can I read the entire file (either one at a time, or all at once) into a slice so that I can then do something in lines?
Here is the code I tried:
file, err := os.Open(feedFolder + value) handleError(err) defer file.Close() // fileInfo, _ := file.Stat() var linesInFile []string r := bufio.NewReader(file) for { path, err := r.ReadLine("\n") // 0x0A separator = newline linesInFile = append(linesInFile, path) if err == io.EOF { fmt.Printf("End Of File: %s", err) break } else if err != nil { handleError(err) // if you return error } } fmt.Println("Last Line: ", linesInFile[len(linesInFile)-1])
Here is what I have tried:
var fileSize int64 = fileInfo.Size() fmt.Printf("File Size: %d\t", fileSize) var bufferSize int64 = 1024 * 60 bytes := make([]byte, bufferSize) var fullFile []byte var start int64 = 0 var interationCounter int64 = 1 var currentErr error = nil for currentErr != io.EOF { _, currentErr = file.ReadAt(bytes, st) fullFile = append(fullFile, bytes...) start = (bufferSize * interationCounter) + 1 interationCounter++ } fmt.Printf("Err: %s\n", currentErr) fmt.Printf("fullFile Size: %s\n", len(fullFile)) fmt.Printf("Start: %d", start) var currentLine []string for _, value := range fullFile { if string(value) != "\n" { currentLine = append(currentLine, string(value)) } else { singleLine := strings.Join(currentLine, "") linesInFile = append(linesInFile, singleLine) currentLine = nil } }
I'm at a loss. Either I do not understand how the buffer works, or I do not understand the other. Thanks for reading.