go read text file into string array

http://stackoverflow.com/questions/5884154/golang-read-text-file-into-string-array-and-write

方法一

 1 package main
 2
 3 import (
 4   "bufio"
 5   "fmt"
 6   "log"
 7   "os"
 8 )
 9
10 // readLines reads a whole file into memory
11 // and returns a slice of its lines.
12 func readLines(path string) ([]string, error) {
13   file, err := os.Open(path)
14   if err != nil {
15     return nil, err
16   }
17   defer file.Close()
18
19   var lines []string
20   scanner := bufio.NewScanner(file)
21   for scanner.Scan() {
22     lines = append(lines, scanner.Text())
23   }
24   return lines, scanner.Err()
25 }
26
27 // writeLines writes the lines to the given file.
28 func writeLines(lines []string, path string) error {
29   file, err := os.Create(path)
30   if err != nil {
31     return err
32   }
33   defer file.Close()
34
35   w := bufio.NewWriter(file)
36   for _, line := range lines {
37     fmt.Fprintln(w, line)
38   }
39
40 }

方法二(比较简洁,但文件不能太大)

1 content, err := ioutil.ReadFile(filename)
2 if err != nil {
3     //Do something
4 }
5 lines := strings.Split(string(content), "\n")
时间: 2024-08-04 15:58:47

go read text file into string array的相关文章

How to find a specific string in a text file?

How to find a specific string in a text file? how i did it: i store the whole contents of the .txt file in a single string, close the file, and then, split the string, based on searching a specific character: a | bar. text file test.txt: helloooooooo

create feature from text file

'''---------------------------------------------------------------------------------- Tool Name: CreateFeaturesFromTextFile Source Name: CreateFeaturesFromTextFile.py Version: ArcGIS 9.1 Author: Environmental Systems Research Institute Inc. Required

unity, read text file

using System.IO; //test read txt        //Resources.Load(...) loads an asset stored at path in a Resources folder.        //ref: http://docs.unity3d.com/Manual/class-TextAsset.html        //ref: http://forum.unity3d.com/threads/read-text-file-that-is

Binary file to C array

1 /******************************************************************************** 2 * Binary file to C array 3 * 说明: 4 * 由于工作中需要将bmp文件数据转换成C数组,于是写了这个工具(bin2c),代码如你 5 * 所见,只有看上去不多的几行. 6 * 7 * 2015-4-20 周一 阴 深圳 南山 西丽平山村 曾剑锋 8 ************************

shell脚本执行时报"bad interpreter: Text file busy"的解决方法

在执行一个shell脚本时,遇到了"-bash: ./killSession.sh: /bin/bash: bad interpreter: Text file busy"错误提示,如下所示: [[email protected] bin]$ ./killSession.sh      -bash: ./killSession.sh: /bin/bash: bad interpreter: Text file busy 此时只需要在#!/bin/bash,加一空格#! /bin/bas

"text"和new String("text")的区别

转自:What is the difference between "text" and new String("text")? new String("text"); explicitly creates a new and referentially distinct instance of a Stringobject; String s = "text"; may reuse an instance from the 

Writing Text File From A Tabular Block In Oracle Forms

The example given below for writing text file or CSV using Text_IO package from a tabular block in Oracle Forms. Suppose there is a tabular grid data block "Job_History" in your forms and you want to write a CSV on click of a button by reading w

SharePoint 2010: Export User Profile Properties to a Text File or Excel using PowerShell

导出到txt [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server") [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.UserProfiles") [void][System.Reflection.Assembly]::LoadWithParti

【转】shell脚本执行时报"bad interpreter: Text file busy"的解决方法

1)问题现象: 在ubuntu下执行以下脚本( while_count),报错: -bash: ./while_count: /bin/bash: bad interpreter: Text file busy 2)问题原因: This happens because the script file is open for writing, possibly by a rogue process which has not terminated. 3)解决办法: Solution: Check