一、File

1、File为静态类

File类,是一个静态类,支持对文件的基本操作,包括创建,拷贝,移动,删除和打开一个文件。File类方法的参量很多时候都是路径path。主要提供有关文件的各种操作,在使用时需要引用System.IO命名空间。

常用的方法

二、FileStream

FileStream文件流 只能处理原始字节(raw byte)。FileStream 类可以用于任何数据文件,而不仅仅是文本文件。FileStream 对象可以用于读取诸如图像和声音的文件,FileStream读取出来的是字节数组,然后通过编码转换将字节数组转换成字符串。

string str = "今天天气好晴朗,处处好风光";
byte[] buttf = Encoding.Default.GetBytes(str);
//文件流的写入
using (FileStream fscreat = new FileStream(path, FileMode.Append, FileAccess.Write))
{
fscreat.Write(buttf, , buttf.Length);
}

三、StreamWriter

StreamWriter 类主要用于向流中写入数据

属性或方法 作用
bool AutoFlush 属性,获取或设置是否自动刷新缓冲区
Encoding Encoding 只读属性,获取当前流中的编码方式
void Close() 关闭流
void Flush() 刷新缓冲区
void Write(char value) 将字符写入流中
void WriteLine(char value) 将字符换行写入流中
Task WriteAsync(char value) 将字符异步写入流中
Task WriteLineAsync(char value)  将字符异步换行写入流中
    class Program
{
static void Main(string[] args)
{
string path = @"D:\\code\\test.txt";
//创建StreamWriter 类的实例
StreamWriter streamWriter = new StreamWriter(path);
//向文件中写入姓名
streamWriter.WriteLine("小张");
//向文件中写入手机号
streamWriter.WriteLine("");
//刷新缓存
streamWriter.Flush();
//关闭流
streamWriter.Close();
}
}

四、StringWriter

实现用于将信息写入字符串的 TextWriter。 信息存储在基础 StringBuilder 中。

using System;
using System.IO; class StringRW
{
static void Main()
{
string textReaderText = "TextReader is the abstract base " +
"class of StreamReader and StringReader, which read " +
"characters from streams and strings, respectively.\n\n" + "Create an instance of TextReader to open a text file " +
"for reading a specified range of characters, or to " +
"create a reader based on an existing stream.\n\n" + "You can also use an instance of TextReader to read " +
"text from a custom backing store using the same " +
"APIs you would use for a string or a stream.\n\n"; Console.WriteLine("Original text:\n\n{0}", textReaderText); // From textReaderText, create a continuous paragraph
// with two spaces between each sentence.
string aLine, aParagraph = null;
StringReader strReader = new StringReader(textReaderText);
while(true)
{
aLine = strReader.ReadLine();
if(aLine != null)
{
aParagraph = aParagraph + aLine + " ";
}
else
{
aParagraph = aParagraph + "\n";
break;
}
}
Console.WriteLine("Modified text:\n\n{0}", aParagraph); // Re-create textReaderText from aParagraph.
int intCharacter;
char convertedCharacter;
StringWriter strWriter = new StringWriter();
strReader = new StringReader(aParagraph);
while(true)
{
intCharacter = strReader.Read(); // Check for the end of the string
// before converting to a character.
if(intCharacter == -) break; convertedCharacter = Convert.ToChar(intCharacter);
if(convertedCharacter == '.')
{
strWriter.Write(".\n\n"); // Bypass the spaces between sentences.
strReader.Read();
strReader.Read();
}
else
{
strWriter.Write(convertedCharacter);
}
}
Console.WriteLine("\nOriginal text:\n\n{0}",
strWriter.ToString());
}
}

File、FileStream、StreamWriter、StringWriter文件使用总结的更多相关文章

  1. File FileStream StreamWriter StreamReader文件读写操作方法

    string path = "D:\\AccountChecking\\Test.txt"; string content = "abcdefg\r\nhigklmn\r ...

  2. File FileStream StreamReader StreamWriter C#

    存在各种各样的IO设备,比如说文件File类(字符串文件和二进制文件),可以直接使用File类对文件进行读写操作. 这些各种IO的读取和写入是通过流的形式实现的,基类为Stream,针对各种不同的IO ...

  3. 20151024_003_C#基础知识(File / FileStream / StreamReader/StreamWriter)

    1:绝对路径和相对路径 绝对路径:通过给定的路径直接能在我的电脑中找到这个文件. 相对路径:文件相对于应用程序的路径. 2:编码格式 乱码:产生乱码的原因,就是你保存这个文件所采用的编码,跟你打开这个 ...

  4. 【File】递归删除文件夹中子级文件/夹,并删除文件夹

    今天有这样一个需求,需要删除某一个文件夹,但是文件夹中还有子级的文件 或者还可能会有文件夹在里面,所以就需要使用一个简单的递归才能将文件夹删除成功,包括文件夹中的子级文件/夹.!!! 其实很简单,就一 ...

  5. Filestream复制视频文件

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  6. Java文件File操作一:文件的创建和删除

    一.简述 File 文件类,主要对文件进行相关操作.常用的File操作有:文件(夹)的创建.文件(夹)的删除,文件的读入和下载(复制)等: 二.文件(夹)的创建和删除 1.创建过程 实例: //cre ...

  7. java File delete 无法删除文件的原因。

    windows下使用java.io.File.delete()方法删除文件时,返回值为true. 但是本地文件仍然存在,也就是说没有删除成功. 这时候你要检查下你传进来的文件目录格式是否正确. 正确: ...

  8. 如何用一张图片代替 'input:file' 上传本地文件??

    今天去面试,碰到了一道题,也许是因为紧张或者喝水喝多了,一时竟然没有转过弯来,回来之后一细想原来这么简单,哭笑不得,特此记录一下! 原题是这样的:  如何用一张图片代替 'input:file' 上传 ...

  9. 关于Java里面File类创建txt文件重复???

    private JButton getOpenButton() { if (openButton == null) { openButton = new JButton(); openButton.s ...

  10. Linux:file命令显示自定义文件类型

    file 命令可以查看文件类型信息,原理见: 非常Linux-file命令与magic file 修改 /ect/magic 文件后,可用 file 命令显示自定义文件类型信息. man magic ...

随机推荐

  1. The command '/bin/sh -c unzip -o php-7.2.23-src.zip' returned a non-zero code: 1

    Dockerfile 内容 #centos7 nginx php redis inotify FROM centos:7 MAINTAINER INFOBIRD RUN yum -y update & ...

  2. SpringBoot项目配置Date类型数据自动格式转换

    application.yml加入配置 spring: jackson: date-format: yyyy-MM-dd HH:mm:ss time-zone: GMT+8

  3. Anaconda的CondaHTTPError问题

    在Anaconda+Spyder配置Opencv的过程中遇到了缺乏cv2的问题,当时我在cmd的窗口(管理员身份)中输入了如下命令 conda install --channel https://co ...

  4. HTML中使用Vue+Dhtmlxgantt制作任务进度图

    HTML中使用Vue+Dhtmlxgantt制作任务进度图 Dhtmlxgantt官网: https://dhtmlx.com/docs/products/dhtmlxGantt/ 参考文章 甘特图配 ...

  5. 读取Core下的appsettings.json的值的时候中文乱码

    这个百度一下一大堆,我就用的这个:然后重新生成一次就好了. 2.有的是更改VS的什么高级保存之类的,我记得之气设置过, 然后就是:这篇文章

  6. Nginx作为负载均衡服务器——server参数讲解

    upstream举例 upstream backend { server backend1.ecample.com weight = 5; # wwight 代表权重 server backend2. ...

  7. 9.Break和Continue

    Break直接跳出循环和Continue略过本次循环,循环继续执行: Break在任何循坏语句的主体部分,均可用break控制循环的流程.break用于强制退出循环,不执行循环体中的语句,后边语句继续 ...

  8. Spring MVC中的拦截器Interceptor

    谈谈spring中的拦截器 在web开发中,拦截器是经常用到的功能.它可以帮我们验证是否登陆.预先设置数据以及统计方法的执行效率等等.今天就来详细的谈一下spring中的拦截器.spring中拦截器主 ...

  9. MySQL基础知识清单

    学习大纲(★为重点,√其次) 一.为什么要学习数据库 二.数据库的相关概念 DBMS.DB.SQL 三.数据库存储数据的特点 四.初始MySQL MySQL产品的介绍 MySQL产品的安装 ★ MyS ...

  10. 学_汇编语言_王爽版 要点采集笔记(未完待续…)

    第一章 基础知识 存储器(内存)存放CPU工作的指令和数据(CPU可以直接使用的信息在内存中存放):指令和数据都是二进制数没有任何区别,由CPU决定是数据还是指令 内存单元:存储器被分为若干个存储单元 ...