Windows系统中使用C#读取文本文件内容的小示例
作者:goldensun 发布时间:2023-05-05 20:27:08
标签:C#,读取
读取文本文件中的内容
此示例读取文本文件的内容以使用 System.IO.File 选件类的静态方法 ReadAllText 和 ReadAllLines。
class ReadFromFile
{
static void Main()
{
// The files used in this example are created in the topic
// How to: Write to a Text File. You can change the path and
// file name to substitute text files of your own.
// Example #1
// Read the file as one string.
string text = System.IO.File.ReadAllText(@"C:\Users\Public\TestFolder\WriteText.txt");
// Display the file contents to the console. Variable text is a string.
System.Console.WriteLine("Contents of WriteText.txt = {0}", text);
// Example #2
// Read each line of the file into a string array. Each element
// of the array is one line of the file.
string[] lines = System.IO.File.ReadAllLines(@"C:\Users\Public\TestFolder\WriteLines2.txt");
// Display the file contents by using a foreach loop.
System.Console.WriteLine("Contents of WriteLines2.txt = ");
foreach (string line in lines)
{
// Use a tab to indent each line of the file.
Console.WriteLine("\t" + line);
}
// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}
}
一次一行地读取文本文件
本示例使用 StreamReader 类的 ReadLine 方法将文本文件的内容读取(一次读取一行)到字符串中。所有文本行都保存在字符串 line 中并显示在屏幕上。
int counter = 0;
string line;
// Read the file and display it line by line.
System.IO.StreamReader file =
new System.IO.StreamReader(@"c:\test.txt");
while((line = file.ReadLine()) != null)
{
System.Console.WriteLine (line);
counter++;
}
file.Close();
System.Console.WriteLine("There were {0} lines.", counter);
// Suspend the screen.
System.Console.ReadLine();
0
投稿
猜你喜欢
- 一、枚举类类的对象只有有限个, 确定的. 我们称此类为枚举类.说明:1.类的对象只有有限个,确定的。如:星期:Monday(星期一)、…、S
- 代理对象的生成方法是:Proxy.newProxyInstance(...) ,进入这个方法内部,一步一步往下走会发现会调用ProxyGen
- Spring Cloud 为开发人员提供了一系列的工具来快速构建分布式系统的通用模型 。例如:配置管理、服务发现、断路由、智能路由、微代理、
- 我先解释一下什么叫IO流:I:指的是InputStream,这是一个抽象类,最常用的子类是FileInputStreamO:值得是Outpu
- 问题描述ResultSet 表示 select 语句的查询结果集。ResultSet 对象具有指向其当前数据行的指针, 最初,指针被置于第一
- 这篇文章主要介绍了如何使用Jenkins编译并打包SpringCloud微服务目录,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有
- 1、背景一般情况下,有些搜索需求是需要根据拼音和中文来搜索的,那么在elasticsearch中是如何来实现基于拼音来搜索的呢?可以通过el
- 我们知道,值类型的变量是在堆栈上分配内存的,而引用类型包括System.Object的对象是在堆上分配内存的,基于这一特点,当值类型被类型转
- 本文实例讲述了在Linux上运行C#的方法。分享给大家供大家参考。具体方法如下:在任何一个平台(操作系统+硬件体系)上,编写和运行程序的三个
- java url中如何传递数组,springMVC框架controller类如何接收数组参数?下面介绍一下URL中传递数组参数方法:dd.d
- 前言以下为本文要记录的大概内容:Java基础案例:两只老虎、三个和尚、考试奖励以下是本篇文章正文内容,仅供参考一、案例1:两只老虎1.题目:
- 本文实例讲述了C#使用foreach语句简单遍历数组的方法。分享给大家供大家参考。具体如下:using System;public clas
- 开发过程中,有时候图标稍微大点,比如48×48的时候,文字就会和图标叠加起来,解决方法如下:TabWidget tw = tabHost.g
- Semaphore、SemaphoreSlim 类两者都可以限制同时访问某一资源或资源池的线程数。这里先不扯理论,我们从案例入手,通过示例代
- Java 线程池原理Executor框架的两级调度模型在HotSpot VM的模型中,Java线程被一对一映射为本地操作系统线程。JAVA线
- 1、conditional注解介绍含义: 基于条件的注解作用: 根据是否满足某一个特定条件来决定是否创建某个特定的bean意义: Sprin
- idea统计代码行数可以用到插件:Statistic。步骤:File→Settings进入Plugins点击Marketplace搜索Sta
- 1、cmd指令,进入.svn目录,找到wc.db文件 sqlite 3 打开2、 对 svn源代码目录 右键, clean up, 稍等1至
- 一,项目简介经过调查研究进行开发设计的这款仓库管理系统,主要是为商家提供商品货物进销存的信息化管理,以便让商家在竞争如此激烈的今天占据一定的
- 这篇文章主要介绍了Springboot配置文件内容加密代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,