C#按行读取、写入txt文件
最近帮一个老学长,准研究生,做一个入学要交的作业,要求是C#/Python语言,我就想熟悉下C#。
大概需求如下图展示:
直接看代码:=====================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 作业
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void SelectedBtb_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Multiselect = false;//该值确定是否可以选择多个文件
dialog.Title = "请选择文件夹";
dialog.Filter = "文本文件(*.txt*)|*.txt*";
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string file = dialog.FileName;
this.txtPath.Text = file;
}
else
{
MessageBox.Show("未选中txt文件");
}
}
private void Button1_Click(object sender, EventArgs e)
{
//读取文件的位置
string readPath = this.txtPath.Text.Trim();
//存储文件的位置及名字
string writePath = this.textBox1.Text.Trim();
//按行读取txt
string[] lines = File.ReadAllLines(readPath, System.Text.Encoding.Default);
//list用于存目标列中的、转为double类型的数据
List<double> list = new List<double>();
//list_xxx用于存分级数据
List<double> list_1 = new List<double>();
List<double> list_2 = new List<double>();
List<double> list_3 = new List<double>();
List<double> list_4 = new List<double>();
//为了方便double数值与科学计数法的数值转换,存在键值对中
Dictionary<double, string> dic = new Dictionary<double, string>();
foreach (var item in lines)
{
//分割数据
string[] str = item.Split(new char[] { ' ' });
//得到目标列数据
string goalStr = str[2];
//将目标列的数据转为double类型
double goalData = double.Parse(goalStr);
//将得到的double的数据存起来
list.Add(goalData);
//以double类型数据作为键,科学计数值作为值,目的是:找到科学记数法的值容易找到
dic[goalData] = goalStr;
}
//归类,找到自己属于哪一分级
foreach (var item in list)
{
if (item < -0.005)
{
list_1.Add(item);
}
else if (item > -0.005 && item < -0.003)
{
list_2.Add(item);
}
else if (item > -0.003 && item < -0.001)
{
list_3.Add(item);
}
else
{
list_4.Add(item);
}
}
//注意:分块只能4 2 3 1这样的顺序,不然会导致越界
//i为lines的索引
int i = 0;
for (; i < list_4.Count; i++)
{
StreamWriter sw = CreateAppendTxt(writePath);
//按行写入
sw.WriteLine(lines[i] + " " + dic[list_4[i]] + "_分块4");
sw.Close();
}
//为了验证是否长度正确
Console.WriteLine(i);
int j = 0;
for (; i < (list_4.Count + list_2.Count) && j < list_2.Count; i++)
{
StreamWriter sw = CreateAppendTxt(writePath);
sw.WriteLine(lines[i] + " " + dic[list_2[j++]] + "_分块2");
sw.Close();
}
//为了验证是否长度正确
Console.WriteLine(i);
int k = 0;
for (; i < (list_4.Count + list_2.Count + list_3.Count) && k < list_3.Count; i++)
{
StreamWriter sw = CreateAppendTxt(writePath);
sw.WriteLine(lines[i] + " " + dic[list_3[k++]] + "_分块3");
sw.Close();
}
//为了验证是否长度正确
Console.WriteLine(i);
int l = 0;
for (; i < (list_4.Count + list_2.Count + list_3.Count + list_1.Count) && l < list_1.Count; i++)
{
StreamWriter sw = CreateAppendTxt(writePath);
sw.WriteLine(lines[i] + " " + dic[list_1[l++]] + "_分块1");
sw.Close();
}
this.shouTxt.Text = "完成啦,赶紧查看吧!";
}
/// <summary>
/// 创建一个txt写入流
/// </summary>
/// <param name="path">写文件路径</param>
/// <returns>StreamWriter对象</returns>
private static StreamWriter CreateAppendTxt(string path)
{
FileInfo myFile = new FileInfo(path);
StreamWriter sw = myFile.AppendText();
return sw;
}
private void Form1_Load(object sender, EventArgs e)
{
this.shouTxt.Text = "数据量大,可能要一会儿!!";
//如果不手动选存储位置,则默认为D盘根目录
this.textBox1.Text = @"D:\new_" + DateTime.Now.Year+DateTime.Now.Month+DateTime.Now.Day+DateTime.Now.Hour+DateTime.Now.Minute+DateTime.Now.Second+".txt";
}
}
}
直接看代码:================================
各位可以根据代码添加窗体就行!
这是数据自己下载:
链接:https://pan.baidu.com/s/19l3mGq-QnLfn2jvK7BGv_A
提取码:k2ve
复制这段内容后打开百度网盘手机App,操作更方便哦