(三)C#关于txt文件的读取和写入
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; namespace textTest
{
public partial class WebForm1 : System.Web.UI.Page
{
private string downLoadFileName = "C:/Users/zuomeiyan/Desktop/DISPUTE.TXT";
//读取系统临时文件
private string savePath =System.Environment.GetEnvironmentVariable("TEMP")+"\\new.txt"; protected void Page_Load(object sender, EventArgs e)
{
ReadFromTxt();
}
protected void ReadFromTxt()
{
//读取文件
FileStream fs = new FileStream(downLoadFileName, FileMode.Open);
StreamReader sr = new StreamReader(fs, Encoding.UTF8);
//try是为了防止代码出现问题是文件流不能关闭
try
{
//新建list存放读取的数据
List<string> datalist = new List<string>();
//文件中不到末尾就一直读取
while (!sr.EndOfStream)
{
//读取每一行
string strDataOld = sr.ReadLine();
string strData = "";
//如果文件的每一行的第一个字符和最后一个字符是|,则去除
if (strDataOld[] == '|')
{
strData = strDataOld.Substring(, strDataOld.Length - );
}
if (strDataOld[strDataOld.Length - ] == '|')
{
strData = strDataOld.Substring(, strDataOld.Length - );
}
//加到list中
datalist.Add(strData);
}
var lineNo = datalist.Count;
//去除不用的行,本代码中是出去第一行第三行和最后一行
List<string> newdatalist = new List<string>();
for (int i = ; i < lineNo - ; i++)
{
if (i != )
{
newdatalist.Add(datalist[i]);
}
}
WriteToNewTxt(newdatalist);
}
catch
{
}
finally
{
//关闭文件流
sr.Close();
fs.Close();
}
}
protected void WriteToNewTxt(List<string> lst)
{
//将生成的新list写入文件
FileStream fsnew = new FileStream(savePath, FileMode.Create);
StreamWriter sw = new StreamWriter(fsnew, Encoding.UTF8);
try
{ sw.Flush();
for (int i = ; i < lst.Count; i++)
{
sw.WriteLine(lst[i]);
}
}catch {}
finally
{
//关闭此文件
sw.Flush();
sw.Close();
fsnew.Close();
}
}
}
}
(三)C#关于txt文件的读取和写入的更多相关文章
- c++学习笔记—c++对txt文件的读取与写入
一.文件的输入输出 头文件fstream定义了三个类型支持文件IO:ifstream从给定文件读取数据.ofstream向一个给定文件写入数据.fstream读写给定数据.这些类型与cin和cout的 ...
- c# txt文件的读取和写入
我们在工程实践中经常要处理传感器采集的数据,有时候要把这些数据记录下来,有时候也需要把记录下来的数据读取到项目中.接下来我们用C#演示如何对txt文件进行读写操作.我们要用到StreamReader ...
- C# txt文件的读取与写入
C#创建记事本方法一://创建对象 FileStream stream = new FileStream(@"d:\aa.txt",FileMode.Create);//fileM ...
- 《程序实现》从xml、txt文件里读取数据写入excel表格
直接上码 import java.io.BufferedReader; import java.io.DataInputStream; import java.io.File; import java ...
- c++对txt文件的读取与写入
转自:http://blog.csdn.net/lh3325251325/article/details/4761575 #include <iostream> #include < ...
- c#中对txt文件的读取与写入,针对二维数组
class Program { ; ; static string[,] str = new string[ROW, COL]; static void Main(string[] args) { R ...
- 一些常用的文本文件格式(TXT,JSON,CSV)以及如何从这些文件中读取和写入数据
TXT文件: txt是微软在操作系统上附带的一种文本格式,文件以.txt为后缀. 从txt文件中读取数据: with open ('xxx.txt') as file: data=file.readl ...
- 编写Java程序,在硬盘中选取一个 txt 文件,读取该文档的内容后,追加一段文字“[ 来自新华社 ]”,保存到一个新的 txt 文件内
查看本章节 查看作业目录 需求说明: 在硬盘中选取一个 txt 文件,读取该文档的内容后,追加一段文字"[ 来自新华社 ]",保存到一个新的 txt 文件内 实现思路: 创建 Sa ...
- Java I/O---RandomAccessFile类(随机访问文件的读取和写入)
1.JDK API中RandomAccessFile类的描述 此类的实例支持对随机访问文件的读取和写入.随机访问文件的行为类似存储在文件系统中的一个大型 byte 数组.存在指向该隐含数组的光标或索引 ...
随机推荐
- iOS的动画效果类型及实现方法
实现iOS漂亮的动画效果主要有两种方法, 一种是UIView层面的, 一种是使用CATransition进行更低层次的控制, 第一种是UIView,UIView方式可能在低层也是使用CATransit ...
- C++(MFC)
C++(MFC) 1.关联变量(与控件关联): (1)一组单选按钮:需要将第一个单选按钮的Group选项设为true,则单选按钮就为一组(组框为标示作用).选中第一个则为0,第二个为1,依次类推(P2 ...
- MYSQL存储过程实现in传入参数 where in('1','2')
android 服务器端开发中遇到这么一个问题: 突然发现将字符串传入到存储过程,参数为 '1','2' ,竟然执行无效 所以看到网上有在存储过程中直接拼凑sql的代码,今天也试了一下,可以执行了, ...
- DevExpress GridControl使用(转)
DevExpress GridControl使用 (一)原汁原味的表格展示 Dev控件中的表格控件GridControl控件非常强大.不过,一些细枝末节的地方有时候用起来不好找挺讨厌的.使用过程中,多 ...
- C# - 设置DLL的属性Embed Interop Type 设为False
错误: Error msg: A reference was created to embedded interop assembly. because of an indirect referenc ...
- CentOS 大量的TIME_WAIT解决方法
CentOS 大量的TIME_WAIT解决方法 最近个人博客总是出现无法打开的现象,具体表现为,打开页面需要等待n长时间,登陆系统后发现系统存在大量TIME_WAIT状态的连接,google了一下解决 ...
- Asp.Net修改上传文件大小限制(修改web.config)
i. Configuration节点下 <system.webServer> <security> <requestFiltering> <!--单位为字节 ...
- ubuntu 安装git
问题描述: ubuntu安装git 问题解决: (1)ubuntu下载git 注: 使用命令apt-get install git安装 (2)查看g ...
- 【UVA】【11427】玩纸牌
数学期望 也是刘汝佳老师白书上的例题……感觉思路很神奇啊 //UVA 11427 #include<cmath> #include<cstdio> #include<cs ...
- hibernate Session
转: http://kayo.iteye.com/blog/204143 Session 接口 Session 接口对于Hibernate 开发人员来说是一个最重要的接口.然而在Hibernate 中 ...