问题描述
不是关于计算机专业这一块的,纯纯纯菜鸟,老师想让我把一个txt文本读出来,把里面的数据提取一些有用信息,再整理一下,把整理好的数据生成一个新的txt文本,文本里面能像数据库里面一样,有表头和下面相应的数据。数据整理这一块老师已经把程序给我了,我想读去和生成新的TXT文本但代码完全被我整乱了,也不会改,谁能帮我一下啊,整理整理,看看哪里少了什么,哪里写错了,发一份完整的代码,万分感谢usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.IO;namespacedaoru{classNEDataParseClass{publicstaticStringAlCateNMID="000000";publicstaticStringMacrniNMID="000001";publicstaticStringNECNMID="000002";publicstaticStringECINMID="000003";publicstaticvoidMacrni_Business(stringfileName,refList<TJToplogy.Model.DataBase.BusinessTable>businessInfoList,refList<TJToplogy.Model.DataBase.BusinessPathTable>businessPathList,refstringMsgInfo,refintokCount,refinterrCount,refDictionary<String,TJToplogy.Model.DataBase.virtualPort2realPortTable>MacrnDic){FileStreamaFile=newFileStream(@"F:实验室zwz天津项目11数据整理马可尼dianluxinxi20131116.txt",FileMode.Open);StreamReaders=File.OpenText(fileName);stringread=null;intnLines=0;okCount=0;intnBegin=0;errCount=0;StringBuilderstrBuild=newStringBuilder();TJToplogy.Model.DataBase.BusinessTablebusinessInfoItem=newTJToplogy.Model.DataBase.BusinessTable();businessInfoItem.NMID=MacrniNMID;TJToplogy.Model.DataBase.BusinessPathTablebusinessPathItem=newTJToplogy.Model.DataBase.BusinessPathTable();businessPathItem.NMID=MacrniNMID;businessPathItem.PathDirection="<->";businessPathItem.TrailLabel="";intnState=0;//0:基本信息阶段1:workerRouteing2:Protectionintnpath=0;while((read=s.ReadLine())!=null){nLines++;//分析所在位置if(read.IndexOf(""")>=0){nState=0;}elseif(read.IndexOf("Worker:")>=0){nState=1;}elseif(read.IndexOf("Protection:")>=0){nState=2;}if(read.IndexOf(""")>=0){//businessInfoItem.Descript=strBuild.ToString();//TopologyLinkList.Add(topoItem);//okCount++;businessInfoItem=newTJToplogy.Model.DataBase.BusinessTable();businessInfoItem.NMID=MacrniNMID;strBuild=newStringBuilder();//Stringstr1="*;|@123***456@|;*";//Stringdelim="*;|@";//Stringstr2=str1.Trim(delim.ToCharArray());strBuild.Append(read);//stringch=@"""";read=read.Trim(null);businessInfoItem.BusinessLabel=read.Substring(1,read.Length-2).Trim(null);continue;}strBuild.Append(read);if(read.IndexOf("Type:")==0){stringtem="Type:";stringt2=read.Substring(tem.Length,read.Length-tem.Length).Trim(null);intnpt=t2.IndexOf("");businessInfoItem.PathType=t2.Substring(0,npt);businessInfoItem.ServiceRate=t2.Substring(npt,t2.IndexOf(",")-npt).Trim(null);continue;}if(read.IndexOf("ProtectionType:")>=0){stringtem="ProtectionType:";stringt2=read.Substring(tem.Length,read.Length-tem.Length).Trim(null);intnpt=t2.IndexOf("(");businessInfoItem.Protection=t2.Substring(0,npt);continue;}if(read.IndexOf("Routeing:")>=0){npath=0;businessInfoItem.BusinessMem=strBuild.ToString();businessInfoList.Add(businessInfoItem);okCount++;continue;}if(read.IndexOf("<->")>=0){businessPathItem=newTJToplogy.Model.DataBase.BusinessPathTable();businessPathItem.NMID=MacrniNMID;businessPathItem.PathDirection="<->";businessPathItem.TrailLabel="";businessPathItem.BusinessLabel=businessInfoItem.BusinessLabel;businessPathItem.IsMainRoute=((nState==1)?true:false);businessPathItem.nIndex=npath;npath++;businessPathItem.NEName=read.Substring(0,read.IndexOf(""));Stringtmps=read.Substring(businessPathItem.NEName.Length+1,read.IndexOf("<->")-businessPathItem.NEName.Length-1).Trim();intnl=tmps.LastIndexOf("");intnportindex=tmps.LastIndexOf("PORT");if(nl>nportindex+5){businessPathItem.VC=tmps.Substring(nl+1);businessPathItem.Port=tmps.Substring(0,nl);}else{businessPathItem.VC="";businessPathItem.Port=tmps;}businessPathItem.NEofNMID=MacrniNMID;if(MacrnDic.ContainsKey(businessPathItem.Port)){businessPathItem.NEName=MacrnDic[businessPathItem.Port].realNEName;businessPathItem.NEofNMID=MacrnDic[businessPathItem.Port].realNMID;businessPathItem.Port=MacrnDic[businessPathItem.Port].realPort;}intnAc=read.IndexOf("(revertive)(Active)");if(nAc<0){nAc=read.IndexOf("(Active)");}Stringxt="<->";Stringxsec=read.Substring(read.IndexOf(xt)+xt.Length,nAc-read.IndexOf(xt)-xt.Length);StringsecPort=xsec.Substring(0,xsec.LastIndexOf("")).TrimEnd();//businessPathItem.VC=xsec.Substring(xsec.LastIndexOf("")+1,xsec.Length-xsec.LastIndexOf("")-1);businessPathItem.PathMEM=read;businessPathList.Add(businessPathItem);businessPathItem=newTJToplogy.Model.DataBase.BusinessPathTable();businessPathItem.NMID=MacrniNMID;businessPathItem.PathDirection="<->";businessPathItem.TrailLabel="";businessPathItem.BusinessLabel=businessInfoItem.BusinessLabel;businessPathItem.IsMainRoute=((nState==1)?true:false);businessPathItem.nIndex=npath;npath++;businessPathItem.NEName=read.Substring(0,read.IndexOf(""));//businessPathItem.Port=secPort;nl=secPort.LastIndexOf("");nportindex=secPort.LastIndexOf("PORT");if(nl>nportindex+5){businessPathItem.VC=secPort.Substring(nl+1);businessPathItem.Port=secPort.Substring(0,nl);}else{businessPathItem.VC="";businessPathItem.Port=secPort;}businessPathItem.NEofNMID=MacrniNMID;if(MacrnDic.ContainsKey(businessPathItem.Port)){businessPathItem.NEName=MacrnDic[businessPathItem.Port].realNEName;businessPathItem.NEofNMID=MacrnDic[businessPathItem.Port].realNMID;businessPathItem.Port=MacrnDic[businessPathItem.Port].realPort;}businessPathItem.VC=xsec.Substring(xsec.LastIndexOf("")+1,xsec.Length-xsec.LastIndexOf("")-1);businessPathItem.PathMEM=xsec;businessPathList.Add(businessPathItem);}}staticvoidPrintBussinessInfo(refTJToplogy.Model.DataBase.BusinessTableitm){Console.WriteLine("businessLabel:"+itm.BusinessLabel);Console.WriteLine("ServiceRate:"+itm.ServiceRate);Console.WriteLine("Protection:"+itm.Protection);Console.WriteLine("PathWorkingState:"+itm.PathWorkingState);Console.WriteLine("PathType:"+itm.PathType);Console.WriteLine("Descript:"+itm.BusinessMem);}staticvoidPrintBusinessPath(refTJToplogy.Model.DataBase.BusinessPathTableitm){Console.WriteLine("businessLabel:"+itm.BusinessLabel);Console.WriteLine("nIndex:"+itm.nIndex.ToString());Console.WriteLine("TrailName:"+itm.TrailLabel);Console.WriteLine("IsMainRoute:"+((itm.IsMainRoute==true)?"是":"否"));Console.WriteLine("NEName:"+itm.NEName);Console.WriteLine("PortName:"+itm.Port);Console.WriteLine("VC:"+itm.VC);Console.WriteLine("PathDirection:"+itm.PathDirection);Console.WriteLine("Descript:"+itm.PathMEM);}}}staticvoidMain(string[]args){}
解决方案
解决方案二:
如何读取文本文件中的所有数据以及追加数据问题//读取文本文件数据privatevoidbutton1_Click(objectsender,EventArgse){try{openFileDialog1.Filter="文本文件(*.txt)|*.txt";openFileDialog1.ShowDialog();textBox1.Text=openFileDialog1.FileName;StreamReaderSReader=newStreamReader(textBox1.Text,Encoding.Default);textBox2.Text=SReader.ReadToEnd();}catch{MessageBox.Show("请选择文件");}}//向文本文件写入追加数据privatevoidbutton2_Click(objectsender,EventArgse){if(String.IsNullOrEmpty(textBox1.Text.Trim())){MessageBox.Show("请设置文件");return;}if(String.IsNullOrEmpty(textBox2.Text.Trim())){MessageBox.Show("请输入要写入的文件内容");return;}try{StreamWriterSWriter=newStreamWriter(textBox1.Text);SWriter.Write(textBox2.Text);SWriter.Close();MessageBox.Show("写入文件成功!");}catch(Exceptionex){MessageBox.Show(ex.Message);}}
解决方案三:
你要把前后的数据贴出来,否则别人没法帮
解决方案四:
引用2楼rtdb的回复:
你要把前后的数据贴出来,否则别人没法帮
原数据:有十几条1."!BJ1(16)-CF1(17)BJ-CFIPACS-5931ABH"Type:BidirectionalVC-12,Notpre-emptiveState:ActiveWorkerColours:NoColoursProtectionColours:NoColoursOperationalState:EnabledInService:YesTrailTraceState:UndefinedProtectionType:SNCP(Revertive)ProtectionState:ActiveASTNOperationalState:DisabledCreatedon:Tuesday,August23,201110:00:57AMCreatedby:nmcmanRouteingProperties:PhysicalNodeDiversity:SatisfiedSRLGDiversity:SatisfiedPhysicalLinkDiversity:SatisfiedRouteing:Worker:BeiJiaoBian.BeiJB.1LTU1-132Mb/sPORT16<->Card1-33STM-16PORT11-1-2-3(Active)BeiJB/1-ChenF/11S11-1-2-3ChenFu.ChenF.1LTU1-132Mb/sPORT17<->Card1-21STM-16PORT11-1-2-3(Active)Protection:BeiJiaoBian.BeiJB.1LTU1-132Mb/sPORT16<->Card1-21STM-16PORT11-1-5-1(Active)BeiJB/1-JiXianB/11S11-1-5-1JiXB.JiXianB.1Card1-22STM-16PORT11-1-5-1<->Card1-21STM-16PORT11-1-5-1(Active)BaoDB/1-JiXianB/11S11-1-5-1BaoDB.BaoDB.1Card1-31STM-16PORT11-1-5-1<->Card1-22STM-16PORT11-1-2-3(Active)BaoDB/1-LuTB/11S11-1-2-3LuTB.LuTB.1Card1-22STM-16PORT11-1-2-3<->Card1-21STM-16PORT11-1-2-3(Active)ChenF/1-LuTB/11S11-1-2-3ChenFu.ChenF.1LTU1-132Mb/sPORT17<->Card1-22STM-16PORT11-1-2-3(Active)要改成这个样子的txt格式