zoukankan      html  css  js  c++  java
  • C# StreamReader对象

    1.读取文件

    输入流用于从外部源读取数据,在很多情况下,数据源可以是磁盘上的文件或网络的某些位置,任何可能发送数据的位置都可以是数据源,比如网络应用程序,web服务,甚至是控制台。StreamReader是一个通用类,可以用于任何流;StreamReader对象的创建方式非常类似于StreamWriter对象的创建方式。

    StreamWriter类有两个基本的方法read和readLine

    Read()方法将流的下一个字符作为正整数值返回,如果到达了流的结尾处,则返回-1

    ReadLing()方法是读取并返回一行字符,如果返回为空,那么就是到达了流的结尾。

    ReadEnd()方法读小文件最好,它直接读取完整的文件并作为字符串返回。

    例:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;

    namespace StreamReaderTest
    {
        class Program
        {
            
            static void Main(string[] args)
            {
                string strLine; 
                try
                {
                    Console.Write("请输入文件路径及文件名:");
                    string mess = Console.ReadLine();
                    FileStream aFile = new FileStream(mess, FileMode.Open);
                    StreamReader sr = new StreamReader(aFile);//用FileStream对象实例化一个StreamReader对象
                    //strLine = sr.ReadToEnd();//读取完整的文件,如果用这个方法,就可以不用下面的while循环
                    strLine = sr.ReadLine();//  读取一行字符并返回
                    while (strLine != null)
                    {
                        Console.WriteLine(strLine);
                        strLine = sr.ReadLine();
                    }
                    sr.Close();
                    Console.ReadLine();
                }
                catch (IOException e)
                {
                    Console.WriteLine("an IOexception has been thrown!");
                    Console.WriteLine(e.ToString());
                    return;
                }
                return;
            }
        }
    }

    2.分隔文件

    读取使用逗号分隔的文件,string类提供了一种称为Split()的方法,可以用于将字符串按照提供的分隔符分隔成字符组.

    例:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;

    namespace CommaValues
    {
        class Program
        {
            static void Main(string[] args)
            {
                while (true)
                {
                    string strLine;
                    string[] strArray;
                    char[] charArray = new Char[] { ',' };
                    Console.Write("请输入文件内容以逗号分隔的文件路径及文件名:");
                    string name = Console.ReadLine();

                    try
                    {
                        FileStream aFile = new FileStream(name, FileMode.Open);
                        StreamReader sr = new StreamReader(aFile);

                        strLine = sr.ReadLine();
                        while (strLine != null)
                        {
                            strArray = strLine.Split(charArray);
                            for (int x = 0; x <= strArray.GetUpperBound(0); x++)
                            {
                                Console.WriteLine(strArray[x].Trim());
                            }
                            strLine = sr.ReadLine();
                        }
                        sr.Close();
                        //Console.ReadLine();
                    }
                    catch (IOException e)
                    {
                        Console.WriteLine(e.ToString());
                        return;
                    }
                    //return;
                }
            }
        }
    }

  • 相关阅读:
    Code Forces Gym 100886J Sockets(二分)
    CSU 1092 Barricade
    CodeChef Mahesh and his lost array
    CodeChef Gcd Queries
    CodeChef GCD2
    CodeChef Sereja and LCM(矩阵快速幂)
    CodeChef Sereja and GCD
    CodeChef Little Elephant and Balance
    CodeChef Count Substrings
    hdu 4001 To Miss Our Children Time( sort + DP )
  • 原文地址:https://www.cnblogs.com/melao2006/p/4239678.html
Copyright © 2011-2022 走看看