using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO; namespace FileTest
{
class Program
{
static void Main(string[] args)
{
WriteFile();
ReadFile(); Console.ReadKey();
} /// <summary>
/// 写、读文件
/// </summary>
static void WriteFile()
{
string pathUrl = @"E:\我的任务\其他\MyCodes_VS2013\MyCodes\TestFolder\WriteAndReadFile.txt"; try
{
if (File.Exists(pathUrl))
{
#region 向文件写入数据 Console.WriteLine("");
//以WriteAllBytes方式向文件中写入数据,可以操作除文本文件外的文件类型,比如音频文件,视频文件等
Console.WriteLine("以WriteAllBytes方式向文件中写入数据");
byte[] contentInByte = Encoding.GetEncoding("gb2312").GetBytes("以WriteAllBytes方式向文件中写入数据。"); ;
File.WriteAllBytes(pathUrl, contentInByte); Console.WriteLine("");
//以WriteAllLines方式向文件中写入数据
Console.WriteLine("以WriteAllLine方式向文件中写入数据");
string[] contentInString = new string[] { "以WriteAllLine方式向文件中写入数据。" };
File.WriteAllLines(pathUrl, contentInString, Encoding.GetEncoding("gb2312")); Console.WriteLine("");
//以WriteAllText方式向文件中写入数据
Console.WriteLine("以WriteAllText方式向文件中写入数据");
File.WriteAllText(pathUrl, "以WriteAllText方式向文件中写入数据。", Encoding.GetEncoding("gb2312"));
#endregion
                    #region  以上三种方会将文件原有数据覆盖,若想不覆盖原有数据,可以用下面两种方法;实现在原有数据后追加数据
//AppendAllLines方式向文件写入数据
File.AppendAllLines(pathUrl, new string[] { "AppendAllLine方式向文件写入数据" }, Encoding.GetEncoding("gb2312"));
//AppendAllText方式向文件写入数据
File.AppendAllText(pathUrl, "AppendAllText方式向文件写入数据", Encoding.GetEncoding("gb2312"));
#endregion
}
else
{
File.Create(pathUrl);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + ex.StackTrace);
} } static void ReadFile()
{
string pathUrl = @"E:\我的任务\其他\MyCodes_VS2013\MyCodes\TestFolder\WriteAndReadFile.txt"; try
{
if (File.Exists(pathUrl))
{
Console.WriteLine("");
#region 读取文件数据 //以ReadAllBytes方式读取数据
Console.WriteLine("以ReadAllBytes方式读取文件数据:");
byte[] readContentInByte = File.ReadAllBytes(pathUrl);
Console.WriteLine(Encoding.GetEncoding("gb2312").GetString(readContentInByte)); Console.WriteLine("");
//以ReadAllLine方式读取数据
Console.WriteLine("以ReadAllLine方式读取文件数据:");
string[] readContentInString = File.ReadAllLines(pathUrl, Encoding.GetEncoding("gbk"));
Console.WriteLine(readContentInString.Aggregate(string.Empty, (result, current) => result += current)); Console.WriteLine("");
//以ReadAllText方式读取数据
Console.WriteLine("以ReadAllTex方式读取文件数据:");
Console.WriteLine(File.ReadAllText(pathUrl, Encoding.GetEncoding("GBK"))); #endregion
}
else
{
File.Create(pathUrl);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + ex.StackTrace);
} }
}
}

C#向文件写、读数据的更多相关文章

  1. C 常用的输入输出 栈 哈希 文件写读 实现 字符串处理

    #include "stdafx.h"#include <stdio.h>#include <string.h>#include <stdlib.h& ...

  2. java后端导入excel模板和导入excel文件去读数据

    模板转载地址:https://www.cnblogs.com/zhangyangtao/p/9802948.html 直接上代码(我是基于ssm写的demo,导入文件目前只能读取.xls后缀的exce ...

  3. python文件处理-读、写

    Python中文件处理的操作包括读.写.修改,今天我们一起来先学习下读和写操作. 一.文件的读操作 例一: #文件读操作 f = open(file="first_blog.txt" ...

  4. python3中文件的读与写

    Python open() 函数用于打开一个文件,并返回文件对象,在对文件进行处理过程都需要使用到这个函数,如果该文件无法被打开,会抛出错误 完整语法:open(file, mode='r', buf ...

  5. Json文件转换为Excel文件!涉及读文件,时间戳转化,写文档

    一. 思路 今天接到个小任务,让把json文件转换成excel文件,按照列展开. 思路:既然json已经都已经是现成的,那直接将json文件做读操作,在通过不同的key,找到对应的信息,在存到单元格中 ...

  6. (数据科学学习手札143)为geopandas添加gdb文件写出功能

    本文示例代码已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 大家好我是费老师,很多读者朋友跟随着我先前写作的 ...

  7. java实现服务端守护进程来监听客户端通过上传json文件写数据到hbase中

    1.项目介绍: 由于大数据部门涉及到其他部门将数据传到数据中心,大部分公司采用的方式是用json文件的方式传输,因此就需要编写服务端和客户端的小程序了.而我主要实现服务端的代码,也有相应的客户端的测试 ...

  8. C语言中怎么将文件里的数据创建到(读到)链表中?

    定义的结构体: struct student { ]; //学生学号 ]; //学生姓名 struct student *next; //next 指针 指向 struct student 类型的变量 ...

  9. java向文件写数据的3种方式

    下边列举出了三种向文件中写入数据的方式,当然还有其他方式,帮助自己理解文件写入类的继承关系.类的关系: file->fileoutputstream->outputstreamWriter ...

随机推荐

  1. (转)Apache+Tomcat集群配置

    本文Apache+Tomcat集群配置 基于最新的Apache和Tomcat,具体是2011年4月20日最新的Tomcat和Apache集群和负载均衡配置. 准备环境 Apache Apache是ht ...

  2. Staple: Complementary Learners For Real-time Tracking Tracking

    转载注明出处: http://www.cnblogs.com/sysuzyq/p/6169414.html By 少侠阿朱 讨论班上的PPT 1.同学大家好.今天给大家讲一篇单目标跟踪的论文,方法比较 ...

  3. 《Swift开发指南》国内第一本Swift图书上市了

    <Swift开发指南>国内第一本Swift图书上市了 既<courseId=799262">苹果Swift编程语言开发指南>视频教程地址:courseId=79 ...

  4. boost库asio详解1——strand与io_service区别

    namespace { // strand提供串行执行, 能够保证线程安全, 同时被post或dispatch的方法, 不会被并发的执行. // io_service不能保证线程安全 boost::a ...

  5. 涂抹Oracle笔记1-创建数据库及配置监听程序

    一.安装ORACLE数据库软件及创建实例OLTP:online transaction processing 指那些短事务,高并发,读写频繁的数据库系统.--DB_BLOCK_SIZE通常设置较小.O ...

  6. asp.net mvc 访问.html文件

    把html页面放在除Views文件夹外的任意文件夹,如Htmls--123.html,url直接访问便可,如 http://yourname/htmls/123.html 可以在路由表中排出不需要路由 ...

  7. PHP学习笔记二十八【抽象类】

    <?php //定义一个抽象类.主要用来被继承 //如果一个类继承了抽象类,则它必须实现该抽象类的所有抽象方法(除非它自己也是抽象类) // abstract class Animal{ pub ...

  8. Qt调用外部程序QProcess通信

    mainwindow.cpp文件: -------------------------------- #include "mainwindow.h" #include " ...

  9. 图的割点 桥 双连通(byvoid)

    [点连通度与边连通度] 在一个无向连通图中,如果有一个顶点集合,删除这个顶点集合,以及这个集合中所有顶点相关联的边以后,原图变成多个连通块,就称这个点集为割点集合.一个图的点连通度的定义为,最小割点集 ...

  10. C++ 语言特性的性能分析

    转载:http://www.cnblogs.com/rollenholt/archive/2012/05/07/2487244.html      大多数开发人员通常都有这个观点,即汇编语言和 C 语 ...