读取和写入文本文件时出现意外输出
本文关键字:意外 输出 文件 文本 读取 | 更新日期: 2023-09-27 18:34:24
我对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,我将非常感谢您的建议。
有几个"陷阱":
-
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);
}
试试这个:
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();
这对你有用吗?
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;
}
}
}
不过,您仍然需要注意字符编码!
如果你不关心卡拉伊恩回报,你可以使用File.ReadAllText
此方法打开一个文件,读取文件的每一行,然后将每一行添加为字符串的元素。然后关闭文件。行定义为字符序列,后跟回车符 ("''r")、换行符 ("'") 或紧跟换行符的回车符。生成的字符串不包含终止回车符和/或换行符。
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());
除非我错过了什么,否则您的问题似乎是您在每次块读取迭代中覆盖了输出文件的现有内容。
你打电话:
using (StreamWriter writer = File.CreateText(outputFile))
{
writer.Write(sb.ToString());
}
对于每个读取块迭代。文件的输出将只是读取的最后一个数据块。
来自 File.CreateText 上的 MSDN 文档:
如果 path 指定的文件不存在,则创建该文件。如果 文件确实存在,其内容被覆盖。