C#读写文件的例子

爱上不会游泳的鱼 2011-08-26 05:58:31
现在有一个1.txt文件,内容是一个1,用二进制的方式把他读到另外一个文件2.txt中是49,不知道为什么是49(这个不重要),然后读2.txt文件,要读到的内容是1,怎么样读呢。读到的变量data是49,求救啊!!!!!!!!!!!
string pathbody = @"C:\Documents and Settings\Administrator\桌面\新建文件夹 (2)" + "\\1.txt";
string pathheader = @"C:\Documents and Settings\Administrator\桌面\新建文件夹 (2)" + "\\2.txt";
byte[] arrFile=null;
using (FileStream fs2 = new FileStream(pathbody, FileMode.Open))
{
arrFile= new byte[1];
fs2.Read(arrFile, 0, 1);//读1.txt的文件
using (StreamReader sr = new StreamReader(fs2))
{
using (StreamWriter sw1 = new StreamWriter(pathheader, true))
{
for (int i = 0; i < 1; i++)
{
sw1.Write(arrFile[i]);

}
}
}

}

using (FileStream fsBody = new FileStream(pathheader, FileMode.Open, FileAccess.Read))
{

using (StreamReader sw1 = new StreamReader(fsBody))
{
string str = sw1.ReadLine();
byte[] demo = System.Text.Encoding.Default.GetBytes(str);
string data = System.Text.Encoding.Default.GetString(demo);

}
}
...全文
197 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
freemangood 2011-08-27
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 stonespace 的回复:]
你读到49是正确的,49就是字符“1”,字符“1”的ascii码和UTF-8,UTF7编码都是49,你把49写入另一个txt文件,然后打开看,还是1,因为49就是字符“1”,
[/Quote]

4楼正解,1的ASCII正是49,转换一下就行了,他已经在下面给出解决办法了!
zhouxingyu896 2011-08-26
  • 打赏
  • 举报
回复
1楼的挺好
你的选择B 2011-08-26
  • 打赏
  • 举报
回复
<a href="http://blog.csdn.net/nidexuanzhe/article/details/6060869">
看看去
</a>
hui_play 2011-08-26
  • 打赏
  • 举报
回复
应该是二进制存储就是49,不要用byte,直接readline就好了
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 stonespace 的回复:]
这样之后读出来的2.txt就是1了,

string pathbody = @"C:\Documents and Settings\Administrator\桌面\新建文件夹 (2)" + "\\1.txt";
string pathheader = @"C:\Documents and Settings\Administrator\桌面\新建文件夹 (2)" + "\\2.txt";
……
[/Quote]

我是想把1.txt内容存到2.txt文件的是二进制.你这样写存的是一样的内容。
stonespace 2011-08-26
  • 打赏
  • 举报
回复
这样之后读出来的2.txt就是1了,

string pathbody = @"C:\Documents and Settings\Administrator\桌面\新建文件夹 (2)" + "\\1.txt";
string pathheader = @"C:\Documents and Settings\Administrator\桌面\新建文件夹 (2)" + "\\2.txt";
byte[] arrFile=null;
using (FileStream fs2 = new FileStream(pathbody, FileMode.Open))
{
arrFile= new byte[1];
fs2.Read(arrFile, 0, 1);//读1.txt的文件
string sData = System.Text.Encoding.Default.GetString(arrFile);
using (StreamReader sr = new StreamReader(fs2))
{
using (StreamWriter sw1 = new StreamWriter(pathheader, true))
{
for (int i = 0; i < 1; i++)
{
sw1.Write(sData );

}
}
}

}

using (FileStream fsBody = new FileStream(pathheader, FileMode.Open, FileAccess.Read))
{

using (StreamReader sw1 = new StreamReader(fsBody))
{
string str = sw1.ReadLine();
byte[] demo = System.Text.Encoding.Default.GetBytes(str);
string data = System.Text.Encoding.Default.GetString(demo);

}
}
stonespace 2011-08-26
  • 打赏
  • 举报
回复
为了避免这类问题,应该统一用StreamReader和StreamWriter来读写,不要用FileStream直接读写,
萧炎 2011-08-26
  • 打赏
  • 举报
回复
这才是读取
---------

if (File.Exists(@"../../XX.txt"))//路径和文件类型大家自己设置
{
StreamReader sr = new StreamReader(@"../../XX.txt", true);

string str = sr.ReadLine();
if (str != null)
{
txtAA.Text = str.Trim();
}
sr.Close();
}

stonespace 2011-08-26
  • 打赏
  • 举报
回复
你读到49是正确的,49就是字符“1”,字符“1”的ascii码和UTF-8,UTF7编码都是49,你把49写入另一个txt文件,然后打开看,还是1,因为49就是字符“1”,
萧炎 2011-08-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zyloveyrf 的回复:]
LZ以下代码是读取.txt值的例子
------------------------

C# code


if (File.Exists("路径/XX.txt"))
{
if (!this.txtAA.AutoCompleteCustomSource.Contains(txtAA.Text))//判断记录是否存在
……
[/Quote]
晕 今天下午老是打错字
纠正下LZ这是写入值
萧炎 2011-08-26
  • 打赏
  • 举报
回复
LZ以下代码是读取.txt值的例子
------------------------

if (File.Exists("路径/XX.txt"))
{
if (!this.txtAA.AutoCompleteCustomSource.Contains(txtAA.Text))//判断记录是否存在
{
StreamWriter sw = new StreamWriter("路径/XX.txt", false);//true参数不可少,否则会覆盖以前存入的记录
sw.WriteLine(this.txtAA.Text.Trim());//存入记录
sw.Close();
if (!this.txtAA.AutoCompleteCustomSource.Contains(this.txtAA.Text))
{
this.txtAA.AutoCompleteCustomSource.Add(this.txtAA.Text);
}
}
this.DialogResult = DialogResult.OK;
}

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧