C#では、文字エンコーディングを維持しながら、テキストファイルを複数のテキストファイルに分割する最も効率的な方法(分割デリミタは空白行です)は何ですか?テキストファイルを複数のファイルに分割する方法は?
答えて
私はStreamReaderをとのStreamWriterクラスを使用します。
public void Split(string inputfile, string outputfilesformat) {
int i = 0;
System.IO.StreamWriter outfile = null;
string line;
try {
using(var infile = new System.IO.StreamReader(inputfile)) {
while(!infile.EndOfStream){
line = infile.ReadLine();
if(string.IsNullOrEmpty(line)) {
if(outfile != null) {
outfile.Dispose();
outfile = null;
}
continue;
}
if(outfile == null) {
outfile = new System.IO.StreamWriter(
string.Format(outputfilesformat, i++),
false,
infile.CurrentEncoding);
}
outfile.WriteLine(line);
}
}
} finally {
if(outfile != null)
outfile.Dispose();
}
}
あなたは、このように、このメソッドを呼び出します。
Split("C:\\somefile.txt", "C:\\output-files-{0}.txt");
+1ですが、空白行にnullまたは空の文字列ではなく、 'System.Environment.NewLine'という値があるかどうか疑問です。 –
@adrift: 'System.Environment.NewLine'はすべての行の末尾(または先頭)に追加されませんか? – GPX
テキストファイルの "空白行"は、常に\ r \ n(またはOSに基づいた亜種)ですが、それ以外はどのように検出しますか?テキストファイルは文字列です。 –
純粋な思考を避けたい人のために:
場合CSV(カンマ区切りの値)ファイルがあり、フィールドが変更されたときにファイルを分割し、変更によってファイルを識別/名前付けします(不要な引用符は不要です)。 (ここでは#」で始まるによって識別される)ertainライン
修正方法:
public void Split(string inputfile, string outputfilesformat)
{
System.IO.StreamWriter outfile = null;
string line;
string[] splitArray;
string nameFromFile = "";
try
{
using (var infile = new System.IO.StreamReader(inputfile))
{
while (!infile.EndOfStream)
{
line = infile.ReadLine();
splitArray = line.Split(new char[] { ',' });
if (!splitArray[0].StartsWith("\"#"))
{
if (splitArray[4].Replace("\"", "") != nameFromFile.Replace("\"", ""))
{
if (outfile != null)
{
outfile.Dispose();
outfile = null;
}
nameFromFile = splitArray[4].Replace("\"", "");
continue;
}
if (outfile == null)
{
outfile = new System.IO.StreamWriter(
string.Format(outputfilesformat, nameFromFile),
false,
infile.CurrentEncoding);
}
outfile.WriteLine(line);
}
}
}
}
finally
{
if (outfile != null)
outfile.Dispose();
}
}
ローカルパスコール:
string strpath = Server.MapPath("~/Data/SPLIT/DATA.TXT");
string newFile = Server.MapPath("~/Data/SPLIT");
if (System.IO.File.Exists(@strpath))
{
Split(strpath, newFile+"\\{0}.CSV");
}
- 1. テキストファイルを複数のフラグメントに分割する方法は?
- 2. Laravel:ファイルを複数のファイルに分割する方法は?
- 3. yamlファイルを複数のファイルに分割する方法は?
- 4. Cプログラムを複数のファイルに分割する方法は?
- 5. epub - チャプターを複数のxhtmlファイルに分割する方法は?
- 6. モジュールを複数のファイルに分割する方法は?
- 7. React Native:ファイルを複数のファイルに分割する方法
- 8. テキストファイルに複数回線を分割する方法はありますか?
- 9. 分割dmpファイルを複数のdmpファイルに分割する
- 10. Scalaスクリプトを複数のファイルに分割する方法
- 11. 複数のファイルにjQueryプラグインを分割する方法
- 12. 複数のファイルにコードを分割する方法
- 13. javascriptとhtmlを複数のファイルに分割する方法
- 14. JavaScriptオブジェクトを複数のファイルに分割する方法
- 15. Python Tkinterコードを複数のファイルに分割する方法
- 16. ダウンロードマネージャがファイルを複数の部分に分割する方法は?
- 17. テキストファイルを複数のテキストファイルに分割したい
- 18. linuxを使ってファイルを複数のファイルに分割する方法は?
- 19. vueのdev-server.jsファイルを複数のファイルに分割する方法は?
- 20. 複数のExcelファイルでパンダのデータフレームを分割する方法
- 21. ファイルを複数のファイルに分割
- 22. 長い入力を複数のテキストファイルに分割する
- 23. marklogic mlcpカスタム変換分割ファイルを複数のファイルに分割
- 24. Rを複数のサブ部分に分割する方法
- 25. テキストファイルを2つの配列に分割する方法は?
- 26. Mavenマルチモジュールビルドを複数のJenkinsビルドジョブに分割する方法は?
- 27. この行を複数行に分割する方法は?
- 28. デリミタのn番目のインスタンスでテキストファイルを複数のファイルに分割
- 29. テキストファイルを単語に分割する方法は?
- 30. サーバからテキストファイルを読み込み、asp.netの複数のファイルに分割
あなたのタイトルとあなたの実際の質問が異なっています。テキストファイル(タイトル)を分割する方法や、それをより効率的に行う方法(質問)を知りたいですか? –
私は両方を探しています。最も効率的な方法でテキストファイルを分割する! – GPX