내가해야 할 일은 각각 약 2GB의 파일 (txt)이 거의 없다는 것입니다. '%% XGF NEW_SET' 표시가 나타날 때마다 파일을 잘라 내야합니다. 새 파일을 만들어 저장해야합니다. 나는이 표식이 약 40 ~ 50 줄마다 등장한다고 생각한다. 각 라인은 4 ~ 20 개의 문자를가집니다. 큰 파일을 수천 개의 작은 파일로 잘라낸 다음 나중에 처리해야합니다. 나는 그런 샘플 코드를 생각했다.효율적인 파일 읽기 및 잘라 내기
DirectoryInfo di = new DirectoryInfo(ConfigurationManager.AppSettings["BilixFilesDir"]);
var files = di.GetFiles();
int count = 0;
bool hasObject = false;
StringBuilder sb = new StringBuilder();
string line = "";
foreach (var file in files)
{
using (StreamReader sr = new StreamReader(file.FullName,Encoding.GetEncoding(1250)))
{
while ((line = sr.ReadLine()) != null)
{
//when new file starts
if (line.Contains("%%XGF NEW_SET"))
{
//when new file existed I need to store old one
if (hasObject)
{
File.WriteAllText(string.Format("{0}/{1}-{2}", ConfigurationManager.AppSettings["OutputFilesDir"], count++, file.Name), sb.ToString());
sb.Length = 0;
sb.Capacity = 0;
}
//setting exist flag
hasObject = true;
}
//when there is no new object
else
//when object exists adding new lines
if (hasObject)
sb.AppendLine(line);
}
//when all work done saving last object
if (hasObject)
{
File.WriteAllText(string.Format("{0}/{1}-{2}", ConfigurationManager.AppSettings["OutputFilesDir"], count++, file.Name), sb.ToString());
sb.Length = 0;
sb.Capacity = 0;
}
}
}
}
그래서 내 표본은 높은 효율을 필요로합니다. 솔루션을 개선 할 수있는 아이디어가 있습니까? 감사합니다
는'%% XGF NEW_SET' 분할 선에 유일하게인가? 그렇지 않은 경우이 줄을 버리므로 다른 정보가 손실됩니다. –
당신의 방법은 : 원본 파일에서 스트림을 열고, 모든 섹션 브레이크마다 새로운 파일에 기록하십시오. 코드 가독성을 최적화하고 문자열 작성자를 필요로하지 않는다는 점을 제외하면이 방법을 훨씬 빨리 구현할 수있는 방법을 알지 못합니다. 쓰기 작업을 쓰레드에 스레딩하는 것은 당신이 다른 드라이브에서 읽고 재 작성하는 경우에 도움이 될 것입니다. – deepee1