zoukankan      html  css  js  c++  java
  • C#文件输入输出流

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

    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;
                }
            }
        }
    }

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

                    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();
  • 相关阅读:
    用记事本编写C#程序并运行C#代码
    C#传递参数大集合
    JQuery或JavaScript获取网页的宽度、高等
    mybatis
    mysql 函数
    eclipse sts 常规操作
    谷歌浏览器自动翻译当前网页
    推荐Calendar操作日期
    IDEA 快捷键
    eclipse sts 快捷键
  • 原文地址:https://www.cnblogs.com/XDJjy/p/3679207.html
Copyright © 2011-2022 走看看