Demonstrates seeking to a position in a file from the end : File Read Write « File Stream « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » File Stream » File Read WriteScreenshots 
Demonstrates seeking to a position in a file from the end
Demonstrates seeking to a position in a file from the end
 
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/

// Seek.cs -- Demonstrates seeking to a position in a file from the end,
//            middle and beginning of a file
//
//            Compile this program with the following command line:
//                C:>csc Seek.cs
using System;
using System.IO;
using System.Text;

namespace nsStreams
{
    public class Seek
    {
        const string str1 = "Now is the time for all good men to " +
                            "come to the aid of their Teletype.\r\n";
        const string str2 = "The quick red fox jumps over the " +
                           "lazy brown dog.\r\n";
        static public void Main ()
        {
            FileStream strm;
            try
            {
                strm = new FileStream ("./StrmSeek.txt",
                                       FileMode.Create,
                                       FileAccess.ReadWrite);
            }
            catch (Exception e)
            {
                Console.WriteLine (e);
                Console.WriteLine ("Cannot open StrmSeek.txt " +
                                   "for reading and writing");
                return;
            }
// Clear out any remnants in the file
//            strm.SetLength (0);
            foreach (char ch in str1)
            {
                strm.WriteByte ((bytech);
            }
            foreach (char ch in str2)
            {
                strm.WriteByte ((bytech);
            }
// Seek from the beginning of the file
            strm.Seek (str1.Length, SeekOrigin.Begin);
// Read 17 bytes and write to the console.
            byte [] text = new byte [17];
            strm.Read (text, 0, text.Length);
            ShowText (text);
// Seek back 17 bytes and reread.
            strm.Seek (-17, SeekOrigin.Current);
            strm.Read (text, 0, text.Length);
            ShowText (text);
// Seek from the end of the file to the beginning of the second line.
            strm.Seek (-str2.Length, SeekOrigin.End);
            strm.Read (text, 0, text.Length);
            ShowText (text);
        }
        static void ShowText (byte [] text)
        {
            StringBuilder str = new StringBuilder (text.Length);
            foreach (byte b in text)
            {
    
                str.Append ((charb);
            }
            Console.WriteLine (str);
        }
    }
}
//File: StrmSeek.txt

/*
Now is the time for all good men to come to the aid of their Teletype.
The quick red fox jumps over the lazy brown dog.

*/

           
         
  
Related examples in the same category
1.Demonstrates opening/creating a file for writing and truncating its length to 0 bytes.Demonstrates opening/creating a file for writing and truncating its length to 0 bytes.
2.Writes the same string to a file and to the screen using a common methodWrites the same string to a file and to the screen using a common method
3.Display a text file
4.Write to a file
5.Copy a file
6.Demonstrate random accessDemonstrate random access
7.Hex value Dump
8.Read all the content from a file as byte array
9.Read all the content from a file as string in default encoding
10.Gets a files' contents
11.Gets a files' contents with MemoryStream
12.Gets a files' contents from a Url
13.Gets a files' contents from a Url and save to an OutputStream
14.Gets a file's contents (Used primarily for text documents on an FTP)
15.Gets a files' contents from an Url with NetworkCredential
16.Read/Write File Transacted
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.