请帮看看这个winform读取计算写出文件程序还可以优化吗

.Net技术 码拜 8年前 (2016-05-09) 815次浏览
本人刚开始学C#,写了一个窗口小程序,目的是读取工作中的一些数据,存到数组中,计算,再写出,请大家帮看看还有那些地方可以优化或有更好的方法,以便于提高。谢谢
界面
请帮看看这个winform读取计算写出文件程序还可以优化吗
数据格式
请帮看看这个winform读取计算写出文件程序还可以优化吗
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
/*
程序功能:
读取指定格式的数据,重新存储用于后期计算,再写出;
*/
namespace FormatConvert
{

public partial class Form1 : Form
{
public static string inputName;
public static string outputName;
public string headLine;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
//打开文件
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog file = new OpenFileDialog();
file.Filter = “文本文件|*.txt|双狐|*.dfv”;//文件格式筛选
//显示文本路径
if (file.ShowDialog() == DialogResult.OK)
{
inputName = file.FileName;
textBox1.Text = inputName;
}
}
// 点击确定按键开始数据读取和写入到文件中
private void button3_Click(object sender, EventArgs e)
{
//定义文件读取流实例
FileStream fileR = new FileStream(inputName, FileMode.Open);
StreamReader inputFile = new StreamReader(fileR);
FileStream fileW = new FileStream(outputName, FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter outputFile = new StreamWriter(fileW);
string[] headStr;//存取第一行字符数组
int hvNum;//存数据对个数
string hvLine;//存第二行字符
string[] hvStr;//

while ((headLine = inputFile.ReadLine()) != null)
{
outputFile.WriteLine(headLine,true);  // 写入第一行;
headStr = headLine.Split(” “);         //空格分离第一行成为字符串数组
//第一行最后元素为数据对的个数,转换成Int;
hvNum = Convert.ToInt16(headStr[headStr.Length – 1]);
double[] high = new double[hvNum];      // 存储数据对第一列数据
double[] velocity = new double[hvNum];   //存储数据对第二列数据
//for开始读取数据对并写入文件中;
for (int i = 0; i < hvNum;i++ )
{
hvLine = inputFile.ReadLine();
hvStr = hvLine.Split(“,”);    //逗号分离数据对;
high[i] = Convert.ToDouble(hvStr[0]);  // 转换成数值类型便后期运算
outputFile.Write(high[i]);           // 写入文件中
velocity[i] = Convert.ToDouble(hvStr[1]);
outputFile.WriteLine(“,{0}”, velocity[i]);
}
}
inputFile.Close();
outputFile.Close();
}
//保存文件路径格式
private void button2_Click(object sender, EventArgs e)
{
SaveFileDialog file2 = new SaveFileDialog();
file2.Filter = “文本文件|*.txt|dat|*.dat”;
if (file2.ShowDialog() == DialogResult.OK)
{
outputName = file2.FileName;
textBox2.Text = outputName;
}
}
// 点击取消,关闭程序;
private void button4_Click(object sender, EventArgs e)
{
this.Close();
}
}
}

解决方案

10

你想怎么优化?

30

数组和list其实都一样,数组你要提前分配空间,list可以自动增长(几何级数增长1->2->4->8)
数组相对来说会节约内存空间,list相对来说使用方便。
全部在C#中才会有两种数据类型同存的情况

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明请帮看看这个winform读取计算写出文件程序还可以优化吗
喜欢 (0)
[1034331897@qq.com]
分享 (0)