gpt4 book ai didi

c# - 为什么 FileStream 不可写?

转载 作者:行者123 更新时间:2023-11-30 12:08:56 24 4
gpt4 key购买 nike

<分区>

我正在编写一个控制台程序。我得到了可以工作的代码,但我想将其更改为通用函数以启用使用不同的 Stream 源(例如 MemoryStream)。

这是我的程序:

private static void Main(string[] args)
{
using (var fs = new FileStream("f:\\!temp\\marcXmlRecords.mxml", FileMode.Open))
{
var reader = new MarcXmlReader().Read(fs);

foreach (var record in reader)
{
var leader = record.Leader;
var controlFields = record.GetControlFields();
var dataFields = record.GetDataFields();
var allFields = record.GetVariableFields();

using (var fs2 = new FileStream("f:\\!temp\\marcRecords.mrc",
FileMode.OpenOrCreate))
{
using (var writer = new MarcStreamWriter(fs2, "UTF-8"))
{
var wrecord = MarcFactory.Instance.NewRecord();
wrecord.Leader = record.Leader;

foreach (var cfield in controlFields)
{
wrecord.AddVariableField(cfield);
}

foreach (var dfield in dataFields)
{
wrecord.AddVariableField(dfield);
}

foreach (var afield in allFields)
{
wrecord.AddVariableField(afield);
}

writer.Write(wrecord);
}
}
}
}
}

好的,工作正常。现在让我们让它更通用:

internal class Program
{
private static void Main(string[] args)
{
using (var fs = new FileStream("f:\\!temp\\marcXmlRecords.mxml", FileMode.Open))
{
using (var fs2 = new FileStream("f:\\!temp\\marcRecords.mrc",
FileMode.OpenOrCreate))
{
ConvertXmlMarcToMarc(fs, fs2);
}
}
}

private static void ConvertXmlMarcToMarc(Stream fin, Stream fout)
{
var reader = new MarcXmlReader().Read(fin);

foreach (var record in reader)
{
var leader = record.Leader;
var controlFields = record.GetControlFields();
var dataFields = record.GetDataFields();
var allFields = record.GetVariableFields();

using (var writer = new MarcStreamWriter(fout, "UTF-8"))
{
var wrecord = MarcFactory.Instance.NewRecord();
wrecord.Leader = record.Leader;

foreach (var cfield in controlFields)
{
wrecord.AddVariableField(cfield);
}

foreach (var dfield in dataFields)
{
wrecord.AddVariableField(dfield);
}

foreach (var afield in allFields)
{
wrecord.AddVariableField(afield);
}

writer.Write(wrecord);
}
}
}
}

但我在行中遇到异常:using (var writer = new MarcStreamWriter(fs2, "UTF-8")):

An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll
Additional information: Stream was not writable.

堆栈跟踪

at System.IO.BinaryWriter..ctor(Stream output, Encoding encoding, Boolean leaveOpen)
at MARC4J.Net.MarcStreamWriter..ctor(Stream output, String encoding, Boolean allowOversizeRecord)
at MARC4J.Net.MarcStreamWriter..ctor(Stream output, String encoding)
at SummonImport.Program.ConvertXmlMarcToMarc(Stream fin, Stream fout) in F:!!LocalRepository\Libraries_Apps\SummonImport\trunk\release\SummonImport\SummonImport\Program.cs:line 35
at SummonImport.Program.Main(String[] args) in F:!!LocalRepository\Libraries_Apps\SummonImport\trunk\release\SummonImport\SummonImport\Program.cs:line 20
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com