txt文件是我从网上下载的而不是用程序写的,程序调试一切正常,streamreader也是用的ASCII的正确编码方式,但是就是读出来都是“”的空串,求各位大神解答啊~小妹不甚感谢!!!

解决方案 »

  1.   

    代码
     FileStream fs1 = File.Open(a, FileMode.Open);
                    StreamReader sr1 = new StreamReader(fs1, System.Text.Encoding.ASCII);
                    string aa=null ,tit=null ;
                    int pos=-1;
                    do
                    {
                        aa=sr1.ReadLine();
                        if(aa.Contains("]"))
                        {
                            pos =aa.IndexOf ("]");
                            break ;
                        }                }
                    while (true);
                    tit = aa.Substring(pos +1);
                    File.Copy(a, "E:\\new file\\"+tit+".txt" );
      

  2.   

    我原来写那个还修改了一下文件的position,不过对结果没什么影响
      

  3.   

    我建立了一个test.txt
    this is a [test] file.
    i'm the text in row [two].
    然后用了你的代码:
    FileStream fs1 = File.Open(@".\test.txt", FileMode.Open);
    StreamReader sr1 = new StreamReader(fs1, System.Text.Encoding.ASCII);string aa = null;
    string tit = null;
    int pos = -1;do
    {
    aa = sr1.ReadLine();
    if (aa.Contains("]"))
    {
    pos = aa.IndexOf("]");
    break;
    }}
    while (true);
    tit = aa.Substring(pos + 1);
    然后在if (aa.Contains("]"))tit = aa.Substring(pos + 1);处设置了断点。在调试窗口里,可以看到:
    aa   "this is a [test] file."
    tit    " file."
    pos  15

    不知道你说的是哪里空字符串的问题?
      

  4.   

    文本就是网上下的普通txt 文档, 第一行是有字符的 啊啊是显示“”空串
      

  5.   

    您测试没有问题吗?
    为什么我调试一直不行呢, 我看了一下,我那个txt文档是“英文字符”的编码方式,按道理说应该是ASCII码啊。而且我用中文GB1232(忘了是不是这个了)也读不出来。。
      

  6.   


    除了txt文件与你的不同,最后我省略了File.Copy()以外,其他代码完全复制的你的。
      

  7.   

    不知道为什么,刚刚测试读出来不是空串了,英文字符显示,中文为乱码,我发誓我一个代码也没改,但是结果就是不一样了。。
    p.s.把encoding的ASCII改成default之后就完全没有问题了
    p.s之p.s.衷心的感谢各位的热心帮助~鞠躬谢谢~~~~