C# 如何使用FileHelpers库按列和行映射txt
我想知道如何从txt文件中获取列项目,并在格式化后将其写入输出。 这是我的input.txtC# 如何使用FileHelpers库按列和行映射txt,c#,text,filehelpers,C#,Text,Filehelpers,我想知道如何从txt文件中获取列项目,并在格式化后将其写入输出。 这是我的input.txt DC 2INCL1 50000 20190802< DC 2INCL2 50000 20190809< DC 2INCL3 50000 20190816< DC 2INCL4 50000 20190823< DC 2INCL5 50000
DC 2INCL1 50000 20190802<
DC 2INCL2 50000 20190809<
DC 2INCL3 50000 20190816<
DC 2INCL4 50000 20190823<
DC 2INCL5 50000 20190830<
DC 2INCL6 50000 20190906<
DC 2INCL7 50000 20190913<
DC 2INCL8 50000 20190920<
DC 2INCL9 50000 20190927<
提前感谢因为值中没有空格,如果您要归档.ReadAllLines(…在中,您可以执行以下操作:
string[] lines = File.ReadAllLines("C:\\MyFile.txt");
foreach (string line in lines)
{
string[] cols = line.split(' ');
}
然后,您可以根据需要修剪和操纵cols数组中的值,因为如果要归档.ReadAllLines(…在中,您可以执行以下操作:
string[] lines = File.ReadAllLines("C:\\MyFile.txt");
foreach (string line in lines)
{
string[] cols = line.split(' ');
}
然后,您可以根据需要修剪和操纵cols数组中的值。下面给出了正确的结果:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string INPUT_FILENAME = @"c:\temp\test.txt";
const string OUTPUT_FILENAME = @"c:\temp\test1.txt";
static void Main(string[] args)
{
List<Column> columns = new List<Column>() {
new Column() { name = "Type", type = typeof(string), width = 5},
new Column() { name = "TypeWeb", type = typeof(string), width = 9},
new Column() { name = "ValueToPay", type = typeof(int), width = 18},
new Column() { name = "Date", type = typeof(DateTime), width = 8, inputFormat = "yyyyMMdd", outputFormat = "yyyy/MM/dd"}
};
List<List<object>> data = FixedWidth.ReadFile(INPUT_FILENAME, columns);
FixedWidth.WriteFile(OUTPUT_FILENAME, data, columns);
}
}
public class FixedWidth
{
public static List<List<object>> ReadFile(string filename,List<Column> columns)
{
List<List<object>> data = new List<List<object>>();
string line = "";
using (StreamReader reader = new StreamReader(filename))
{
while ((line = reader.ReadLine()) != null)
{
List<object> newLine = new List<object>();
data.Add(newLine);
int startPos = 0;
foreach (Column column in columns)
{
string col = line.Substring(startPos, column.width);
switch (column.type.ToString())
{
case "System.String" :
newLine.Add(col.Trim());
break;
case "System.Int32" :
newLine.Add(int.Parse(col));
break;
case "System.DateTime":
newLine.Add(DateTime.ParseExact(col, column.inputFormat, CultureInfo.InvariantCulture));
break;
default:
break;
}
startPos += column.width;
}
}
}
return data;
}
public static List<List<object>> WriteFile(string filename, List<List<object>> data, List<Column> columns)
{
using (StreamWriter writer = new StreamWriter(filename))
{
int rowCount = 1;
foreach(List<object> row in data)
{
string line = string.Format("{0}> ", rowCount.ToString());
for (int index = 0; index < columns.Count; index++)
{
line += string.Format("{0}:", columns[index].name);
switch (columns[index].type.ToString())
{
case "System.DateTime":
line += ((DateTime)row[index]).ToString(columns[index].outputFormat).PadRight(columns[index].width);
break;
default:
line += row[index].ToString().PadRight(columns[index].width);
break;
}
}
writer.WriteLine("{0}<",line);
rowCount++;
}
}
return data;
}
}
public class Column
{
public string name { get; set; }
public Type type { get; set; }
public int width { get; set; }
public string inputFormat { get; set; }
public string outputFormat { get; set; }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
利用制度全球化;
使用System.IO;
命名空间控制台应用程序1
{
班级计划
{
常量字符串输入\u文件名=@“c:\temp\test.txt”;
常量字符串输出\u文件名=@“c:\temp\test1.txt”;
静态void Main(字符串[]参数)
{
列表列=新列表(){
新列(){name=“Type”,Type=typeof(string),width=5},
新列(){name=“TypeWeb”,type=typeof(string),width=9},
新列(){name=“ValueToPay”,type=typeof(int),width=18},
新列(){name=“Date”,type=typeof(DateTime),width=8,inputFormat=“yyyyMMdd”,outputFormat=“yyyy/MM/dd”}
};
列表数据=FixedWidth.ReadFile(输入文件名,列);
FixedWidth.WriteFile(输出文件名、数据、列);
}
}
公共类固定宽度
{
公共静态列表ReadFile(字符串文件名,列表列)
{
列表数据=新列表();
字符串行=”;
使用(StreamReader=新StreamReader(文件名))
{
而((line=reader.ReadLine())!=null)
{
List newLine=新列表();
data.Add(换行符);
int startPos=0;
foreach(列中的列)
{
string col=line.Substring(startPos,column.width);
开关(column.type.ToString())
{
案例“System.String”:
newLine.Add(col.Trim());
打破
案例“System.Int32”:
newLine.Add(int.Parse(col));
打破
案例“System.DateTime”:
Add(DateTime.ParseExact(col,column.inputFormat,CultureInfo.InvariantCulture));
打破
违约:
打破
}
startPos+=列宽;
}
}
}
返回数据;
}
公共静态列表写文件(字符串文件名、列表数据、列表列)
{
使用(StreamWriter=newstreamwriter(文件名))
{
int rowCount=1;
foreach(数据中的列表行)
{
string line=string.Format(“{0}>”,rowCount.ToString());
for(int index=0;index writer.WriteLine(“{0}以下内容给出了正确的结果:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string INPUT_FILENAME = @"c:\temp\test.txt";
const string OUTPUT_FILENAME = @"c:\temp\test1.txt";
static void Main(string[] args)
{
List<Column> columns = new List<Column>() {
new Column() { name = "Type", type = typeof(string), width = 5},
new Column() { name = "TypeWeb", type = typeof(string), width = 9},
new Column() { name = "ValueToPay", type = typeof(int), width = 18},
new Column() { name = "Date", type = typeof(DateTime), width = 8, inputFormat = "yyyyMMdd", outputFormat = "yyyy/MM/dd"}
};
List<List<object>> data = FixedWidth.ReadFile(INPUT_FILENAME, columns);
FixedWidth.WriteFile(OUTPUT_FILENAME, data, columns);
}
}
public class FixedWidth
{
public static List<List<object>> ReadFile(string filename,List<Column> columns)
{
List<List<object>> data = new List<List<object>>();
string line = "";
using (StreamReader reader = new StreamReader(filename))
{
while ((line = reader.ReadLine()) != null)
{
List<object> newLine = new List<object>();
data.Add(newLine);
int startPos = 0;
foreach (Column column in columns)
{
string col = line.Substring(startPos, column.width);
switch (column.type.ToString())
{
case "System.String" :
newLine.Add(col.Trim());
break;
case "System.Int32" :
newLine.Add(int.Parse(col));
break;
case "System.DateTime":
newLine.Add(DateTime.ParseExact(col, column.inputFormat, CultureInfo.InvariantCulture));
break;
default:
break;
}
startPos += column.width;
}
}
}
return data;
}
public static List<List<object>> WriteFile(string filename, List<List<object>> data, List<Column> columns)
{
using (StreamWriter writer = new StreamWriter(filename))
{
int rowCount = 1;
foreach(List<object> row in data)
{
string line = string.Format("{0}> ", rowCount.ToString());
for (int index = 0; index < columns.Count; index++)
{
line += string.Format("{0}:", columns[index].name);
switch (columns[index].type.ToString())
{
case "System.DateTime":
line += ((DateTime)row[index]).ToString(columns[index].outputFormat).PadRight(columns[index].width);
break;
default:
line += row[index].ToString().PadRight(columns[index].width);
break;
}
}
writer.WriteLine("{0}<",line);
rowCount++;
}
}
return data;
}
}
public class Column
{
public string name { get; set; }
public Type type { get; set; }
public int width { get; set; }
public string inputFormat { get; set; }
public string outputFormat { get; set; }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
利用制度全球化;
使用System.IO;
命名空间控制台应用程序1
{
班级计划
{
常量字符串输入\u文件名=@“c:\temp\test.txt”;
常量字符串输出\u文件名=@“c:\temp\test1.txt”;
静态void Main(字符串[]参数)
{
列表列=新列表(){
新列(){name=“Type”,Type=typeof(string),width=5},
新列(){name=“TypeWeb”,type=typeof(string),width=9},
新列(){name=“ValueToPay”,type=typeof(int),width=18},
新列(){name=“Date”,type=typeof(DateTime),width=8,inputFormat=“yyyyMMdd”,outputFormat=“yyyy/MM/dd”}
};
列表数据=FixedWidth.ReadFile(输入文件名,列);
FixedWidth.WriteFile(输出文件名、数据、列);
}
}
公共类固定宽度
{
公共静态列表ReadFile(字符串文件名,列表列)
{
列表数据=新列表();
字符串行=”;
使用(StreamReader=新StreamReader(文件名))
{
而((line=reader.ReadLine())!=null)
{
List newLine=新列表();
data.Add(换行符);
int startPos=0;
foreach(列中的列)
{
string col=line.Substring(startPos,column.width);
开关(column.type.ToString())
{
案例“System.String”:
换行。添加(列T