C#使用StreamReader读取文件的方法

本文实例讲述了C#使用StreamReader读取文件的方法。分享给大家供大家参考。具体实现方法如下:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace W
{
  class Program
  {
    static void Main(string[] args)
    {
      using (StreamReader sr = new StreamReader (@"c:\1.txt",Encoding.UTF8,true))
      {
        while (!sr.EndOfStream)
        {
          Console.WriteLine(sr.ReadLine());
        }
      }
      Console.WriteLine("读取完毕");
      Console.ReadKey();
    }
  }
}

希望本文所述对大家的C#程序设计有所帮助。

(0)

相关推荐

  • C#使用StreamWriter写入文件的方法

    本文实例讲述了C#使用StreamWriter写入文件的方法.分享给大家供大家参考.具体实现方法如下: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace W { class Program { static void Main(string[] args) { u

  • C#控制台进行文件读写的方法

    本文实例讲述了C#控制台进行文件读写的方法.分享给大家供大家参考.具体如下: C#控制台进行文件写入: using System; using System.IO; class file1 { public static void Main() { string PATH = null; Console.WriteLine("file path: "); //这里输入文本所在目录 例如 d:\text.txt PATH = Console.ReadLine(); StreamW

  • C#实现获取文件夹大小的方法

    本文实例讲述了C#实现获取文件夹大小的方法.分享给大家供大家参考.具体如下: 当然了,首先都需要引入System.IO这个命名空间 第一个方法: public static long GetDirectoryLength(string dirPath) { //判断给定的路径是否存在,如果不存在则退出 if (!Directory.Exists(dirPath)) return 0; long len = 0; //定义一个DirectoryInfo对象 DirectoryInfo di = n

  • C#对文件/文件夹操作代码汇总

    C#追加文件 StreamWriter sw = File.AppendText(Server.MapPath(".")+"\\myText.txt"); sw.WriteLine("追逐理想"); sw.WriteLine("kzlll"); sw.WriteLine(".NET笔记"); sw.Flush(); sw.Close(); C#拷贝文件 string OrignFile,NewFile; O

  • C#判断本地文件是否处于打开状态的方法

    本文实例讲述了C#判断本地文件是否处于打开状态的方法.分享给大家供大家参考.具体分析如下: 对于应用程序,有时候可能需要判断某个文件是否已经被打开,也就是指是否被某个流连接着.这在对文件的读写比较频繁的程序中尤为重要,因为一个文件同一时刻只能有一个流连接的.下面的代码也许能有所帮助. public class FileStatus { [DllImport("kernel32.dll")] private static extern IntPtr _lopen(string lpPat

  • C#使用Streamwriter打开文件的方法

    本文实例讲述了C#使用Streamwriter打开文件的方法.分享给大家供大家参考.具体如下: using System; using System.IO; public class KtoD1 { public static void Main() { string str; StreamWriter fstr_out; // Open the file directly using StreamWriter. try { fstr_out = new StreamWriter("test.t

  • C#实现文件断点续传下载的方法

    本文实例讲述了C#实现文件断点续传下载的方法.分享给大家供大家参考.具体实现方法如下: using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.IO; using System.Text; using System.Net; namespace simpleDemo { class Program { /// <su

  • C#文件流进行压缩和解压缩的方法

    本文实例讲述了C#文件流进行压缩和解压缩的方法.分享给大家供大家参考.具体实现方法如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.IO.Compression; using System.Linq; using System.Text; usi

  • C#实现最完整的文件和目录操作类实例

    本文实例讲述了C#实现最完整的文件和目录操作类.分享给大家供大家参考.具体如下: using System; using System.Text; using System.IO; namespace HelloCsharp.Utilities { /// <summary> /// 文件操作类 /// </summary> public static class DirFile { #region 检测指定目录是否存在 /// <summary> /// 检测指定目录

  • C#使用StreamReader读取文件的方法

    本文实例讲述了C#使用StreamReader读取文件的方法.分享给大家供大家参考.具体实现方法如下: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace W { class Program { static void Main(string[] args) { u

  • C# StreamReader类实现读取文件的方法

    在 C# 语言中 StreamReader 类用于从流中读取字符串.它继承自 TextReader 类. StreamReader 类的构造方法有很多,这里介绍一些常用的构造方法,如下表所示. 构造方法 说明 StreamReader(Stream stream) 为指定的流创建 StreamReader 类的实例 StreamReader(string path) 为指定路径的文件创建 StreamReader 类的实例 StreamReader(Stream stream, Encoding

  • C#逐行读取文件的方法

    本文实例讲述了C#逐行读取文件的方法.分享给大家供大家参考.具体如下: 这里使用C#逐行读取文件,对于大文件的读取非常有用. StreamReader sr = new StreamReader("fileName.txt"); string line; while((line= sr.ReadLine()) != null) { Console.WriteLine("xml template:"+line); } if (sr != null)sr.Close()

  • C#通过指针读取文件的方法

    本文实例讲述了C#通过指针读取文件的方法.分享给大家供大家参考.具体如下: // readfile.cs // 编译时使用:/unsafe // 参数:readfile.txt // 使用该程序读并显示文本文件. using System; using System.Runtime.InteropServices; using System.Text; class FileReader { const uint GENERIC_READ = 0x80000000; const uint OPEN

  • python使用fileinput模块实现逐行读取文件的方法

    本文实例讲述了python使用fileinput模块实现逐行读取文件的方法.分享给大家供大家参考.具体实现方法如下: #-------------------------------- # Name: read_lines.py # Author: Kevin Harris # Last Modified: 02/13/04 # Description: This Python script demonstrates # how to use fileinput to read # each l

  • C#中winform使用相对路径读取文件的方法

    本文实例讲述了C#中winform使用相对路径读取文件的方法.分享给大家供大家参考.具体分析如下: 目录结构如下图所示:   方法一:由于生成的exe文件在bin\debug目录下,可以使用向上查找目录的方式获取要读取的xml文件 复制代码 代码如下: string haarXmlPath = @"../../haarcascade_frontalface_alt_tree.xml"; FileInfo file = new FileInfo(fileName); string  fu

  • php从文件夹随机读取文件的方法

    本文实例讲述了php从文件夹随机读取文件的方法.分享给大家供大家参考.具体实现方法如下: function RandomFile($folder='', $extensions='.*'){ // fix path: $folder = trim($folder); $folder = ($folder == '') ? './' : $folder; // check folder: if (!is_dir($folder)){ die('invalid folder given!'); }

  • C#使用文件流读取文件的方法

    本文实例讲述了C#使用文件流读取文件的方法.分享给大家供大家参考.具体如下: using System; using System.IO; namespace Client.Chapter_11___File_and_Streams { public class OpenExistingFile { static void Main(string[] args) { FileInfo MyFile = new FileInfo(@"c:\Projects\Testing.txt");

  • Java基于IO流读取文件的方法

    本文实例讲述了Java基于IO流读取文件的方法.分享给大家供大家参考,具体如下: public static void readFile(){ String pathString = TEST.class.getResource("/simu").getFile(); try { pathString = URLDecoder.decode(pathString, "utf-8"); } catch (UnsupportedEncodingException e1)

  • Python 多线程不加锁分块读取文件的方法

    多线程读取或写入,一般会涉及到同步的问题,否则产生的结果是无法预期的.那么在读取一个文件的时候,我们可以通过加锁,但读不像写操作,会导致文件错误,另外锁操作是有一定的耗时.因此通过文件分块,可以比较有效的解决多线程读问题,之前看到有人写的分块操作,比较复杂,需要实现建立好线程以及所读取块信息,在这里,我提供了一种比较简便的方法,以供参考. #!/user/bin/env python #_*_coding:utf-8_*_ from threading import Thread import

随机推荐