去注释去空格行
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace 去处注释行
{
class Program
{
static void ReadFileInfo(string path,List<string> info)
{
string str = string.Empty;
using(StreamReader reader = new StreamReader(path,Encoding.UTF8))
{
while((str = reader.ReadLine()) != null)
{
if(!string.IsNullOrEmpty(str))
{
if(str.Trim().Length != 0)
{
if(str.IndexOf("//") != 0)
{
if(str.IndexOf("//") != -1 && !str.Contains('"'))
{
str = str.Substring(0, str.IndexOf("//"));
if(str.Trim().Length != 0)
{
info.Add(str);
}
}else
{
info.Add(str);
}
}
}
}
}
}
}
static void WriteFileInfo(string path,List<string> info)
{
using(StreamWriter writer = new StreamWriter(path,false,Encoding.UTF8))
{
for(int i = 0;i<info.Count;i++)
{
writer.WriteLine(info[i]);
}
writer.WriteLine("//by 自动去除注释工具");
}
}
static void Main(string[] args)
{
Console.WriteLine("操作有风险,取消注释前请先保留一份...");
List<string> list = new List<string>();
string path = Console.ReadLine();
string rePath = path.Insert(path.Length, ".bak");
if(File.Exists(rePath))
{
File.Delete(rePath);
}
File.Copy(path, rePath);
}
}
}
将1.txt文本内容计算结果写入2.txt文本
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace 读文件
{
class Program
{
static bool CheckNumber(string str)
{
bool returnValue = true;
for (int i = 0; i < str.Length; i++)
{
if (!char.IsDigit(str[i]))
{
returnValue = false;
break;
}
}
return returnValue;
}
static bool IsNumber(string number01, string number02)
{
bool returnValue = true;
if (!(CheckNumber(number01) && CheckNumber(number02)))
{
returnValue = false;
}
return returnValue;
}
static void CalcNumberByOper(string[] infos, out string finalStr)
{
finalStr = string.Empty;
long Number01 = Convert.ToInt64(infos[0]);
long Number02 = Convert.ToInt64(infos[2]);
switch (infos[1])
{
case "+":
finalStr = string.Format("{0} {1} {2} {3} {4}", infos[0], infos[1], infos[2], infos[3], Number01 + Number02);
break;
case "-":
finalStr = string.Format("{0} {1} {2} {3} {4}", infos[0], infos[1], infos[2], infos[3], Number01 - Number02);
break;
case "*":
finalStr = string.Format("{0} {1} {2} {3} {4}", infos[0], infos[1], infos[2], infos[3], Number01 * Number02);
break;
case "/":
if (Number02 == 0)
{
finalStr = string.Format("{0} {1} {2} {3} {4}", infos[0], infos[1], infos[2], infos[3], "除数不能为0");
}
else
{
finalStr = string.Format("{0} {1} {2} {3} {4}", infos[0], infos[1], infos[2], infos[3], Number01 / Number02);
}
break;
case "%":
if (Number02 == 0)
{
finalStr = string.Format("{0} {1} {2} {3} {4}", infos[0], infos[1], infos[2], infos[3], "除数不能为0");
}
else
{
finalStr = string.Format("{0} {1} {2} {3} {4}", infos[0], infos[1], infos[2], infos[3], Number01 % Number02);
}
break;
}
}
static string CalcStringArray(string[] infos)
{
string returnValue = string.Empty;
if (!IsNumber(infos[0], infos[2]))
{
returnValue = string.Format("{0} {1} {2} {3} {4}", infos[0], infos[1], infos[2], infos[3], "源文件不是数字");
goto retTip;
}
CalcNumberByOper(infos, out returnValue);
retTip:
return returnValue;
}
static void ReadFileToList(string filePath, List<string> fileInfo)
{
string tmpStr = string.Empty;
using (StreamReader reader = new StreamReader(filePath, Encoding.UTF8))
{
while ((tmpStr = reader.ReadLine()) != null)
{
if (!string.IsNullOrEmpty(tmpStr))
fileInfo.Add(tmpStr);
}
}
Console.WriteLine("测试 读取数据{0}行", fileInfo.Count);
}
static void CalcListInfo(List<string> fileInfo)
{
for (int i = 0; i < fileInfo.Count; i++)
fileInfo[i] = CalcStringArray(fileInfo[i].Split(' '));
}
static void WriteListInfoToFile(string oldPath, List<string> fileInfo)
{
string tmpPath = oldPath.Insert(oldPath.IndexOf('.'), "_copy");
using (StreamWriter writer = new StreamWriter(tmpPath, false, Encoding.UTF8))
{
for (int i = 0; i < fileInfo.Count; i++)
{
writer.WriteLine(fileInfo[i]);
}
}
}
static void Main(string[] args)
{
List<string> infoList = new List<string>();
string path = Console.ReadLine();
ReadFileToList(path, infoList);
CalcListInfo(infoList);
WriteListInfoToFile(path, infoList);
}
}
}