黑马程序员技术交流社区
标题:
C#中的问题
[打印本页]
作者:
小笨
时间:
2014-8-6 10:42
标题:
C#中的问题
本帖最后由 小笨 于 2014-8-6 10:53 编辑
在做入学考试测试题的时候,有个问题不会,是怎样从文本文档txt中读取文本?
作者:
陈君
时间:
2014-8-6 10:47
可以用StreamReader
作者:
_xixi_
时间:
2014-8-6 10:48
要用到文件操作,File.ReadAllLines(path); path是路径,从文本里面读取所有行,返回一个string 数组。
不懂可以查MSDN,转一个来自MSDN的例子。
using System;
using System.IO;
class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";
// This text is added only once to the file.
if (!File.Exists(path))
{
// Create a file to write to.
string[] createText = { "Hello", "And", "Welcome" };
File.WriteAllLines(path, createText);
}
// This text is always added, making the file longer over time
// if it is not deleted.
string appendText = "This is extra text" + Environment.NewLine;
File.AppendAllText(path, appendText);
// Open the file to read from.
string[] readText = File.ReadAllLines(path);
foreach (string s in readText)
{
Console.WriteLine(s);
}
}
}
复制代码
作者:
小笨
时间:
2014-8-6 10:52
_xixi_ 发表于 2014-8-6 10:48
要用到文件操作,File.ReadAllLines(path); path是路径,从文本里面读取所有行,返回一个string 数组。
不 ...
嗯,谢谢了!
作者:
邢凯
时间:
2014-8-6 10:57
进来学习学习
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2