[英]Unexpected output when reading and writing to a text file
我對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();
如果您不在意退貨,可以使用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());
這對您有用嗎?
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;
}
}
}
您仍然必須注意字符編碼!
除非我沒有丟失任何內容,否則看來您的問題是您在每次塊讀取迭代中都覆蓋了輸出文件的現有內容。
您致電:
using (StreamWriter writer = File.CreateText(outputFile))
{
writer.Write(sb.ToString());
}
對於每個ReadBlock迭代。 文件的輸出將僅是讀取的最后一塊數據。
從File.CreateText上的MSDN文檔中:
如果路徑指定的文件不存在,則會創建該文件。 如果該文件確實存在,則其內容將被覆蓋。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.