• System.IO 应用一


    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net;
    using System.IO;

    namespace TestWebClientClass
    {
        class Program
        {
            static void Main(string[] args)
            {
                WebClient client = new WebClient();
                client.BaseAddress = "http://www.microsoft.com/:";
                string data = client.DownloadString("office");
                Console.WriteLine(data);

                //创建一个文本文件,把返回的字符串data写入文本文件
                string m_path = @"c: ile.txt";
                //File.Create(m_path); //此方法的返回类型是FileStream  File.Create方法在这里会产生异常:线程有异常
                File.CreateText(m_path); //File.CreateText在这里不会产生异常
                StreamWriter sw = new StreamWriter(m_path, false);
                //StreamWriter sw = File.CreateText(m_path);这一条语句和上面2条语句等价
                sw.Write(data);
                sw.Close();//必须关闭才会把字符串写入文件。
                Console.WriteLine("写入成功");
                Console.ReadLine();

                ////和上面的作用一样
                //StreamWriter sw = new StreamWriter( @"c: ile.txt", false);//为false则改写,为true则追加在文件后面,true和false只决定是否改写或追加,与文件存不存在没有关系。                                                      
                //sw.Write(data);
                //sw.Close();//必须关闭才会把字符串写入文件。
                ////下面是为了测试FileInfo类而加进来的代码
                //FileInfo fi = new FileInfo(@"c: ile.txt");     
                //Console.WriteLine("写入成功");
                //Console.WriteLine(fi.Extension);//.txt
                //Console.WriteLine (fi.Name);//  file.txt
                //Console.WriteLine (fi.Length );// 3741
                //Console.WriteLine (fi.ToString());//c: ile.txt
                //Console .WriteLine (fi.FullName );//c: ile.txt  
                //Console.WriteLine(fi.Exists);//true
                //Console.ReadLine();

                ////和上面的作用一样
                //File.WriteAllText(@"c: ile01.txt", data, Encoding.UTF8);//创建新文件,如果文件存在则改写文件
                ////File.Encrypt(@"c: ile01.txt");//加密文件,加密后文件成绿色
                ////File.Decrypt(@"c: ile01.txt");//加密文件,解密后文件恢复原来的颜色
                //Console.WriteLine("写入成功");
                //Console.ReadLine();

                //下面的示例在指定路径中创建一个文件,将一些信息写入该文件,再从文件中读取。
                string path = @"c: empMyTest.txt";
                try
                {
                    // Delete the file if it exists.
                    if (File.Exists(path))
                    {
                        // Note that no lock is put on the
                        // file and the possibliity exists
                        // that another process could do
                        // something with it between
                        // the calls to Exists and Delete.
                        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.Create(string Path )//创建文件,此方法的返回类型是FileStreamv,如果指定的文件不存在,则创建该文件;如果存在并且不是只读的,则将改写其内容。
                //File.CreateText(string Path ) //创建或打开一个文件用于写入UTF-8编码的文本 ,此方法的返回类型是StreamWriter.如果由 path 指定的文件不存在,则创建该文件。如果该文件确实存在,则改写其内容。允许其他线程在文件打开后读取该文件。
                //File.CreateText方法,相当于调用StreamWriter类的构造函数,所以指定路径的文件可以存在也可以没有存在
                //File.AppendAllText(string Path ,string contents);//将指定的字符串追加到文件中,如果文件还不存在则创建该文件
                //File.AppendAllText(string Path,string contents,Encoding encoding );
                //File.Open (string Path ,FileMode mode)  //此方法的返回类型是FileStream
                //File.Open (string Path ,FileMode mode,FileAccess access)
                //File.Copy (这里面有两个参数或三个参数)//实现文件复制
                //File.Encrypt(string Path)//将某个文件加密,使得只有加密该文件的帐户才能将其解密
                //File.Decrypt (string Path)//解密由当前帐户用Systen.IO.File.Encrtpt(System.String)方法加密的文件
                //File.Delete (string Path )//删除指定的文件。如果指定的文件不存在,则不引发异常
                //File.Exists (string Path )//确定指定的文件是否存在,返回值为bool
                //File.Move(string sourceFileName,string destFileName)//移动文件夹,并可为文件指定新名字
                //File.OpenRead(string Path) //打开现有文件以进行读取 此方法的返回类型是FileStream
                //File.OpenWrite(string Path)//打开现有文件以进行写入 此方法的返回类型是FileStream
                //File.OpenText (string Path)//返回类型为SteamReader,打开现有UTF-8编码文本文件以进行读取,此方法相当于掉用StreamReader的构造函数,所以要求指定路径的文件必须已经存在,否则产生异常
                //File.ReadAllText (string Path ) //返回值为string,打开一个文件,使用指定的 编码读取文件的所有行,然后关闭文件
                //File.ReadAllText (string Path,Encoding encoding )

            }
        }
    }


    本文来自CSDN博客:http://blog.csdn.net/terry001/archive/2007/08/28/1762478.aspx

  • 相关阅读:
    OpenCL、CUDA
    最小和最廉价的超级计算机,DIY的
    组装属于您自己的Tesla个人超级计算机
    多处理器系统
    开源项目Spark简介
    基于Cassandra的日志和分布式小文件存储系统【1】
    网络广告js备忘【2】
    网络广告js备忘【1】
    成功产品的意外
    Cassandra HBase和MongoDb性能比较
  • 原文地址:https://www.cnblogs.com/KimhillZhang/p/1746861.html
Copyright © 2020-2023  润新知