我对C#中的文件有点新,我遇到了问题。从文件读取并复制到另一个文件时,最后一块文本没有被写入。以下是我的代码:
StringBuilder sb = new StringBuilder(8192);
string fileName = "C:...rest of path...inputFile.txt";
string outputFile = "C:...rest of path...outputFile.txt";
using (StreamReader reader = File.OpenText(fileName))
{
char[] buffer = new char[8192];
while ((reader.ReadBlock(buffer, 0, buffer.Length)) != 0)
{
foreach (char c in buffer)
{
//do some function on char c...
sb.Append(c);
}
using (StreamWriter writer = File.CreateText(outputFile))
{
writer.Write(sb.ToString());
}
}
}
我的目标是以缓冲方式读取和写入文本文件。 Java中我将以下列方式实现的东西:
public void encrypt(File inputFile, File outputFile) throws IOException
{
BufferedReader infromfile = null;
BufferedWriter outtofile = null;
try
{
String key = getKeyfromFile(keyFile);
if (key != null)
{
infromfile = new BufferedReader(new FileReader(inputFile));
outtofile = new BufferedWriter(new FileWriter(outputFile));
char[] buffer = new char[8192];
while ((infromfile.read(buffer, 0, buffer.length)) != -1)
{
String temptext = String.valueOf(buffer);
//some changes to temptext are done
outtofile.write(temptext);
}
}
}
catch (FileNotFoundException exc)
{
} // and all other possible exceptions
}
你能帮我辨别出问题的根源吗?
如果您认为可能有更好的方法来实现文本文件的缓冲i / o,我将非常感谢您的建议。
答案 0 :(得分:1)
有几个“陷阱”:
c
无法更改(它是foreach迭代变量),您需要复制它才能在编写之前进行处理ReadBlock
用字符填充它会使你的输出变脏像这样更改代码看起来很有效:
//extracted from your code
foreach (char c in buffer)
{
if (c == (char)0) break; //GOTCHA #2: maybe you don't want NULL (ascii 0) characters in your output
char d = c; //GOTCHA #1: you can't change 'c'
// d = SomeProcessingHere();
sb.Append(d);
}
答案 1 :(得分:1)
试试这个:
string fileName = @"";
string outputfile = @"";
StreamReader reader = File.OpenText(fileName);
string texto = reader.ReadToEnd();
StreamWriter writer = new StreamWriter(outputfile);
writer.Write(texto);
writer.Flush();
writer.Close();
答案 2 :(得分:0)
如果你不关心回车,你可以使用File.ReadAllText
此方法打开一个文件,读取文件的每一行,然后将每一行添加为字符串的元素。然后它关闭文件。一行被定义为一系列字符,后跟一个回车符('\ r'),一个换行符('\ n')或一个回车符后面紧跟一个换行符。结果字符串不包含终止回车符和/或换行符。
StringBuilder sb = new StringBuilder(8192);
string fileName = "C:...rest of path...inputFile.txt";
string outputFile = "C:...rest of path...outputFile.txt";
// Open the file to read from.
string readText = File.ReadAllText(fileName );
foreach (char c in readText)
{
// do something to c
sb.Append(new_c);
}
// This text is added only once to the file, overwrite it if it exists
File.WriteAllText(outputFile, sb.ToString());
答案 3 :(得分:0)
这对你有用吗?
using (StreamReader reader = File.OpenText(fileName))
{
char[] buffer = new char[8192];
bool eof = false;
while (!eof)
{
int numBytes = (reader.ReadBlock(buffer, 0, buffer.Length));
if (numBytes>0)
{
using (StreamWriter writer = File.CreateText(outputFile))
{
writer.Write(buffer, 0, numBytes);
}
} else {
eof = true;
}
}
}
你仍然需要处理字符编码!
答案 4 :(得分:0)
除非我遗漏了某些内容,否则您的问题似乎是您在每次blockread迭代时都覆盖了输出文件的现有内容。
你打电话:
using (StreamWriter writer = File.CreateText(outputFile))
{
writer.Write(sb.ToString());
}
每次ReadBlock迭代。该文件的输出只是读取的最后一块数据。
来自File.CreateText上的MSDN文档:
如果path指定的文件不存在,则创建该文件。如果 文件确实存在,其内容被覆盖。