namespace Gaf.IO
{
using System.IO;
/// <summary>
/// Summary for the Files class
/// <source>Subsonic 2.1 final</source>
/// </summary>
public static class FileUtility
{
/// <summary>
/// Read a text file and obtain it's contents.
/// </summary>
/// <param name="absolutePath">The complete file path to write to.</param>
/// <returns>String containing the content of the file.</returns>
public static string GetFileText(string absolutePath)
{
using (StreamReader sr = new StreamReader(absolutePath))
return sr.ReadToEnd();
}
/// <summary>
/// Creates or opens a file for writing and writes text to it.
/// </summary>
/// <param name="absolutePath">The complete file path to write to.</param>
/// <param name="fileText">A String containing text to be written to the file.</param>
public static void CreateToFile(string absolutePath, string fileText)
{
using (StreamWriter sw = File.CreateText(absolutePath))
sw.Write(fileText);
}
/// <summary>
/// Update text within a file by replacing a substring within the file.
/// </summary>
/// <param name="absolutePath">The complete file path to write to.</param>
/// <param name="lookFor">A String to be replaced.</param>
/// <param name="replaceWith">A String to replace all occurrences of lookFor.</param>
public static void UpdateFileText(string absolutePath, string lookFor, string replaceWith)
{
string newText = GetFileText(absolutePath).Replace(lookFor, replaceWith);
WriteToFile(absolutePath, newText);
}
/// <summary>
/// Writes out a string to a file.
/// </summary>
/// <param name="absolutePath">The complete file path to write to.</param>
/// <para0m name="fileText">A String containing text to be written to the file.</param>
public static void WriteToFile(string absolutePath, string fileText)
{
using (StreamWriter sw = new StreamWriter(absolutePath, false))
sw.Write(fileText);
}
} //eof clas
} //eof namespace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FileSplitter
{
using System.IO;
class FileCounter
{
static string bigFile = @"D:\mylibs\dictionary_dump\dic-0294\dbo.Msg.TableInsert.sql";
static void Main()
{
Console.WriteLine(" The number of lines is " + CountLinesInFile(bigFile).ToString());
Console.Read();
}
/// <summary>
/// Count the number of lines in the file specified.
/// </summary>
/// <param name="f">The filename to count lines in.</param>
/// <returns>The number of lines in the file.</returns>
static long CountLinesInFile(string f)
{
long count = 0;
using (StreamReader r = new StreamReader(f))
{
StringBuilder objStringBuilder = new StringBuilder();
string line;
while ((line = r.ReadLine()) != null)
{
//not append !!! we need \n
objStringBuilder.AppendLine(line);
count++;
int nuberOfLinesPerSmallFile = 1000;
if (count%nuberOfLinesPerSmallFile == 0)
{
string smallFileName = (count - nuberOfLinesPerSmallFile).ToString() + "-" + count.ToString() + "_" + System.IO.Path.GetFileName(bigFile ) ;
smallFileName = System.IO.Path.GetDirectoryName(bigFile) + "\\" + smallFileName;
Console.WriteLine(" Writing to " + smallFileName);
Gaf.IO.FileUtility.WriteToFile(smallFileName, objStringBuilder.ToString());
objStringBuilder = new StringBuilder();
} //eof if
} //eof while
} //eof using
return count;
} //eof method
} //eof class
} //eof namespace
No comments:
Post a Comment
- the first minus - Comments have to be moderated because of the spammers
- the second minus - I am very lazy at moderating comments ... hardly find time ...
- the third minus - Short links are no good for security ...
- The REAL PLUS : Any critic and positive feedback is better than none, so your comments will be published sooner or later !!!!