如果我写以下语句。
fileStream.Close();
reader.Close();
writer.Close();
Run Code Online (Sandbox Code Playgroud)
reader.Close() 语句执行成功。但我收到错误“无法打开关闭的文件”。在第三条语句 writer.Close()
如果我写
fileStream.Close();
writer.Close();
reader.Close();
Run Code Online (Sandbox Code Playgroud)
第二条语句,即 writer.Close() 本身抛出相同的异常。
有人有想法吗?
我正在使用这段代码从文件中读取,但出现错误“无法创建抽象类或接口“System.IO.TextReader”的实例”
using (FileStream fileStream = File.Open(fileName, FileMode.Open, FileAccess.Read))
using(TextReader reader = new TextReader(fileStream))//error
{
...
}
Run Code Online (Sandbox Code Playgroud) 我想将字节数组附加到现有文件中。它必须位于文件的末尾。我已经可以设法在文件的开头写入。(感谢 stackoverflow ;))。
代码:
public bool ByteArrayToFile(string _FileName, byte[] _ByteArray)
{
try
{
// Open file for reading
System.IO.FileStream _FileStream =
new System.IO.FileStream(_FileName, System.IO.FileMode.Create,
System.IO.FileAccess.Write);
// Writes a block of bytes to this stream using data from
// a byte array.
_FileStream.Write(_ByteArray, 0, _ByteArray.Length);
// close file stream
_FileStream.Close();
return true;
}
catch (Exception _Exception)
{
// Error
Console.WriteLine("Exception caught in process: {0}",
_Exception.ToString());
}
// error occured, return false
return false;
Run Code Online (Sandbox Code Playgroud)
}
从这里得到的:
但我需要它在文件末尾
提前致谢。
找到了解决方案: …
有一段时间不需要在这里发帖了,但我在实现文件流时遇到了问题。将字符串写入文件流时,结果文本文件在每个字符之间插入了额外的空格
所以当运行这个方法时:
Function TDBImportStructures.SaveIVDataToFile(const AMeasurementType: integer;
IVDataRecordList: TIV; ExportFileName, LogFileName: String;
var ProgressInfo: TProgressInfo): Boolean; // AM
var
TempString: unicodestring;
ExportLogfile, OutputFile: TFileStream;
begin
ExportLogfile := TFileStream.Create(LogFileName, fmCreate);
TempString :=
'FileUploadTimestamp, Filename, MeasurementTimestamp, SerialNumber, DeviceID, PVInstallID,'
+ #13#10;
ExportLogfile.WriteBuffer(TempString[1], Length(TempString) * SizeOf(Char));
ExportLogfile.Free;
OutputFile := TFileStream.Create(ExportFileName, fmCreate);
TempString :=
'measurementdatetime,closestfiveseconddatetime,closesttenminutedatetime,deviceid,'
+ 'measuredmoduletemperature,moduletemperature,isc,voc,ff,impp,vmpp,iscslope,vocslope,'
+ 'pvinstallid,numivpoints,errorcode' + #13#10;
OutputFile.WriteBuffer(TempString[1], Length(TempString) * SizeOf(Char));
OutputFile.Free;
end;
Run Code Online (Sandbox Code Playgroud)
(这是一种精简的测试方法,仅写入标题)。“OutPutFile”生成的 csv 文件读取
'measuredmoduletempera ture等在写字板中查看时,但不是在excel、记事本等中查看。我猜测它的SizeOf(Char)语句在unicode上下文中是错误的,但我不确定什么是正确的插入此处。“ExportLogfile”似乎工作正常,但“OutPutFile”不行
从我在其他地方读到的内容来看,这是问题所在,而不是写字板,请参阅http://social.msdn.microsoft.com/Forums/en-US/7e040fd1-f399-4fb1-b700-9e7cc6117cc4/ unicode-to-files-and-console-vs-notepad-wordpad-word-etc?forum=vcgeneral
大家有什么建议吗?非常感谢,布莱恩
我需要创建一个文件,其中某些部分是字符串(utf-8),而某些部分是字节。
我用StreamWriter和修改了几个小时BinaryWriter,但这是唯一有效的:
using (var stream = new FileStream(_caminho, FileMode.Create, FileAccess.Write))
{
using (var writer = new StreamWriter(stream))
{
writer.Write(myString);
}
}
using (var stream = new FileStream(_caminho, FileMode.Append, FileAccess.Write))
{
using (var writer = new BinaryWriter(stream))
{
writer.Write(oneSingleByte);
}
}
Run Code Online (Sandbox Code Playgroud)
问题是我必须关闭 FileStream 并打开另一个文件流才能写入单个字节,因为该方法要么BinaryStream.Write(string)在前面添加一个“长度”字段(在我的情况下是不需要的),要么对StreamWriter.Write(byte)字节值进行编码而不是实际直接写入。
我的问题是:是否可以使用另一个类,以便我只能创建一次 FileStream,并依次写入我的字符串和字节?
我试图在比赛后打印接下来的 3 行
例如输入是:
Testing
Result
test1 : 12345
test2 : 23453
test3 : 2345454
Run Code Online (Sandbox Code Playgroud)
所以我试图在文件中搜索“结果”字符串并从中打印下 3 行:
输出将是:-
test1 : 12345
test2 : 23453
test3 : 2345454
Run Code Online (Sandbox Code Playgroud)
我的代码是:
with open(filename, 'r+') as f:
for line in f:
print line
if "Benchmark Results" in f:
print f
print next(f)
Run Code Online (Sandbox Code Playgroud)
它只给我输出:
testing
Run Code Online (Sandbox Code Playgroud)
我如何获得我想要的输出,请帮忙
我的目标是将 PDF 文件流返回给客户端。
所以在 WCF 方面我有:
public interface IPersonalPropertyService
{
[OperationContract]
Stream GetQuotation();
}
public class PersonalPropertyService : IPersonalPropertyService
{
public Stream GetQuotation()
{
var filePath = HostingEnvironment.ApplicationPhysicalPath + @"Quotation.pdf";
var fileInfo = new FileInfo(filePath);
// check if exists
if (!fileInfo.Exists)
throw new FileNotFoundException("File not found");
FileStream stm = File.Open(filePath, FileMode.Open);
WebOperationContext.Current.OutgoingResponse.ContentType = "application/pdf";
return stm;
}
}
Run Code Online (Sandbox Code Playgroud)
配置部分如下:
<system.serviceModel>
<client>
<endpoint
binding="basicHttpBinding"
bindingConfiguration="StreamedHttp"
contract="IPersonalPropertyService" >
</endpoint>
</client>
<bindings>
<basicHttpBinding>
<binding name="StreamedHttp" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"
transferMode="Streamed">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" …Run Code Online (Sandbox Code Playgroud) 我从 xml 文件中读取 UTF8 内容,然后需要保存并按需重新加载。我正在从 AssignFile/Writeln/Readln 转换为 David Heffernan 的缓冲流:缓冲文件(用于更快的磁盘访问)
我有简单的新 WriteLn 和 ReadLn 过程,WriteLn 可以工作,但我不能让 ReadLn 工作。
我对 ReadLn 的概念是处理:
新的 WriteLn 过程:
{ * New WriteLn * }
procedure TForm1.Button2Click(Sender: TObject);
var FileOut: TWriteCachedFileStream;
vText: string;
vUTF8Text: RawByteString;
begin
FileOut := TWriteCachedFileStream.Create('c:\tmp\file.txt');
try
vText := 'Delphi';
vUTF8Text := Utf8Encode(vText + sLineBreak);
FileOut.WriteBuffer(PAnsichar(vUTF8Text)^, Length(vUTF8Text));
vText := 'VB??';
vUTF8Text := Utf8Encode(vText + sLineBreak);
FileOut.WriteBuffer(PAnsichar(vUTF8Text)^, Length(vUTF8Text));
vText := 'Java??';
vUTF8Text := Utf8Encode(vText + …Run Code Online (Sandbox Code Playgroud) 我在这里可能遗漏了一些明显的东西,但我似乎无法在我的 FileStream 读取中设置编码。这是代码:
FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
using (fs)
{
byte[] buffer = new byte[chunk];
fs.Seek(chunk, SeekOrigin.Begin);
int bytesRead = fs.Read(buffer, 0, chunk);
while (bytesRead > 0)
{
ProcessChunk(buffer, bytesRead, database, id);
bytesRead = fs.Read(buffer, 0, chunk);
}
}
fs.Close();
Run Code Online (Sandbox Code Playgroud)
ProcessChunk 将读取的值保存到对象,然后将其序列化为 XML,但读取的字符显示错误。编码需要是 1250。我还没有看到将编码添加到 FileStream 的选项。我在这里缺少什么?
我正在寻找一种可以将 System.IO.Stream 转换为 byte[] 的 C# 语言解决方案。我已经尝试了下面的代码,但我收到的字节 [] 为空。有人可以指导我从下面的代码中缺少什么吗?我从 Alfresco Web 服务接收,除非保存到临时位置,否则我无法读取文件。
private static byte[] ReadFile(Stream fileStream)
{
byte[] bytes = new byte[fileStream.Length];
fileStream.Read(bytes, 0, Convert.ToInt32(fileStream.Length));
fileStream.Close();
return bytes;
//using (MemoryStream ms = new MemoryStream())
//{
// int read;
// while ((read = fileStream.Read(bytes, 0, bytes.Length)) > 0)
// {
// fileStream.CopyTo(ms);
// }
// return ms.ToArray();
//}
}
Run Code Online (Sandbox Code Playgroud) filestream ×10
c# ×7
delphi ×2
file ×2
streamwriter ×2
alfresco ×1
binarywriter ×1
byte ×1
delphi-xe7 ×1
encoding ×1
file-io ×1
filewriter ×1
io ×1
offset ×1
pascal ×1
pdf ×1
python ×1
python-3.x ×1
streamreader ×1
tfilestream ×1
wcf ×1