我该如何编辑文本文件? [英] How Can I Edit Text File?

查看:107
本文介绍了我该如何编辑文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有程序从用户读取信息并将其保存在带有二进制格式化程序的文本文件中如何才能接收某些记录或只是文件的某些部分?





使用System; 
使用System.Collections.Generic;使用System.ComponentModel
;
使用System.Data;使用System.Drawing
;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.Runtime.Serialization.Formatters.Binary;
使用System.Runtime.Serialization;
使用System.IO;

命名空间修复
{



公共部分类Form1:表格
{
私有FileStream输出;
私有FileStream输入;
private BinaryFormatter formtter = new BinaryFormatter();
BinaryFormatter reader = new BinaryFormatter();
public Form1()
{
InitializeComponent();
}




private void openfile()
{
output = new FileStream(C:\\\ \\Users\\lenovo\\Documents\\kar.txt,FileMode.Append,FileAccess.Write);
}
public void cleartextbox()
{
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
}

private void Form1_Load(object sender,EventArgs e)
{

}

private void button1_Click(object sender,EventArgs e)
{
openfile();
user User = new user();
try
{
User.Name = textBox1.Text;
User.Family = textBox2.Text;
User.Job = textBox3.Text;
}
catch(FormatException)
{
MessageBox.Show(invaid format);
output.Close();
返回;
}
formtter.Serialize(输出,用户);
output.Close();
cleartextbox();
}

private void button2_Click(object sender,EventArgs e)
{
try
{
input = new FileStream(C: \\Users\\lenovo\\Documents\\kar.txt,FileMode.Open,FileAccess.Read);
textBox4.Text =用户是:;
while(true)
{
user User =(user)reader.Deserialize(input);
string result = User.Name +\t+ User.Family +\t+ User.Job +\t;
textBox4.Text + =\\\\ n+结果;
}
}
catch(FileNotFoundException)
{
MessageBox.Show(file not exists);
返回;
}
catch(SerializationException)
{
input.Close();
}
}

private void button3_Click(object sender,EventArgs e)
{
ReadRepFile();
}

}
}







和用户类







 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;

命名空间修复
{
[可序列化]
class user
{
private string name;
private string family;
private string job;
public user()
{
name = ;
family = ;
job = ;


}
public user( string namevalue , string familyvalue, string jobvalue)
{
name = namevalue;
family = familyvalue;
job = jobvalue;
}
public string 名称
{
< span class =code-keyword> get
{
return name;
}
set
{
name = value ;
}
}
public string Family
{
获取
{
返回系列;
}
set
{
family = value ;
}

}
public string Job
{
get
{
return job;
}
set
{
job = value ;
}
}
}
}

解决方案

这里的主要问题是(看来)您正在一次保存和恢复i>一个实例。但是,你想要的是能够以结构化的方式访问'用户的集合:



1.创建一个类来保持'User:

公共类用户:List< user>的集合
{
//序列化并反序列化此
}

2。一旦你有这个集合,你可以使用Linq执行如下搜索:

 var SameJob = theUsers.GroupBy(user => user.Job); 

foreach(SameJob中的var grp)
{
Console.WriteLine(Job:{0},grp.Key.ToString());

foreach(var usr in grp)
{
Console.WriteLine(usr.Name);
}
}

我还建议您切换到使用DataContract(从.NET 3.0开始提供)进行序列化和反序列化:这是对以前设施的改进:[< a href =https://msdn.microsoft.com/en-us/library/ms733811(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ]。


i have program that read information from user and save it in a text file with binary formatter how can i accese to some records or just some parts of file?


using System;
using System.Collections.Generic;
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.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.IO;

namespace repair
{

   

    public partial class Form1 : Form
    {
        private FileStream output;
        private FileStream input;
        private BinaryFormatter formtter = new BinaryFormatter();
        BinaryFormatter reader = new BinaryFormatter();
        public Form1()
        {
            InitializeComponent();
        }
    

          

        private void openfile()
        {
            output = new FileStream("C:\\Users\\lenovo\\Documents\\kar.txt",FileMode.Append,FileAccess.Write);
        }
        public void cleartextbox()
        {
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            openfile();
            user User=new user();
            try
            {
                User.Name = textBox1.Text;
                User.Family = textBox2.Text;
                User.Job = textBox3.Text;
            }
            catch(FormatException)
            {
                MessageBox.Show("invaid format");
                output.Close();
                return;
            }
            formtter.Serialize(output, User);
            output.Close();
            cleartextbox();
        }

        private void button2_Click(object sender, EventArgs e)
        {
try
{
    input = new FileStream("C:\\Users\\lenovo\\Documents\\kar.txt", FileMode.Open, FileAccess.Read);
    textBox4.Text = "users are:";
    while (true)
    {
        user User = (user)reader.Deserialize(input);
        string result = User.Name+"\t" + User.Family+"\t" + User.Job+"\t";
        textBox4.Text +="\r\n" +result;
    }
}
            catch(FileNotFoundException)
{
    MessageBox.Show("file not exists");
    return;
}
            catch(SerializationException)
{
    input.Close();
}
        }

        private void button3_Click(object sender, EventArgs e)
        {
            ReadRepFile();
        }
        
    }
}




and the user class



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace repair
{
    [Serializable]
    class user
    {
        private string name;
        private string family;
        private string job;
        public user()
        {
        name=" ";
        family = " ";
        job = " ";


    }
        public user( string namevalue,string familyvalue,string jobvalue)
        {
            name = namevalue;
            family = familyvalue;
            job = jobvalue;
        }
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
            public string Family
            {
                get
                {
                    return family;
                }
                set
                {
                    family=value;
                }

            }
        public string Job
        {
            get
            {
                return job;
            }
            set
            {
                job=value;
            }
        }
        }
    }

解决方案

Your main problem here is that (it appears) you are saving and restoring one instance of 'User at a time. But, what you want is to be able to access a collection of 'User in structured ways:

1. Create a class to hold a collection of 'User:

public class Users : List<user>
{
   // serialize and de-serialize this
}

2. once you have this collection you can use Linq to perform searches like:

var SameJob = theUsers.GroupBy(user => user.Job);

foreach (var grp in SameJob)
{
    Console.WriteLine("Job: {0}", grp.Key.ToString());

    foreach (var usr in grp)
    {
        Console.WriteLine(usr.Name);
    }
}

I also suggest you switch to using DataContract (available beginning with .NET 3.0) for serialization and de-serialization: it is an improvement over previous facilities: [^].


这篇关于我该如何编辑文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆