开发者

Trim a text file to desired length [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

How would I write a function that trims a text file approximately to desired size in bytes? The file is a newline-separated list and I w开发者_JAVA百科ould like to trim it between the elements, not inside them.

EDIT: Example file

element one
element two
third element
hello
final element

I'd like to to cut off the file in a way that elements are kept intact, e.g. not like this:

element one
element two
third element
he

But like this (even if it means that the size won't be exactly equal to the parameter):

element one
element two
third element


This will read the desired length and then trim it to the last newline:

 protected static string GetString(string filename, int contentLength)
    {
        string retString;
        using (FileStream fs = new FileStream(filename, FileMode.Open))
        {
            int length = (fs.Length < contentLength ? (int)fs.Length : contentLength);
            byte[] b = new byte[length];
            fs.Read(b, 0, contentLength);
            string str = System.Text.ASCIIEncoding.UTF8.GetString(b);
            if (str.LastIndexOf("\r") > 0)
                retString = str.Substring(0, str.LastIndexOf("\r"));
            else
                retString = str;
        }
        return retString;
    }

Then call it like this to write to new file:

string filename = "c:\\test.txt";
File.WriteAllText(filename,GetString(filename, 100));


    public static void TrimTextFile(string fileName, long sizeInByte)
    {
        using(FileStream fs = new FileStream(fileName, FileMode.Open))
        {
            fs.Position = sizeInByte;
            int byteRead = 0;
            while ((byteRead = fs.ReadByte()) >= 0)
            {
                if(byteRead == 13)
                {
                    fs.SetLength(fs.Position - 1);
                    break;

                }
            }
        }
    }


This sample code will continue reading lines writing them away again to another file up to a maximum file size specified. It converts each read line to your desired character encoding to determine its length in bytes.

Code is written without compile check.

var input = new FileStream(inputFilename, FileMode.Open);
var output = new FileStream(outputFilename, FileMode.Create);
var reader = new StreamReader(input);
var writer = new BinaryWriter(output);
var data = reader.ReadLine();
var maxSize = 10000;
var totalLength = 0;
while (!string.IsNullOrWhitespace(data) && totalLength < maxSize)
{
  var bytes = System.Text.UnicodeEncoding.GetBytes(data);
  totalLength += bytes.GetLength(0);
  if totalLength < maxSize) writer.Write(bytes);
  data = reader.ReadLine();
}
writer.Flush();
writer.Close();
reader.Close();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜