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

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. XML
25. XML LINQ
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
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
w__w___w.j_av_a2_s__._c___o___m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.