All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
Visual,C#命名空间详解_[Asp.Net教程]
C#中程序调试之终止执行
AJAX使用技巧:如何处理书签和翻页按扭_.net资料_编程技术
Visual C++ 6.0教程:c++程序的组成之关键字
ASP.NET中通过对话框方式下载文件_.net资料_编程技术
总结:一些PHP学习过程中的心得和经验_php资料_编程技术
ADO.NET删除数据库中数据
SQL SERVER数据库开发之触发器的应用_[SQL Server教程]
FCKEditor在Asp.net环境下的配置安装_.net资料_编程技术
PHP教程:PHP中对文件和目录的操作方法_[PHP教程]
Reading number is top 10 pictures
Hunan province aizhai super-large suspension bridge open to traffic and 4 world first2
日本小萝莉1
Angie Chiu vijara myth2
Sora aoi mirror memorial classics5
The cat shit
Wear bikinis cock
Park waits to have her picture taken exposed
Exquisite decoration is not paying too much1
运动的范冰冰3
BingBingFan apple dew point photo gallery5
Download software ranking
Wild things 2
The hero
Tram sex maniac 2 (H) rar bag19
asp.net技术内幕
VC++6.0培训教程
Boxer's Top ten classic battle1
Sora aoi - one of more PK
少妇苏霞全本
Unix video tutorial2
天龙八部最新服务端
delv published in(发表于) 2014/1/23 3:14:29 Edit(编辑)
在c#中如何操作文本文件_[Asp.Net教程]

在c#中如何操作文本文件_[Asp.Net教程]

在c#中如何操作文本文件_[Asp.Net教程]

// C#文本文件操作
//如何向现有文件中添加文本


using System;using System.IO;
class Test {
public static void Main() {
// Create an instance of StreamWriter to write text to a file. // The using statement also closes the StreamWriter. using
(StreamWriter sw = new StreamWriter("TestFile.txt"))
{// Add some text to the file.
sw.Write("This is the ");
sw.WriteLine("header for the file.");
sw.WriteLine("-------------------");
// Arbitrary objects can also be written to the file.
sw.Write("The date is: ");
sw.WriteLine(DateTime.Now);
}
}
}


//如何创建一个新文本文件并向其中写入一个字符串。WriteAllText方法可提供类似的功能。
using System;
using System.IO;
public class TextToFile {
private const string FILE_NAME = "MyFile.txt";
public static void Main(String[] args)
{
if (File.Exists(FILE_NAME))
{
Console.WriteLine("{0} already exists.", FILE_NAME);
return;
}
using (StreamWriter sw = File.CreateText(FILE_NAME))
{
sw.WriteLine ("This is my file.");
sw.WriteLine ("I can write ints{0} or floats {1}, and so on.", 1, 4.2);
sw.Close();
}
}
}



//如何从文本文件中读取文本
using System;using System.IO;
class Test {
public static void Main()
{
try{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader("TestFile.txt"))
{String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null) {
Console.WriteLine(line);
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
}


//在检测到文件结尾时向您发出通知。通过使用 ReadAll 或 ReadAllText 方法也可以实现此功能。
using System;using System.IO;
public class TextFromFile {
private const string FILE_NAME= "MyFile.txt";
public static void Main(String[] args)
{
if (!File.Exists(FILE_NAME))
{
Console.WriteLine("{0} does not exist.", FILE_NAME);
return;
}
using (StreamReader sr = File.OpenText(FILE_NAME))
{
String input;
while ((input=sr.ReadLine())!=null){
Console.WriteLine(input);
}
Console.WriteLine ("The end of the stream has been reached.");
sr.Close();
}
}


来源:csdn







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.