| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 780 人关注过本帖
标题:C#文件读写求助一直遇到 流不可写问题~~~~
只看楼主 加入收藏
hdusec
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2015-4-27
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
C#文件读写求助一直遇到 流不可写问题~~~~
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using

namespace arraylist
{
    class Program
    {
        static void Main(string[] args)
        {
            FileStream aFile = new FileStream(@"D:\test1.txt", FileMode.OpenOrCreate,FileAccess.ReadWrite);     
            StreamReader sr = new StreamReader(aFile);
            int charCode = sr.Read();
            char[] ch=new  char[100];
            int i = 0;
            while(charCode!=-1)
            {
                 ch[i]=Convert.ToChar(charCode);
                charCode=sr.Read();
                i++;
            }         
            string str1 = new string(ch);
            sr.Close();
            StreamWriter sw = new StreamWriter(aFile);
            string str2 = "ok";
            char[] chs = { 'p', 'c' };
            for ( i = 0; i < aFile.Length + 2; i++)
            {
                if (str1.Substring(i, 2) == str2)
                {
                    sw.Write(chs, i, 2);
                }
            }
            Console.WriteLine(str1);
            Console.ReadKey();
        }
    }
}
代码一直卡死在 StreamWriter sw = new StreamWriter(aFile);这一步,可是我的aFile明明已经设置为可读写了啊~~为什么还是说不可写呢,
这个代码是要在test.txt内容上找到有OK字符的地方,然后加上字母pc。
不知道为什么会报这个错。。求大神解答。
2015-05-04 21:10
八画小子
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:37
帖 子:709
专家分:2063
注 册:2010-11-11
收藏
得分:20 
sr.Close();
这一句已经把文件流给关闭了,当然不可写了。
2015-05-05 01:59
快速回复:C#文件读写求助一直遇到 流不可写问题~~~~
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021157 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved