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. ios delegate 和 block

    //委托的协议定义 @protocol UpdateDelegate <NSObject> - (void)update; @end @interface Test : NSObject ...

  2. Android学习总结——Popup menu:弹出式菜单

    PopupMenu,弹出菜单,一个模态形式展示的弹出风格的菜单,绑在在某个View上,一般出现在被绑定的View的下方(如果下方有空间). 注意:弹出菜单是在API 11和更高版本上才有效的. 核心步 ...

  3. apache访问控制设置

    apache访问控制设置 (2009-03-17 11:24:36) 转载▼ 标签: it 杂谈   Order allow,deny    默认情况下禁止所有客户机访问 Order deny,all ...

  4. JS判断RadioButtonList是否有选中项

    提交表单之前对RadioButtonList控件的选中项进行判断: 方法一: <script type="text/javascript"> function chec ...

  5. Highcharts属性

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  6. PHP学习笔记三十六【try 二】

    <?php //定义一个顶级异常处理器 要定义在最上面 function my_exception($e) { echo "我是顶级异常处理:".$e->getMess ...

  7. Ubuntu eclipse 命令补全失效 (转载)

    我的eclipse 3.4,从ibm网站上下载解压后使用.发觉自动补全功能(alt + /)失效. 解决的办法: 1.(eclipse)window --> preferences --> ...

  8. iOS 发布证书错误 Your build settings specify a provisioning profile with the UUID, no provisioning profile was found

    解决办法 1.找到项目中的**.xcodeproj文件,点击右键,show package contents(打开包内容). 2.打开后找到project.pbxproj文件,用文本编辑器打开.其实就 ...

  9. .Net Memory -- GC基本知识

    参考资料: http://blogs.msdn.com/b/tess/archive/2008/04/17/how-does-the-gc-work-and-what-are-the-sizes-of ...

  10. 在现代渲染API下,封装跨平台渲染框架的尝试 - 资源管理

    小生资历浅薄,不讨论该主题的重要性与未来的意义,只是个人兴趣爱好平日对这个问题思考了很多,总觉得要写点东西记录下来.框架还没有定型,只是记录自己设计的过程. 系统要跨平台,首先得将平台相关的实现与平台 ...