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. 新手使用ThinkPHP3.2.3的命名空间问题

    ThinkPHP3.2.3的命名空间问题 命名空间的出现是为了避免命名冲突. 我们在TP3.2.3的Collection和Model的创建过程中经常会遇到这样的两行代码: 这是在控制器中的写法.其中n ...

  2. Hadoop 写SequenceFile文件 源代码

    package com.tdxx.hadoop.sequencefile; import java.io.IOException; import org.apache.hadoop.conf.Conf ...

  3. android CMWAP, CMNET有何差别

    什么是CMNET,什么是CMWAP? 答:CMWAP和CMNET仅仅是中国移动为其划分的两个GPRS接入方式.中国移动对CMWAP作了一定的限制,主要表如今CMWAP接入时仅仅能訪问GPRS网络内的I ...

  4. HDU 1248 寒冰王座(全然背包:入门题)

    HDU 1248 寒冰王座(全然背包:入门题) http://acm.hdu.edu.cn/showproblem.php?pid=1248 题意: 不死族的巫妖王发工资拉,死亡骑士拿到一张N元的钞票 ...

  5. OutputCache各参数的说明

    OutputCache各参数的说明 Duration 缓存时间,以秒为单位,这个除非你的Location=None,可以不添加此属性,其余时候都是必须的. Location Location当被设置为 ...

  6. Android生命周期注意事项

                                    生命周期图解     以下英文引用全部来自google官方文档说明,方便理解. onCreate (Bundle savedInstan ...

  7. Spring MVC中数据绑定(转)

    Spring MVC中数据绑定 比如Product有一个createTime属性,是java.util.Date类型.那么最简单的转型处理是,在SimpleFormController中覆盖initB ...

  8. I/O多路复用之epoll

    1.select.poll的些许缺点 先回忆下select和poll的接口 int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set ...

  9. POJ 2140 Herd Sums

    http://poj.org/problem?id=2140 Description The cows in farmer John's herd are numbered and branded w ...

  10. busybox中tftp服务器使用命令

    参数说明:-l 是local的缩写,后跟存在于Client的源文件名,或下载Client后重命名的文件名.-r 是remote的缩写,后跟Server即PC机tftp服务器根目录中的源文件名,或上传S ...