F#: DataContractJsonSerializer.WriteObject 方法
F#: DataContractJsonSerializer.WriteObject method
我是编程新手,F# 是我的第一语言。
以下是我的代码的相关部分:
let internal saveJsonToFile<'t> (someObject:'t) (filePath: string) =
use fileStream = new FileStream(filePath, FileMode.OpenOrCreate)
(new DataContractJsonSerializer(typeof<'t>)).WriteObject(fileStream, someObject)
let dummyFighter1 = { id = 1; name = "Dummy1"; location = "Here"; nationality = "Somalia"; heightInMeters = 2.0; weightInKgs = 220.0; weightClass = "Too fat"}
let dummyFighter2 = { id = 2; name = "Dummy2"; location = "There"; nationality = "Afghanistan"; heightInMeters = 1.8; weightInKgs = 80.0; weightClass = "Just Nice"}
let filePath = @"G:\User\Fighters.json"
saveJsonToFile dummyFighter1 filePath
saveJsonToFile dummyFighter2 filePath
当我运行"saveJsonToFile dummyFighter1 filePath"时,信息保存成功。我的问题是:一旦我 运行 "saveJsonToFile dummyFighter2 filePath",它会立即替换文件中已有的所有内容,即所有关于 dummyFighter1.
的信息
我应该做哪些更改才能将有关 dummyFighter2 的信息附加到文件中,而不是替换有关 dummyFighter1 的信息?
将打开文件的方式设置 FileMode.OpenOrCreate
更改为 FileMode.Append
。附加意味着 "create or append" :
use fileStream = new FileStream(filePath, FileMode.Append)
来自 MSDN (https://msdn.microsoft.com/fr-fr/library/system.io.filemode%28v=vs.110%29.aspx) :
FileMode.Append opens the file if it exists and seeks to the end of the file, or
creates a new file. This requires FileIOPermissionAccess.Append
permission. FileMode.Append can be used only in conjunction with
FileAccess.Write. Trying to seek to a position before the end of the
file throws an IOException exception, and any attempt to read fails
and throws a NotSupportedException exception.
我是编程新手,F# 是我的第一语言。
以下是我的代码的相关部分:
let internal saveJsonToFile<'t> (someObject:'t) (filePath: string) =
use fileStream = new FileStream(filePath, FileMode.OpenOrCreate)
(new DataContractJsonSerializer(typeof<'t>)).WriteObject(fileStream, someObject)
let dummyFighter1 = { id = 1; name = "Dummy1"; location = "Here"; nationality = "Somalia"; heightInMeters = 2.0; weightInKgs = 220.0; weightClass = "Too fat"}
let dummyFighter2 = { id = 2; name = "Dummy2"; location = "There"; nationality = "Afghanistan"; heightInMeters = 1.8; weightInKgs = 80.0; weightClass = "Just Nice"}
let filePath = @"G:\User\Fighters.json"
saveJsonToFile dummyFighter1 filePath
saveJsonToFile dummyFighter2 filePath
当我运行"saveJsonToFile dummyFighter1 filePath"时,信息保存成功。我的问题是:一旦我 运行 "saveJsonToFile dummyFighter2 filePath",它会立即替换文件中已有的所有内容,即所有关于 dummyFighter1.
的信息我应该做哪些更改才能将有关 dummyFighter2 的信息附加到文件中,而不是替换有关 dummyFighter1 的信息?
将打开文件的方式设置 FileMode.OpenOrCreate
更改为 FileMode.Append
。附加意味着 "create or append" :
use fileStream = new FileStream(filePath, FileMode.Append)
来自 MSDN (https://msdn.microsoft.com/fr-fr/library/system.io.filemode%28v=vs.110%29.aspx) :
FileMode.Append opens the file if it exists and seeks to the end of the file, or creates a new file. This requires FileIOPermissionAccess.Append permission. FileMode.Append can be used only in conjunction with FileAccess.Write. Trying to seek to a position before the end of the file throws an IOException exception, and any attempt to read fails and throws a NotSupportedException exception.