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. mac下显示隐藏文件

    一.在终端中 ls -a就可以查看隐藏文件. 显示和隐藏的命令例如以下: 显示:defaults write com.apple.finder AppleShowAllFiles -bool true ...

  2. Golang性能调优入门

    如何利用golang自带的profile工具进行应用程序的性能调优,前一段时间我做的日志分析系统在线上遇到了一个问题,就是分任务的系统down机了,日志处理延迟了10几个小时,这个时候任务分发系统重启 ...

  3. UIImage与UIColor互转

    Objective-C UIColor -> UIImage ? 1 2 3 4 5 6 7 8 9 10 11 - (UIImage*) createImageWithColor: (UICo ...

  4. POJ-1118(超时,但未找到原因)

    #include<iostream> #include<map> #include<vector> using namespace std; //y=kx+z ty ...

  5. 调试Linq的时候获得相对应的SQL

    (query as System.Data.Objects.ObjectQuery).ToTraceString()

  6. nodejs报错 events.js:72 throw er; // Unhandled 'error' event

    var http = require('http'); var handlerRequest = function(req,res){ res.end('hello');}; var webServe ...

  7. tomcat6.0目录和server.xml详解

    Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,目前最新版本是6.x,相对5.x性能提升很多,主要优化了内存使用,增强IO能力,重新构造集群功能. 近期对Tomcat6.x作深入学习, ...

  8. SQL Server 2008 geometry 数据类型

    摘自SQL Server 2008帮助 平面空间数据类型 geometry 是作为 SQL Server 中的公共语言进行时 (CLR) 数据类型实现的.此类型表示欧几里得(平面)坐标系中的数据. 注 ...

  9. java一点内容

    1.JAVA有哪些基本数据类型 String是不是基本数据类型 Java语言提供了八种基本类型: 六种数字类型(四个整数型,两个浮点型) 字节型byte 8位 短整型short 16位 整型int 3 ...

  10. 移动web前端的一些硬技能(二)动手前必须掌握的基本常识

    记得刚开始接触移动端web的时候,书和网上的资料都不多,查起来很费劲,现在比以前要好很多了,可是还是会有一些刚接触移动端的朋友会问我一些我最初会遇到的问题,或许是书本写的并不那么重,也或许是这些知识写 ...