我正在编写一个控制台程序。我得到了代码,但我想将其更改为通用函数以启用不同的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"))
:
未处理的类型' System.ArgumentException'发生在mscorlib.dll中 附加信息:Stream不可写。
堆栈跟踪
at System.IO.BinaryWriter..ctor(Stream output,Encoding encoding,Boolean leaveOpen)
在MARC4J.Net.MarcStreamWriter..ctor(流输出,字符串编码,布尔allowOversizeRecord)
在MARC4J.Net.MarcStreamWriter..ctor(流输出,字符串编码)
在F:!! LocalRepository \ Libraries_Apps \ SummonImport \ trunk \ release \ SummonImport \ SummonImport \ Program.cs中的SummonImport.Program.ConvertXmlMarcToMarc(Stream fin,Stream fout):第35行
在F:!! LocalRepository \ Libraries_Apps \ SummonImport \ trunk \ release \ SummonImport \ SummonImport \ Program.cs中的SummonImport.Program.Main(String [] args):第20行
在System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,String [] args)
在System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args)
在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)
在System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean preserveSyncCtx)
在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,Object状态,Boolean preserveSyncCtx)
在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态)
在System.Threading.ThreadHelper.ThreadStart()
答案 0 :(得分:2)
在第一个示例中,您将在每个循环上打开和关闭文件。
在你的第二个例子中,你没有重新打开文件,可能是当编写器被处理时它正在关闭底层流。例外情况将在第二次循环中发生。
尝试移动using语句来包装for循环,它应该可以工作
答案 1 :(得分:1)
您需要FileAccess
个using (var fs2 = new FileStream("f:\\!temp\\marcRecords.mrc", FileMode.OpenOrCreate, FileAccess.Write))
参数:
FileMode.OpenOrCreate
<link rel="stylesheet" href="css/layout.css">
本身并不足以让它成为可写的:
指定操作系统应该打开文件(如果存在);否则,应创建一个新文件。如果使用FileAccess.Read打开文件,则需要FileIOPermissionAccess.Read权限。如果文件访问是FileAccess.Write,则需要FileIOPermissionAccess.Write权限。如果使用FileAccess.ReadWrite打开文件,则需要FileIOPermissionAccess.Read和FileIOPermissionAccess.Write权限。