fileRowDeduplicator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.IO; | |
using System.Text; | |
namespace samples | |
{ | |
public static class FileUtility | |
{ | |
/// <summary> | |
/// ファイル内の重複行を削除する。 | |
/// </summary> | |
/// <param name="filePath">対象のファイルパス</param> | |
public static void rowDeduplicator(string filePath) { | |
List<string> rows = new List<string>(); | |
using (FileStream fs = new FileStream(filePath, FileMode.Open)) | |
{ | |
using (StreamReader reader = new StreamReader(fs, Encoding.Default)) | |
{ | |
while (!reader.EndOfStream) { | |
rows.Add(reader.ReadLine()); | |
} | |
} | |
} | |
using (FileStream fs = new FileStream(filePath, FileMode.Create)) | |
{ | |
using (StreamWriter writer = new StreamWriter(fs, Encoding.Default)) | |
{ | |
foreach (var uniqueRow in rows.Distinct()) | |
{ | |
writer.WriteLine(uniqueRow); | |
} | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.IO; | |
using System.Text; | |
namespace samples | |
{ | |
public static class FileUtility | |
{ | |
/// <summary> | |
/// ファイル内の重複行を削除する。 | |
/// </summary> | |
/// <param name="filePath">対象のファイルパス</param> | |
public static void rowDeduplicator(string filePath) { | |
var rows = File.ReadAllText(filePath).Split(new string[] {Environment.NewLine}, StringSplitOptions.None); | |
using (StreamWriter writer = new StreamWriter(filePath, False, Encoding.Default)) | |
{ | |
writer.WriteLine(string.Join(Environment.NewLine, rows.Distinct().ToArray())); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment