如何在文件中写入特定位置的内容

huangapple go评论72阅读模式
英文:

How to write to a specific location in a file

问题

我正在编写一个命令行工具来帮助我构建项目。我需要能够将一行文本添加到文件的特定位置。以下是示例:

我有一个名为 routes.js 的文件:

router.map({
  '/home':{
    name: 'home',
    component: Home
  },
  '/about':{
    name: 'about',
    component: About
  },
  '/quote':{
    name: 'quote',
    component: Quote
  }
})

现在我想运行一个命令来创建一个新的路由,比如 my-cli generate route ExampleRoute

我希望它写入的路由如下所示:

router.map({
    '/home':{
    name: 'home',
    component: Home
  },
    '/about':{
    name: 'about',
    component: About
  },
    '/quote':{
    name: 'quote',
    component: Quote
  },
    '/example-route':{
    name: 'example-route',
    component: ExampleRoute
  }
})

追加到文件底部很容易,但如何写入到特定位置呢?

英文:

I'm writing a command line tool to help me scaffold my projects. I need to be able to add a line of text to a file but to a specific location. Here is the example:

I have this routes.js file:

router.map({
  '/home':{
    name: 'home',
    component: Home
  },
  '/about':{
    name: 'about',
    component: About
  },
  '/quote':{
    name: 'quote',
    component: Quote
  }
})

Now I want to run a command to create a new route so my-cli generate route ExampleRoute

And I would like it to write the route like so:

router.map({
    '/home':{
    name: 'home',
    component: Home
  },
    '/about':{
    name: 'about',
    component: About
  },
    '/quote':{
    name: 'quote',
    component: Quote
  },
    '/example-route':{
    name: 'example-route',
    component: ExampleRoute
  }
})

Appending to the bottom of a file is easy but how do I write to a specific location?

答案1

得分: 0

对于像这样的小文件,最好将文件读入内存,进行修改,然后再写回文件。

一种可能性是将文件读入字符串列表中,每行一个字符串。然后将添加的行插入到列表中,然后将列表写回文件。

另一种可能性是将文件解析为映射,将新元素插入到映射中,然后按所需格式将映射的内容写回文件。

或者,如果您始终希望将新文本插入到文件末尾的固定偏移量处,您可以将其读入字符串中,并在末尾插入新文本,如下所示:

package main

import (
	"io/ioutil"
	"log"
)

const textToInsert = `
'/example-route':{
	name: 'example-route',
	component: ExampleRoute
  }
})

func main() {
	original, err := ioutil.ReadFile("routes.js")
	if err != nil {
		log.Fatal(err)
	}

	// 用textToInsert替换最后4个字符
	modified := append(original[0:len(original)-4], []byte(textToInsert)...)

	err = ioutil.WriteFile("routes.js", modified, 0644)
	if err != nil {
		log.Fatal(err)
	}
}
英文:

For small files like this it is best to read the file into memory, modify it, and write it back out again.

One possibility is to read it in to a list of strings, one per line. Then insert the added lines into the list. Then write out the list back to the file.

Another possibility would be to parse the file into a map, insert the new elements into the map, and then write the contents of the map out to the file in the required format.

Alternatively if you always want to insert the new text at a fixed offset from the end of the file, you could read into a string, and insert your new text at the end, like so:

package main

import (
    "io/ioutil"
    "log"
)

const textToInsert = `,
'/example-route':{
name: 'example-route',
component: ExampleRoute
  }
})
`

func main() {
    original, err := ioutil.ReadFile("routes.js")
    if err != nil {
	    log.Fatal(err)
    }

    // replace last 4 characters with textToInsert
    modified := append(original[0:len(original)-4], []byte(textToInsert)...)

    err = ioutil.WriteFile("routes.js", modified, 0644)
    if err != nil {
	    log.Fatal(err)
    }
}

huangapple
  • 本文由 发表于 2016年3月5日 00:26:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/35801074.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定