A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© slience 中级黑马   /  2012-11-3 08:05  /  1377 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

如何读取文件
try
            {
                FileStream fs = new FileStream("C:\\TZJJ.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
                StreamReader m_streamReader = new StreamReader(fs);

                //用StreamReader类来读取文件
                m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);

                //从数据流中读取每一行,只到文件的最后一行
                string strLine = m_streamReader.ReadLine();
                while (strLine != null)
                {
                    if (this.textBox1.Text.Trim() == "")
                    {
                        this.textBox1.Text = strLine;
                        strLine = m_streamReader.ReadLine();
                        continue;
                    }
                    if (this.textBox2.Text.Trim() == "")
                    {
                        this.textBox2.Text = strLine;
                        strLine = m_streamReader.ReadLine();
                        continue;
                    }
                    if (this.textBox9.Text.Trim() == "")
                    {
                        this.textBox9.Text = strLine;
                        strLine = m_streamReader.ReadLine();
                        break;
                    }
                }
                m_streamReader.Close();
            }

            catch (Exception ee)
            {
                MessageBox.Show("错误:" + ee.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
第二种方法
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
   
        private void Form1_Load(object sender, EventArgs e)
        {
           
            this.rT1.Text = "";
            FileStream fs1 = new FileStream("2.txt", FileMode.Open);
            StreamReader sr = new StreamReader(fs1);
            string str1 = sr.ReadToEnd();
            this.rT1.Text = str1;
            sr.Close();
            fs1.Close();
        }
      
    }
}

1 个回复

正序浏览
你确定是想发在Java板块?
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马