File和FileInfo的区别总结

File类提供了创建、删除、复制、移动文件的静态方法

FileInfo类提供了创建、删除、复制、移动文件的实例方法。(不可继承)

 

 

File类的方法每次执行都要验证安全机制,所以在少量使用的时候用File效率高,但是如果用的多了 要用FileInfo效率高

 string path = @"D:/test.txt";
            if (!File.Exists(path))
            {

                using (StreamWriter sw = File.CreateText(path))
                {
                    sw.WriteLine("Hello");
                    sw.WriteLine("World");
                }
                using (StreamReader sr = File.OpenText(path))
                {
                    StringBuilder sb = new StringBuilder();
                    string str = "";
                    while ((str = sr.ReadLine()) != null)
                    {
                        sb.Append(str);
                    }
                }
                try
                {
                    string path2 = @"D:/temp.txt";
                    string pathNew = @"D:/tempNew.txt";
                    File.Delete(path2);
                    File.Copy(path2, pathNew);
                    File.Delete(pathNew);
                }
                catch (IOException ex)
                {
                }
            }
            string path3 = @"D:/net.txt";
            if (File.Exists(path3))
            {
                File.Delete(path);
            }
            using (FileStream fs = File.Create(path3))
            {
                byte[] byteData = new UTF8Encoding(true).GetBytes("this is some text in the text");
                fs.Write(byteData, 0, byteData.Length);
            }
            using (StreamReader sr = File.OpenText(path3))
            {
                StringBuilder sb = new StringBuilder();
                string str = "";
                while ((str = sr.ReadLine()) != null)
                {
                    sb.Append(str);
                }
            }
        }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
copyToFile是一个方法,用于将一个文件复制到另一个目标文件中。在引用中的Java代码示例中,copyToFile方法使用Apache Commons IO库中的FileUtils类来实现文件的复制操作。其中,源文件的路径是 "G:\\studynote\\StudyNote\\脑图--总结笔记\\1-Java语言核心.png",目标文件的路径是 "F:\\1-Java语言核心.png"。通过创建一个FileInputStream对象来读取源文件的内容,并使用FileUtils.copyToFile()方法将源文件的内容复制到目标文件中。最后,通过调用IOUtils.closeQuietly()方法关闭输入流。中的C#代码示例中,CopyToFile()方法用于将一个文件复制到另一个目标文件中。首先,确定源文件的路径为"D:\Source\Test.txt",然后指定目标文件夹的路径为项目下的NewTest文件夹。如果该文件夹不存在,则创建该文件夹。接下来,获取源文件的文件名,并将其与目标文件夹的路径组合成目标文件的路径。最后,使用FileInfo类的CopyTo()方法将源文件复制到目标文件夹中。是引用自一个博客文章,文章内容未提供与copyToFile方法相关的信息。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [JavaEE日常工作经验总结系列(十七)-----Apache中的copyToFile方法测试](https://blog.csdn.net/qq_27922023/article/details/81567358)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [C# Copy一个文件到另一个文件夹下](https://blog.csdn.net/weixin_30505225/article/details/95191992)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值