useStreamReader to read text from a file

1. in c++ environment

 1 using namespace System;
 2 using namespace System::IO;
 3 int main()
 4 {
 5    try
 6    {
 7       // Create an instance of StreamReader to read from a file.
 8       StreamReader^ sr = gcnew StreamReader( "TestFile.txt" );
 9       try
10       {
11          String^ line;
12
13          // Read and display lines from the file until the end of
14          // the file is reached.
15          while ( line = sr->ReadLine() )
16          {
17             Console::WriteLine( line );
18          }
19       }
20       finally
21       {
22          if ( sr )
23             delete (IDisposable^)sr;
24       }
25    }
26    catch ( Exception^ e )
27    {
28       // Let the user know what went wrong.
29       Console::WriteLine( "The file could not be read:" );
30       Console::WriteLine( e->Message );
31    }
32 }

2.c# environment

 1 using System;
 2 using System.IO;
 3
 4 class Test
 5 {
 6     public static void Main()
 7     {
 8         try
 9         {
10             // Create an instance of StreamReader to read from a file.
11             // The using statement also closes the StreamReader.
12             using (StreamReader sr = new StreamReader("TestFile.txt"))
13             {
14                 string line;
15                 // Read and display lines from the file until the end of
16                 // the file is reached.
17                 while ((line = sr.ReadLine()) != null)
18                 {
19                     Console.WriteLine(line);
20                 }
21             }
22         }
23         catch (Exception e)
24         {
25             // Let the user know what went wrong.
26             Console.WriteLine("The file could not be read:");
27             Console.WriteLine(e.Message);
28         }
29     }
30 }
时间: 2024-10-21 17:46:32

useStreamReader to read text from a file的相关文章

python cookbook —— Searching and Replacing Text in a File

要将文件中的某一个字符串替换为另一个,然后写入一个新文件中: 首先判断输入格式,至少需要输入被搜索的Text和替换的Text,输入输出文件可以是存在的文件,也可以是标准输入输出流 (os.path是个好东西) import os, sys nargs = len(sys.argv) if not 3 <= nargs <= 5: print "usage: %s search_text replace_text [infile [outfile]]" % os.path.b

sublime text 3插件---File Header配置

今天趁着有点闲工夫,准备好好配置一下sublime环境,毕竟天天见面. 首当其冲的就是FileHeader插件了,安装它之后就懒得配置过.(方便起见,以下简称FH) FH是一个为文件自动添加前缀字段的插件,比如这样: 上面的这些都是这个插件在文件创建时自动添加进去的. 为了配置这个插件,我找到了它的官方github地址: https://github.com/shiyanhui/FileHeader/blob/master/README.rst (上面的是README的地址) 通过README我

3 Ways to Write Text to a File in Python

https://cmdlinetips.com/2012/09/three-ways-to-write-text-to-a-file-in-python/ 1 with open("myOutFile.txt", "w") as outF: 2 for line in textList: 3 print(line, file=outF) all_lines = ['1', '2', '3'] with open("myOutFile.txt",

NW.JS File System 文件夹的操作( 创建, 删除, 读取 )

<script type="text/javascript"> /* * 引入File System 模块 */ var fs = require("fs"); /* * 创建文件夹的方法 * mkdir(path, callback(){}) * path: 文件夹所在路径 * callback("错误信息 <成功返回null>"): 回调函数 */ fs.mkdir('./test', function(err){ i

如何用pdfbox-app-1.8.10.jar批处理将pdf文档转换成text文档

1.首先下载pdfbox-app-1.8.10.jar(下载地址:http://pdfbox.apache.org/download.html) 2.将pdfbox-app-1.8.10.jar加载到eclipse工程中 1.新建java工程:Flie->New->Java Project,如PdfToText工程,然后右键该工程BuildPath->Configure Bulid Path..,单击Libaries,点击Add External JARs,将刚才下载好的pdfbox-a

安卓初級教程(2):SD創建file,儲存與讀寫的方法(1)

package com.sdmadik; import java.io.*; import android.app.Activity; import android.os.Bundle; import android.view.*; import android.view.View.OnClickListener; import android.widget.*; public class FileUse extends Activity { // GUI controls EditText t

自己编写的sublime text 3 插件

下载链接 import sublime, sublime_plugin, datetime import webbrowser import re import urllib.request,os import threading import json import sublime_api import sys # D:\Program Files\Sublime Text 3\Data\Packages\User\testone_config.sublime-settings # my_pa

How to upload a file in MVC4

Uploading a file in Asp.Net MVC application is very easy. The posted file is automatically available as a HttpPostedFileBase parameters in the action of the controler. For uploading a file on the server you required to have a file input control with

input类型为file改变默认按钮样式

改变 input file 样式(input  文件域)是很多前端朋友经常遇到的头疼问题,今天推荐两种改变 input file 样式的两种常用方法: 方法一: <input type="text" size="20" name="upfile" id="upfile" style="border:1px dotted #ccc"> <input type="button&quo