I have a byte array of marshalled XML, if I write it to a file using the os library:
fh, _ := os.OpenFile("filename", os.O_CREATE, 0644)
_, err := fh.Write(XMLByteArray)
I get a bunch of junk at the end of the file as if it was a bad write:
<project version="4">
<component name="test">
<option name="urls">
<list></list>
</option>
</component>
</project> </option>
</component>
</project>on>
</component>
</project>
If I write it with io/ioutil library like this:
err = ioutil.WriteFile("filename", XMLByteArray, 0644)
if err != nil {
log.Fatal(err)
}
I get proper XML:
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="test">
<option name="urls">
<list></list>
</option>
</component>
</project>
Here's the part I really don't understand. This file is the result of a dynamic path generation, and is the configuration for IntelliJ. If I use os.Write()
and then properly close the file handler, IntelliJ reads the file immediately, but errors out because the XML is messed up. If I write the file with ioutil.WriteFile()
, the file looks correct, but IntelliJ isn't recognising the file to exist.
So my questions are:
- What is the difference between
os.Write()
andioutil.WriteFile()
? - Why is that difference causing the byte array to be written differently?