C#面向对象程序设计——上机实验报告(五)

实验目的

掌握使用C#创建Windows窗体应用并读写文件,学习使用FileStream、FileMode、StreamReader、StreamWriter、OpenFileDialog、SaveFileDialog。

实验内容

  • 建立一个对话框程序。
  • 制作一个文本文件,包含如下信息:
    A, 584.661, 539.482
    B, 560.872, 685.099
    C,672.094, 655.2033
  • 实现点击一次按钮,将文本读入textbox控件。
  • 实现将读入的文本数据根据分割符“,”对每行进行分割,并将数值字符转为double变量。
  • 添加一个按钮其文本为“保存”,在textbox输入文本后点击保存按钮,将文本写入到一个新文件中。

题目

切割字符串的方法根据输入的文本文件而定
尽量简单一点

代码如下:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

namespace _2016010127yanzhehua_work5
{
    public partial class Form1 : Form
    {
        public double num;
        public ArrayList n = new ArrayList();
        public Form1()
        {
            InitializeComponent();
            input_button.Click += button1_Click;
            save_button.Click += button2_Click;
        }
        void button1_Click(object sender,EventArgs e)
        {
            OpenFileDialog OpenDlg = new OpenFileDialog(); //通过openfiledialog打开数据文件
            if (OpenDlg.ShowDialog() == DialogResult.OK)
            {
                string fname;
                fname = OpenDlg.FileName;//存文件路径
                inputpath.Text += fname;//显示文件路径
                FileStream f = new FileStream(fname, FileMode.Open);
                StreamReader so = new StreamReader(f);//通过filestream对象创建streamreader
                string strAll = so.ReadToEnd();//数据读入到字符串中
                string[] strArray0 = strAll.Split(',');//切割字符串
                foreach (string i in strArray0)//在文本框中输出
                    richTextBoxInput.Text += i;
                string[] strArray = strAll.Split(new char[4] { ',', ' ','\r','\n'});//切割字符串
                foreach (string i in strArray)//将字符串转换为double
                {
                    if (Double.TryParse(i, out num))
                        n.Add(num);
                }
                f.Close();
                so.Close();
            }
        }
        void button2_Click(object sender,EventArgs e)
        {
            SaveFileDialog SaveDlg = new SaveFileDialog();//通过savefiledialog保存数据文件
            if (SaveDlg.ShowDialog() == DialogResult.OK)
            {
                string sname;
                sname = SaveDlg.FileName;//存文件路径
                outputpath.Text += sname;//显示文件路径
                FileStream s = new FileStream(sname, FileMode.Create);
                StreamWriter sw = new StreamWriter(s);//通过filestream对象创建streamwriter
                int j = 1;
                foreach(object i in n)//写入文件
                {
                    sw.Write(i.ToString());
                    sw.Write(" ");
                    if (j % 2 == 0)
                        sw.Write("\n");
                    j++;
                }
                sw.Flush();
                sw.Close();
                s.Close();
            }
        }
    }
}

输出:
在这里插入图片描述

总结

学会了使用OpenFileDialog、StreamReader打开数据文件并写入数据。以及使用SaveFileDialog、StreamWriter保存数据文件并写入文件。
同时我还在Form窗体中显示了输入文件和输出文件的路径以及读入的内容,方便检查输入和输出是否存在错误。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值