[C#] using System; using System.IO; using System.Text;class Test {
public static void Main() { string path = @"c:\temp\MyTest.txt"; // Delete the file if it exists. if (File.Exists(path)) { File.Delete(path); } //Create the file. using (FileStream fs = File.Create(path)) { AddText(fs, "This is some text"); AddText(fs, "This is some more text,"); AddText(fs, "\r\nand this is on a new line"); AddText(fs, "\r\n\r\nThe following is a subset of characters:\r\n"); for (int i=1;i < 120;i++) { AddText(fs, Convert.ToChar(i).ToString()); //Split the output at every 10th character. if (Math.IEEERemainder(Convert.ToDouble(i), 10) == 0) { AddText(fs, "\r\n"); } } } //Open the stream and read it back. using (FileStream fs = File.OpenRead(path)) { byte[] b = new byte[1024]; UTF8Encoding temp = new UTF8Encoding(true); while (fs.Read(b,0,b.Length) > 0) { Console.WriteLine(temp.GetString(b)); } } } private static void AddText(FileStream fs, string value) { byte[] info = new UTF8Encoding(true).GetBytes(value); fs.Write(info, 0, info.Length); } }
[C#] public override void Write( byte[] array, int offset, int count );参数 array 字节所写入的数组。 offset array 中的字节偏移量,从此处开始写入。 count 最多写入的字节数。
全部取出来后处理再写回去。 StreamReader sr = new StreamReader(@"C:\b.txt"); System.Text.StringBuilder s = new System.Text.StringBuilder(sr.ReadToEnd()); sr.Close(); int index = 0; if (index <= s.Length) s.Insert(index, "abc"); index = 2; if (index <= s.Length) s.Insert(index, "ddd"); StreamWriter sw = new StreamWriter(@"C:\b.txt"); sw.Write(s.ToString()); sw.Close();
using System;
using System.IO;
using System.Text;class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt"; // Delete the file if it exists.
if (File.Exists(path))
{
File.Delete(path);
} //Create the file.
using (FileStream fs = File.Create(path))
{
AddText(fs, "This is some text");
AddText(fs, "This is some more text,");
AddText(fs, "\r\nand this is on a new line");
AddText(fs, "\r\n\r\nThe following is a subset of characters:\r\n"); for (int i=1;i < 120;i++)
{
AddText(fs, Convert.ToChar(i).ToString()); //Split the output at every 10th character.
if (Math.IEEERemainder(Convert.ToDouble(i), 10) == 0)
{
AddText(fs, "\r\n");
}
}
} //Open the stream and read it back.
using (FileStream fs = File.OpenRead(path))
{
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);
while (fs.Read(b,0,b.Length) > 0)
{
Console.WriteLine(temp.GetString(b));
}
}
} private static void AddText(FileStream fs, string value)
{
byte[] info = new UTF8Encoding(true).GetBytes(value);
fs.Write(info, 0, info.Length);
}
}
public override void Write(
byte[] array,
int offset,
int count
);参数
array
字节所写入的数组。
offset
array 中的字节偏移量,从此处开始写入。
count
最多写入的字节数。
StreamReader sr = new StreamReader(@"C:\b.txt");
System.Text.StringBuilder s = new System.Text.StringBuilder(sr.ReadToEnd());
sr.Close(); int index = 0; if (index <= s.Length)
s.Insert(index, "abc"); index = 2; if (index <= s.Length)
s.Insert(index, "ddd"); StreamWriter sw = new StreamWriter(@"C:\b.txt");
sw.Write(s.ToString());
sw.Close();
offset 的字节偏移量
指的是从待写入字节数组的索引,并不是文件流的索引,
文件流指定位置可以用FileStream的
Seek方法定位,不过FileStream的write方法是覆盖式的写法
比如
在 abcdefg的第三位置插入ddd
变成abdddfg
/// 把Mac地址与客户数连在一起的20个字符放在10000个随机码中。
/// </summary>
/// <param name="amount">Mac地址与客户数连在一起的20个字符在10000个随机码中的位置</param>
/// <param name="str">Mac地址与客户数连在一起的20个字符</param>
/// <param name="oldstr">10000个随机码</param>
private void RepaleString(int[] amount,char[] str,ref string oldstr)
{
string front = "",back = "";
for(int i=0;i<amount.Length;i++)
{
front = oldstr.Substring(0,amount[i]-1);
back = str[i].ToString() + oldstr.Substring(amount[i],oldstr.Length-amount[i]);
oldstr = front + back;
}
}
long offset,
SeekOrigin origin
);
SeekOrigin origin 是指的什么?
能不能说一下具体的实例啊.谢谢
这个参数我也用了,不过就是不能定位,对已存在数据进行改写.