如何创建golang热文件夹只修改一次文件

我有一个程序正在输出文件的情况。为了修复文件,我必须更改一行文本。我不拥有该程序的源代码,因此我必须更改该程序吐出的每个文件,以便其他程序可以正确使用它。

由于该过程的性质,我无法重命名文件或移动它们。

因此,我希望看到任何放入的新文件(通过循环和睡眠很容易)并更改文件,但只执行一次,因为其他进程需要出现并使用此文件。

我在 Go 中编写了许多类似的应用程序,但总是被允许移动文件。


吃鸡游戏
浏览 118回答 1
1回答

动漫人物

下面是我想出的代码: 接受有关如何使用 Seeker 执行此操作的建议,因为我只删除文件中的一行文本。package mainimport (&nbsp; &nbsp; "time"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; "io/ioutil"&nbsp; &nbsp; "os"&nbsp; &nbsp; "bytes"&nbsp; &nbsp; "strings")const dir = "dir00003"func main() {fmt.Println("Running...")//Go into a loop foreverfor {&nbsp; &nbsp; //Wait 60 seconds before taking any action.&nbsp;&nbsp; &nbsp; time.Sleep(60 * time.Second)&nbsp; &nbsp; //Read all of the file data for all files in the directory:&nbsp;&nbsp; &nbsp; files, err := ioutil.ReadDir(dir)&nbsp; &nbsp; if err != nil {fmt.Println("Failed to read transfer folder. There must be a folder named `dir00003`!"); continue}&nbsp; &nbsp; for _, v := range files {&nbsp; &nbsp; &nbsp; &nbsp; //if this is an index file, skip over it as we don't care:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if strings.Contains(v.Name(), "pmi") {continue}&nbsp; &nbsp; &nbsp; &nbsp; //if the file was created within the last 2 minutes, we should check if we need to modify it&nbsp; &nbsp; &nbsp; &nbsp; if time.Now().Sub(v.ModTime()) < (time.Minute * 2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //open the file&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f, err := os.Open(fmt.Sprintf("%s/%s", dir, v.Name()))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if err != nil {fmt.Printf("\tCouldn't open file: %s\n", v.Name()); continue}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; defer f.Close()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //read all of the bytes of the file&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bs, err := ioutil.ReadAll(f)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if err != nil {fmt.Printf("\tCouldn't read bytes from %s\n", v.Name()); continue}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //see if the <program_parameters/> tag is in the file&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b := bytes.Contains(bs, []byte("<program_parameters/>"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //if the tag is in the file, we should replace it, otherwise we move on to the next file&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if b {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //replace the tag with nothing. Only look for the first instance and then abort the process of replacing.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rbs := bytes.Replace(bs, []byte("<program_parameters/>"), []byte(""), 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //close the file so we can delete it.&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f.Close()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //delete the exisint file.&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; os.Remove(fmt.Sprintf("%s/%s", dir, v.Name()))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //create a new file with the same original name:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nf, err := os.Create(fmt.Sprintf("%s/%s", dir, v.Name()))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if err != nil {fmt.Printf("\tFailed to create new file for %s\n", v.Name()); continue}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //write all of the bytes that we have in memory to our new file.&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _, err = nf.Write(rbs)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if err != nil {fmt.Println("Failed to write to new file %s\n", v.Name()); continue}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //close our new file&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nf.Close()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("Modified new file: %s", v.Name())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Printf("\nDone with round\n")}fmt.Println("PROGRAM STOPPED RUNNING!!")return&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go