使用C#将文本添加到文件开头的最佳方法是什么?
我找不到一种简单的方法来做到这一点,但想出了几个解决办法。
打开新文件,编写我想要添加的文本,将旧文件中的文本追加到新文件的末尾。
由于我要添加的文本应该少于200个字符,我以为我可以在文件的开头添加空格字符,然后用我想要添加的文本覆盖空白区域
有没有其他人遇到过这个问题,如果有的话,你做了什么?
答案 0 :(得分:28)
这适用于我,但适用于小文件。可能这不是一个非常好的解决方案。
string currentContent = String.Empty;
if (File.Exists(filePath))
{
currentContent = File.ReadAllText(filePath);
}
File.WriteAllText(filePath, newContent + currentContent );
答案 1 :(得分:12)
添加到文件的开头(前置而不是附加)通常不是受支持的操作。你的#1选项很好。如果你不能写一个临时文件,你可以将整个文件拉到内存中,将数据预先渲染到字节数组然后将其覆盖回来(这只有在你的文件很小而你不需要的时候才真正可行因为在不使用副本的情况下预先放置数组并不一定容易。或者
答案 2 :(得分:2)
我认为最好的方法是创建临时文件。添加文本然后读取原始文件的内容,将其添加到临时文件中。然后,您可以使用临时文件覆盖原始文件。
答案 3 :(得分:2)
以下算法可以非常轻松地解决问题,对于任何大小的文件都是最有效的,包括非常大的文本文件:
string outPutFile = @"C:\Output.txt";
string result = "Some new string" + DateTime.Now.ToString() + Environment.NewLine;
StringBuilder currentContent = new StringBuilder();
List<string> rawList = File.ReadAllLines(outPutFile).ToList();
foreach (var item in rawList) {
currentContent.Append(item + Environment.NewLine);
}
File.WriteAllText(outPutFile, result + currentContent.ToString());
答案 4 :(得分:2)
使用两个流,您可以在适当的位置执行此操作,但请记住,每次添加时仍会循环遍历整个文件
using System;
using System.IO;
using System.Text;
namespace FilePrepender
{
public class FilePrepender
{
private string file=null;
public FilePrepender(string filePath)
{
file = filePath;
}
public void prependline(string line)
{
prepend(line + Environment.NewLine);
}
private void shiftSection(byte[] chunk,FileStream readStream,FileStream writeStream)
{
long initialOffsetRead = readStream.Position;
long initialOffsetWrite= writeStream.Position;
int offset = 0;
int remaining = chunk.Length;
do//ensure that the entire chunk length gets read and shifted
{
int read = readStream.Read(chunk, offset, remaining);
offset += read;
remaining -= read;
} while (remaining > 0);
writeStream.Write(chunk, 0, chunk.Length);
writeStream.Seek(initialOffsetWrite, SeekOrigin.Begin);
readStream.Seek(initialOffsetRead, SeekOrigin.Begin);
}
public void prepend(string text)
{
byte[] bytes = Encoding.Default.GetBytes(text);
byte[] chunk = new byte[bytes.Length];
using (FileStream readStream = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using(FileStream writeStream = File.Open(file, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
{
readStream.Seek(0, SeekOrigin.End);//seek chunk.Length past the end of the file
writeStream.Seek(chunk.Length, SeekOrigin.End);//which lets the loop run without special cases
long size = readStream.Position;
//while there's a whole chunks worth above the read head, shift the file contents down from the end
while(readStream.Position - chunk.Length >= 0)
{
readStream.Seek(-chunk.Length, SeekOrigin.Current);
writeStream.Seek(-chunk.Length, SeekOrigin.Current);
shiftSection(chunk, readStream, writeStream);
}
//clean up the remaining shift for the bytes that don't fit in size%chunk.Length
readStream.Seek(0, SeekOrigin.Begin);
writeStream.Seek(Math.Min(size, chunk.Length), SeekOrigin.Begin);
shiftSection(chunk, readStream, writeStream);
//finally, write the text you want to prepend
writeStream.Seek(0,SeekOrigin.Begin);
writeStream.Write(bytes, 0, bytes.Length);
}
}
}
}
}
答案 5 :(得分:1)
您应该可以在不打开新文件的情况下执行此操作。使用以下File方法:
public static FileStream Open(
string path,
FileMode mode,
FileAccess access
)
确保指定FileAccess.ReadWrite。
使用从File.Open返回的FileStream,将所有现有数据读入内存。然后将指针重置为文件的开头,写入新数据,然后写入现有数据。
(如果文件很大和/或你怀疑使用太多内存,你可以在不必将整个文件读入内存的情况下执行此操作,但实现该文件仍然是读者的练习。)< / p>
答案 6 :(得分:1)
// The file we'll prepend to
string filePath = path + "\\log.log";
// A temp file we'll write to
string tempFilePath = path + "\\temp.log";
// 1) Write your prepended contents to a temp file.
using (var writer = new StreamWriter(tempFilePath, false))
{
// Write whatever you want to prepend
writer.WriteLine("Hi");
}
// 2) Use stream lib methods to append the original contents to the Temp
// file.
using (var oldFile = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read))
{
using (var tempFile = new FileStream(tempFilePath, FileMode.Append, FileAccess.Write, FileShare.Read))
{
oldFile.CopyTo(tempFile);
}
}
// 3) Finally, dump the Temp file back to the original, keeping all its
// original permissions etc.
File.Replace(tempFilePath, filePath, null);
即使您正在编写的内容很小,Temp文件也会在.Replace()之前将整个原始文件附加到它上面,因此它确实需要在磁盘上。
请注意,此代码不是线程安全的;如果多个线程访问此代码,您可能会丢失文件交换中的写入。也就是说,它也相当昂贵,所以你还是想要对它进行门禁访问 - 将多个Providers的写入传递给缓冲区,缓冲区通过单个Consumer线程上的prepend方法定期清空。
答案 7 :(得分:1)
是的,基本上你可以使用这样的东西:
public static void PrependString(string value, FileStream file)
{
var buffer = new byte[file.Length];
while (file.Read(buffer, 0, buffer.Length) != 0)
{
}
if(!file.CanWrite)
throw new ArgumentException("The specified file cannot be written.", "file");
file.Position = 0;
var data = Encoding.Unicode.GetBytes(value);
file.SetLength(buffer.Length + data.Length);
file.Write(data, 0, data.Length);
file.Write(buffer, 0, buffer.Length);
}
public static void Prepend(this FileStream file, string value)
{
PrependString(value, file);
}
然后
using(var file = File.Open("yourtext.txt", FileMode.Open, FileAccess.ReadWrite))
{
file.Prepend("Text you want to write.");
}
虽然存在大量文件但不是很有效。
答案 8 :(得分:1)
使用此课程:
public static class File2
{
private static readonly Encoding _defaultEncoding = new UTF8Encoding(false, true); // encoding used in File.ReadAll*()
private static object _bufferSizeLock = new Object();
private static int _bufferSize = 1024 * 1024; // 1mb
public static int BufferSize
{
get
{
lock (_bufferSizeLock)
{
return _bufferSize;
}
}
set
{
lock (_bufferSizeLock)
{
_bufferSize = value;
}
}
}
public static void PrependAllLines(string path, IEnumerable<string> contents)
{
PrependAllLines(path, contents, _defaultEncoding);
}
public static void PrependAllLines(string path, IEnumerable<string> contents, Encoding encoding)
{
var temp = Path.GetTempFileName();
File.WriteAllLines(temp, contents, encoding);
AppendToTemp(path, temp, encoding);
File.Replace(temp, path, null);
}
public static void PrependAllText(string path, string contents)
{
PrependAllText(path, contents, _defaultEncoding);
}
public static void PrependAllText(string path, string contents, Encoding encoding)
{
var temp = Path.GetTempFileName();
File.WriteAllText(temp, contents, encoding);
AppendToTemp(path, temp, encoding);
File.Replace(temp, path, null);
}
private static void AppendToTemp(string path, string temp, Encoding encoding)
{
var bufferSize = BufferSize;
char[] buffer = new char[bufferSize];
using (var writer = new StreamWriter(temp, true, encoding))
{
using (var reader = new StreamReader(path, encoding))
{
int bytesRead;
while ((bytesRead = reader.ReadBlock(buffer,0,bufferSize)) != 0)
{
writer.Write(buffer,0,bytesRead);
}
}
}
}
}
答案 9 :(得分:1)
前置:
private const string tempDirPath = @"c:\temp\log.log", tempDirNewPath = @"c:\temp\log.new";
StringBuilder sb = new StringBuilder();
...
File.WriteAllText(tempDirNewPath, sb.ToString());
File.AppendAllText(tempDirNewPath, File.ReadAllText(tempDirPath));
File.Delete(tempDirPath);
File.Move(tempDirNewPath, tempDirPath);
using (FileStream fs = File.OpenWrite(tempDirPath))
{ //truncate to a reasonable length
if (16384 < fs.Length) fs.SetLength(16384);
fs.Close();
}
答案 10 :(得分:0)
将文件内容放在字符串中。将要添加到文件顶部的新数据附加到该字符串 - string = newdata + string
。然后将文件的搜索位置移动到0并将字符串写入文件。