zoukankan      html  css  js  c++  java
  • C# 读取文件内容

    读取文件内容有三种方式

    • 全部读取到字符串变量中
    • 一次读取一行
    • 全部读取到字符串数组中,每个数组元素存储一行文本

    全部读取到字符串变量

    string text = System.IO.File.ReadAllText(@"C:UsersPublicTestFolderWriteText.txt");
    
    System.Console.WriteLine("Contents of WriteText.txt = {0}", text);

    一次读取一行

    int counter = 0;  
    string line;  
      
    System.IO.StreamReader file =   
        new System.IO.StreamReader(@"c:	est.txt");  
    while((line = file.ReadLine()) != null)  
    {  
        System.Console.WriteLine(line);  
        counter++;  
    }  
      
    file.Close();  
    System.Console.WriteLine("There were {0} lines.", counter);  

    全部读取到字符串数组中,每个数组元素存储一行文本

    string[] lines = System.IO.File.ReadAllLines(@"C:UsersPublicTestFolderWriteLines2.txt");
    
    System.Console.WriteLine(
    "Contents of WriteLines2.txt = "); foreach (string line in lines) { Console.WriteLine(" " + line); }
  • 相关阅读:
    koller——PGM 基础
    java 类 多态
    java 基础知识
    相关性检验和独立性检验
    IDEA远程连接Hadoop
    sklearn pipeline
    java static
    sklearn learn preprocessing
    数据预处理 简介
    RATE-MAX alpha冲刺第八天
  • 原文地址:https://www.cnblogs.com/ryanzheng/p/10934083.html
Copyright © 2011-2022 走看看