我正在提取文档并以txt文件格式存储所有数据。 对于提取我正在使用http://www.codeproject.com/Articles/31944/Implementing-a-TextReader-to-extract-various-files此库。 对于文档提取,我使用下面的代码:
public static void ExtractDocument()
{
List<string> lstFiles= new List<string>();
lstFiles.Add("123.txt");
lstFiles.Add("123.doc");
lstFiles.Add("123.docx");
foreach (var fileName in lstFiles)
{
string newFileName = Path.Combine("D://extractFiles//downloadPath", fileName);
string outputFilefiname = "D://extractFiles//vij.txt";
using (var target = new FilterReader(newFileName, 0x1000))
{
target.Init();
const int blockSize = 1024;
var buffer = new Char[blockSize];
var readblockBufferSize = target.ReadBlock(buffer, 0, blockSize);
while (readblockBufferSize > 0)
{
if (buffer[0] != '\0')
{
using (var writer = new StreamWriter(outputFilefiname, true))
{
writer.Write(buffer, 0, readblockBufferSize);
}
readblockBufferSize = target.ReadBlock(buffer, 0, blockSize);
}
else
{
readblockBufferSize = readblockBufferSize - 1; --"may be due to this Line I have lost data"
}
}
}
// Write Delimete.
using (StreamWriter sWriter = new StreamWriter(outputFilefiname, true))
{
sWriter.WriteLine(string.Format("{0} 1111111111--2222222222-333333333-444444444-555555555 {0}", Environment.NewLine));
}
}
}
在此方法中,提取工作正常但我在提取期间丢失了一些数据
else
{
readblockBufferSize = readblockBufferSize - 1; --"may be due to this Line I have lost data"
}
但是当我使用下面的提取代码时:
public static void ExtractDocument()
{
List<string> lstFiles = new List<string>();
lstFiles.Add("123.txt");
lstFiles.Add("123.doc");
lstFiles.Add("123.docx");
foreach (var fileName in lstFiles)
{
string newFileName = Path.Combine("D://extractFiles//downloadPath", fileName);
string outputFilefiname = "D://extractFiles//vij.txt";
using (var target = new FilterReader(newFileName, 0x1000))
{
target.Init();
const int blockSize = 1024;
var buffer = new Char[blockSize];
var readblockBufferSize = target.ReadBlock(buffer, 0, blockSize);
while (target.ReadBlock(buffer, 0, blockSize) > 0)
{
if (buffer[0] != '\0')
{
using (var writer = new StreamWriter(outputFilefiname, true))
{
writer.Write(buffer, 0, readblockBufferSize);
}
}
}
}
// Write Delimete.
using (StreamWriter sWriter = new StreamWriter(outputFilefiname, true))
{
sWriter.WriteLine(string.Format("{0} 1111111111--2222222222-333333333-444444444-555555555 {0}", Environment.NewLine));
}
}
}
在这种情况下,While循环永远不会结束(无限循环)。 我无法分析我在做错误的地方。
更新方法
public ResponseOutput ExtractFile(string outputFilefiname,string downloadedFileLocation,List<string> lstfiles)
{
var responseOutput = new ResponseOutput();
responseOutput.IsSuccessful = false;
int filescount = 0;
if (!Directory.Exists(downloadedFileLocation) || string.IsNullOrWhiteSpace(downloadedFileLocation))
{
return responseOutput;
}
try
{
foreach (var fileName in lstfiles)
{
responseOutput = new ResponseOutput();
responseOutput.IsSuccessful = false;
string finalExtractedFile = Path.Combine(downloadedFileLocation, fileName);
using (var target = new FilterReader(finalExtractedFile, 0x1000))
{
target.Init();
var buffer = new Char[1024];
int blockSize = buffer.Length;
int readBlockBufferSize = 0;
while ((readBlockBufferSize = target.ReadBlock(buffer, 0, blockSize)) > 0)
{
if (buffer[0] != '\0')
{
using (var writer = new StreamWriter(outputFilefiname, true))
{
writer.Write(buffer, 0, readBlockBufferSize);
}
}
}
filescount = filescount + 1;
if (filescount != lstfiles.Count)
{
using (StreamWriter sWriter = new StreamWriter(outputFilefiname, true))
{
sWriter.WriteLine(string.Format("{0} {1} {0}", Environment.NewLine, FileSepratorDelimiter));
}
}
}
if(File.Exists(finalExtractedFile))
{
//File.Delete(finalExtractedFile);
}
}
}
catch (Exception ex)
{
//
}
return responseOutput;
}
请建议我在哪里犯错,或者我需要做些什么来解决这个问题。
答案 0 :(得分:0)
问题在于此代码:
var readblockBufferSize = target.ReadBlock(buffer, 0, blockSize);
while (target.ReadBlock(buffer, 0, blockSize) > 0)
{
....
}
在第一行中,您正在读取一个块并将大小存储在readBlockBufferSize
中。然后你再次阅读 (它会丢弃第一次读取的缓冲区内容),但你不更新readBlockBuffersize
。
代码应如下所示:
int readBlockBufferSize = 0;
while ((readBlockBufferSize = target.ReadBlock(buffer, 0, blockSize)) > 0)
{
if (buffer[0] != '\0')
{
using (var writer = new StreamWriter(outputFilefiname, true))
{
writer.Write(buffer, 0, readblockBufferSize);
}
}
}