首页 > 其他 > 详细

C#文件输入输出流

时间:2014-04-23 23:47:27      阅读:693      评论:0      收藏:0      [点我收藏+]

从输入流中读取数据(行读取字符串)

bubuko.com,布布扣
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ConsoleApplication2
{
    class Class2
    {
        public const string path = "C:/Users/dajun/Desktop/C#/xdj/gadata.txt";
        static void Main(string[] args)
        {
            //string path = "C:/Users/dajun/Desktop/C#/xdj/gadata.txt";
            try
            {
                //FileStream aFile = new FileStream(path, FileMode.Open);// 方式一
                //StreamReader sr = new StreamReader(aFile);
                StreamReader sr = new StreamReader(path); //方式二

                string strLine = sr.ReadLine();
                //Read data in line by line 
                while (strLine != null)
                {
                    Console.WriteLine(strLine);
                    strLine = sr.ReadLine();
                }
                sr.Close();
            }
            catch (IOException ex)
            {
                Console.WriteLine("An IOException has been thrown!");
                Console.WriteLine(ex.ToString());
                Console.ReadLine();
                return;
            }
        }
    }
}
bubuko.com,布布扣

 2.将流的下一个字符(包括空格和换行)作为正整数值返回

bubuko.com,布布扣
                int nChar;
                nChar = sr.Read();
                while (nChar != -1)
                {
                    //Console.Write(nChar);
                    Console.Write(Convert.ToChar(nChar));
                    //Console.WriteLine("{0}",nChar);
                    nChar = sr.Read();
                }
                sr.Close();
bubuko.com,布布扣

 

 

C#文件输入输出流,布布扣,bubuko.com

C#文件输入输出流

原文:http://www.cnblogs.com/XDJjy/p/3679207.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!