using System;
using System.IO;
using System.Text;
class Test
{
public static void Main()
{
string path = @"c: empMyTest.txt";
try
{
// Delete the file if it exists.
if (File.Exists(path))
{
File.Delete(path);
}
// Create the file.
using (FileStream fs = File.Create(path))
{
Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
// Add some information to the file.
fs.Write(info, 0, info.Length);
}
// Open the stream and read it back.
using (StreamReader sr = File.OpenText(path))
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
}
catch (Exception Ex)
{
Console.WriteLine(Ex.ToString());
}
}
}
File.OpenText 方法:打开现有 UTF-8 编码文本文件以进行读取。
参数path:要打开以进行读取的文件。
返回值:指定路径上的 StreamReader。
File.CreateText 方法:创建或打开一个文件用于写入 UTF-8 编码的文本。
参数path:要打开以进行写入的文件。
返回值:一个 StreamWriter,它使用 UTF-8 编码写入指定的文件。
下面的示例创建一个文件,用于写入和读取文本。
using System;
using System.IO;
class Test
{
public static void Main()
{
string path = @"c: empMyTest.txt";
if (!File.Exists(path))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
}
}
// Open the file to read from.
using (StreamReader sr = File.OpenText(path))
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
}
}
下面的示例演示了 FileInfo 类的一些主要成员。
using System;
using System.IO;
class Test
{
public static void Main()
{
string path = Path.GetTempFileName();
FileInfo fi1 = new FileInfo(path);
if (!fi1.Exists)
{
//Create a file to write to.
using (StreamWriter sw = fi1.CreateText())
{
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
}
}
//Open the file to read from.
using (StreamReader sr = fi1.OpenText())
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
try
{
string path2 = Path.GetTempFileName();
FileInfo fi2 = new FileInfo(path2);
//Ensure that the target does not exist.
fi2.Delete();
//Copy the file.
fi1.CopyTo(path2);
Console.WriteLine("{0} was copied to {1}.", path, path2);
//Delete the newly created file.
fi2.Delete();
Console.WriteLine("{0} was successfully deleted.", path2);
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}
FileInfo.Open 方法 (FileMode):在指定的模式中打开文件。
参数 mode:一个 FileMode 常数,它指定打开文件所采用的模式(例如 Open 或 Append)。
返回值:在指定模式中打开、具有读/写访问权限且不共享的文件。
FileInfo.OpenRead 方法:该方法返回一个 FileShare 模式设置为 Read 的只读 FileStream 对象。
返回值:新的只读 FileStream 对象。
FileInfo.AppendText 方法:创建一个 StreamWriter,它向 FileInfo 的此实例表示的文件追加文本。
FileInfo.Create 方法 :创建文件。
下面的示例创建对文件的引用,然后使用 FileInfo.Create() 在磁盘上创建此文件。
using System;
using System.IO;
public class DeleteTest
{
public static void Main()
{
// Create a reference to a file.
FileInfo fi = new FileInfo("temp.txt");
// Actually create the file.
FileStream fs = fi.Create();
// Modify the file as required, and then close the file.
fs.Close();
// Delete the file.
fi.Delete();
}
}
下面的示例创建一个文件,向其中添加一些文本,然后从此文件中读取。
using System;
using System.IO;
using System.Text;
class Test
{
public static void Main()
{
string path = @"c: empMyTest.txt";
FileInfo fi = new FileInfo(path);
// Delete the file if it exists.
if (fi.Exists)
{
fi.Delete();
}
//Create the file.
using (FileStream fs = fi.Create())
{
Byte[] info =
new UTF8Encoding(true).GetBytes("This is some text in the file.");
//Add some information to the file.
fs.Write(info, 0, info.Length);
}
//Open the stream and read it back.
using (StreamReader sr = fi.OpenText())
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
}
}
FileInfo.OpenText 方法:创建使用 UTF8 编码、从现有文本文件中进行读取的 StreamReader。
返回值:使用 UTF8 编码的新 StreamReader。
FileInfo.CreateText 方法:创建写入新文本文件的 StreamWriter。
返回值:新的StreamWriter。
下面的示例说明 CreateText 方法和OpenText 方法。
using System;
using System.IO;
class Test
{
public static void Main()
{
string path = @"c: empMyTest.txt";
FileInfo fi = new FileInfo(path);
if (!fi.Exists)
{
//Create a file to write to.
using (StreamWriter sw = fi.CreateText())
{
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
}
}
//Open the file to read from.
using (StreamReader sr = fi.OpenText())
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
}
}作者: SunshineGirl 时间: 2014-1-14 08:56
有同学给你回答问题了,请及时设置成“提问结束”,这样版主才会给你们加分呀