为什么读出的文件是这样??? 50
.txtThisisaTest!我的代码如下:byte[]mydate=newbyte[100];char[]mychar=newchar[100];FileStream...
.txt
This is a Test !
我的代码如下:
byte[] mydate = new byte[100];
char[] mychar = new char[100];
FileStream aFile = null;
aFile = new FileStream(@"E:\ztx\website\File\abce.txt", FileMode.Open);
aFile.Seek(0, SeekOrigin.Begin);
aFile.Read(mydate, 0, 100);
Decoder ex = Encoding.UTF8.GetDecoder();
ex.GetChars(mydate, 0, mydate.Length, mychar, 0);
Response.Write(mychar);
aFile.Close();
return;
页面显示结果:System.Char[] 展开
This is a Test !
我的代码如下:
byte[] mydate = new byte[100];
char[] mychar = new char[100];
FileStream aFile = null;
aFile = new FileStream(@"E:\ztx\website\File\abce.txt", FileMode.Open);
aFile.Seek(0, SeekOrigin.Begin);
aFile.Read(mydate, 0, 100);
Decoder ex = Encoding.UTF8.GetDecoder();
ex.GetChars(mydate, 0, mydate.Length, mychar, 0);
Response.Write(mychar);
aFile.Close();
return;
页面显示结果:System.Char[] 展开
展开全部
你读文件用StreamReader,写文件用:StreamWriter比较方便一点
例子:
//创建文件
public static void CreateFile()
{
StreamWriter sw;
sw = File.CreateText("c:\\test.txt");
sw.WriteLine("This is the first line!");
sw.WriteLine("This is the Second line!");
sw.Close();
Console.WriteLine("文件建立成功!");
}
//读文件
private static void ReadFile(string path)
{
StreamReader sr;
sr = File.OpenText(path);
string line;
line = sr.ReadLine();
while(line != null)
{
Console.WriteLine(line);
line = sr.ReadLine();
}
sr.Close();
}
//追加文件
private static void AppendFile(string path)
{
StreamWriter sw;
sw = File.AppendText(path);
sw.WriteLine("哈哈,追加成功了!");
sw.Close();
Console.WriteLine("追加文件成功!");
}
例子:
//创建文件
public static void CreateFile()
{
StreamWriter sw;
sw = File.CreateText("c:\\test.txt");
sw.WriteLine("This is the first line!");
sw.WriteLine("This is the Second line!");
sw.Close();
Console.WriteLine("文件建立成功!");
}
//读文件
private static void ReadFile(string path)
{
StreamReader sr;
sr = File.OpenText(path);
string line;
line = sr.ReadLine();
while(line != null)
{
Console.WriteLine(line);
line = sr.ReadLine();
}
sr.Close();
}
//追加文件
private static void AppendFile(string path)
{
StreamWriter sw;
sw = File.AppendText(path);
sw.WriteLine("哈哈,追加成功了!");
sw.Close();
Console.WriteLine("追加文件成功!");
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
mydate是你读出的结果,也就是this is test,但是你对这个字符串进行了获得Char的操作,所以得到的是char型数据
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Response.Write(new string(mychar));
//作为字符数组是不能够字节写出的
或则干脆这样
MyFileStream = new FileStream("sometext.txt", FileMode.Open);
FileSize = MyFileStream.Length;
byte[] Buffer = new byte[(int)FileSize];
MyFileStream.Read(Buffer, 0, (int)FileSize);
MyFileStream.Close();
//Response.Write("<b>File Contents: </b>");
Response.BinaryWrite(Buffer);
//作为字符数组是不能够字节写出的
或则干脆这样
MyFileStream = new FileStream("sometext.txt", FileMode.Open);
FileSize = MyFileStream.Length;
byte[] Buffer = new byte[(int)FileSize];
MyFileStream.Read(Buffer, 0, (int)FileSize);
MyFileStream.Close();
//Response.Write("<b>File Contents: </b>");
Response.BinaryWrite(Buffer);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你读出的文件进行了二进制数组的操作
所以结果不是this is test
所以结果不是this is test
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询