我需要帮助创建一些内容以处理数百个分隔文本文件(001,002,003),这些文本文件都是相同的固定宽度格式,并指定每个字段由管道分隔的特定列|。例如,原始数据可能看起来像:组合多个固定宽度的文本文件并将数据分隔到管道分隔的列中
123456789HA02HANKS PAUL 123 3rd Ave #2 NEW YORK NY10023198601042012235245
,并在数据字典中的定义是:
Field 1: SSN, start 1, end 9, length 9
Field 2: Name ID, start 10, end 11, length 2
Field 3: Transaction Number, start 12, end 13, length 2
Field 4: Last Name, start 14, end 29, length 16
Field 5: First Name, start 30, end 41, length 12
Field 6: Mailing Address, start 42, end 76, length 35
Field 7: City, start 77, end 92, length 16
Field 8: State, start 93, end 94, length 2
Field 9: Zip, start 95, end 99, length 5
Field 10: DOB, start 100, end 107, length 8
Field 11: Phone Number, start 108, end 117, length 10
我需要它看起来像:
123456789|HA|02|HANKS|PAUL|123 3rd Ave #2|NEW YORK|NY|10023|19860104|2012235245
我有一个C#控制台文件阅读器结合了多个文件,但我不知道如何将它们分隔成多列。这里是我的代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EmilysFileReader
{
public class Program
{
static void Main(string[] args)
{
Program prog = new Program();
Console.WriteLine("This program will attempt to combine all the files of a given directory.");
Console.WriteLine("Enter path to the directory:");
var path = Console.ReadLine();
string[] files = prog.CollectFiles(path);
Console.WriteLine("Name for the new file?");
string filename = Console.ReadLine();
prog.DoWork(Path.Combine(path, filename), files);
Console.WriteLine("Finished new file is " + Path.Combine(path, filename));
Console.WriteLine("Press enter to close.");
Console.ReadLine();
}
private void DoWork(string path, string[] files)
{
string filename = path + ".txt";
foreach (string file in files)
{
File.AppendAllText(filename, GetFileContent(file));
}
}
public string[] CollectFiles(string path)
{
string[] files = Directory.GetFiles(path);
Console.WriteLine("Found Files:");
foreach (string file in files)
{
Console.WriteLine(file);
}
return files;
}
public string GetFileContent(string file)
{
return File.ReadAllText(file);
}
}
}
我需要在C#,Java,SAS或SSMS中执行此操作的方法。任何人都可以将我指向正确的方向吗?