代码 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" );
我原来写那个还修改了一下文件的position,不过对结果没什么影响
我建立了一个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 不知道你说的是哪里空字符串的问题?
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" );
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
不知道你说的是哪里空字符串的问题?
为什么我调试一直不行呢, 我看了一下,我那个txt文档是“英文字符”的编码方式,按道理说应该是ASCII码啊。而且我用中文GB1232(忘了是不是这个了)也读不出来。。
除了txt文件与你的不同,最后我省略了File.Copy()以外,其他代码完全复制的你的。
p.s.把encoding的ASCII改成default之后就完全没有问题了
p.s之p.s.衷心的感谢各位的热心帮助~鞠躬谢谢~~~~