Archive for February, 2008

Php web hosting - CHAPTER 16 THE SYSTEM.IO NAMESPACE 539 //

Friday, February 29th, 2008

CHAPTER 16 THE SYSTEM.IO NAMESPACE 539 // Write the data bw.Write(aDouble); bw.Write(anInt); bw.Write(aCharArray); bw.Close(); } Notice how the FileStream object returned from FileInfo.OpenWrite() is passed to the constructor of the BinaryWriter type. Using this technique, it is very simple to layer in a stream before writing out the data. Do understand that the constructor of BinaryWriter takes any Stream-derived type (e.g., FileStream, MemoryStream, or BufferedStream). Thus, if you would rather write binary data to memory, simply supply a valid MemoryStream object. To read the data out of the BinFile.dat file, the BinaryReader type provides a number of options. Here, you will make use of PeekChar() to determine if the stream still has data to provide and, if so, use ReadByte() to obtain the value. Note that you are formatting the bytes in hexadecimal and inserting seven spaces between each: static void Main(string[] args) { // Open a binary writer for a file. FileInfo f = new FileInfo(”BinFile.dat”); … // Read the data as raw bytes BinaryReader br = new BinaryReader(f.OpenRead()); int temp = 0; while (br.PeekChar() != -1) { Console.Write(”{0,7:x} “, br.ReadByte()); if (++temp == 4) { // Write a new line every 4 bytes Console.WriteLine(); temp = 0; } } Console.WriteLine(); } The output of this program appears in Figure 16-9. Figure 16-9. Reading bytes from a binary file
We recommend cheap and reliable webhost to host and run your web applications: Coldfusion Web Hosting services.

CHAPTER 16 538 THE SYSTEM.IO NAMESPACE Working (Web design course)

Friday, February 29th, 2008

CHAPTER 16 538 THE SYSTEM.IO NAMESPACE Working with BinaryWriters and BinaryReaders The final writer/reader sets you will examine here are BinaryReader and BinaryWriter, both of which derive directly from System.Object. These types allow you to read and write discrete data types to an underlying stream in a compact binary format. The BinaryWriter class defines a highly overloaded Write() method to place a data type in the underlying stream. In addition to Write(), BinaryWriter provides additional members that allow you to get or set the Stream-derived type and offers support for random access to the data (see Table 16-9). Table 16-9. BinaryWriter Core Members Member Meaning in Life BaseStream This read-only property provides access to the underlying stream used with the BinaryWriter object. Close() This method closes the binary stream. Flush() This method flushes the binary stream. Seek() This method sets the position in the current stream. Write() This method writes a value to the current stream. The BinaryReader class complements the functionality offered by BinaryWriter with the members described in Table 16-10. Table 16-10. BinaryReader Core Members Member Meaning in Life BaseStream This read-only property provides access to the underlying stream used with the BinaryReader object. Close() This method closes the binary reader. PeekChar() This method returns the next available character without actually advancing the position in the stream. Read() This method reads a given set of bytes or characters and stores them in the incoming array. ReadXXXX() The BinaryReader class defines numerous ReadXXXX() methods that grab the next type from the stream (ReadBoolean(), ReadByte(), ReadInt32(), and so forth). The following example writes a number of data types to a new *.dat file: static void Main(string[] args) { // Open a binary writer for a file. FileInfo f = new FileInfo(”BinFile.dat”); BinaryWriter bw = new BinaryWriter(f.OpenWrite()); // Print out the type of BaseStream. // (System.IO.FileStream in this case). Console.WriteLine(”Base stream is: {0}”, bw.BaseStream); // Create some data to save in the file double aDouble = 1234.67; int anInt = 34567; char[] aCharArray = { ‘A’, ‘B’, ‘C’ };
We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.

CHAPTER 16 THE SYSTEM.IO NAMESPACE 537 static (Hosting web)

Thursday, February 28th, 2008

CHAPTER 16 THE SYSTEM.IO NAMESPACE 537 static void Main(string[] args) { Console.WriteLine(”***** Fun with StringWriter / StringReader *****n”); // Create a StringWriter and emit character data to memory. StringWriter strWriter = new StringWriter(); strWriter.WriteLine(”Don’t forget Mother’s Day this year…”); strWriter.Close(); // Get a copy of the contents (stored in a string) and pump // to console. Console.WriteLine(”Contents of StringWriter:n{0}”, strWriter); } Because StringWriter and StreamWriter both derive from the same base class (TextWriter), the writing logic is more or less identical. However, given that nature of StringWriter, be aware that this class allows you to extract a System.Text.StringBuilder object via the GetStringBuilder() method: static void Main(string[] args) { Console.WriteLine(”***** Fun with StringWriter / StringReader *****n”); // Create a StringWriter and emit character data to memory. StringWriter strWriter = new StringWriter(); … // Get the internal StringBuilder. StringBuilder sb = strWriter.GetStringBuilder(); sb.Insert(0, “Hey!! “); Console.WriteLine(”-> {0}”, sb.ToString()); sb.Remove(0, “Hey!! “.Length); Console.WriteLine(”-> {0}”, sb.ToString()); } When you wish to read from a stream of character data, make use of the corresponding StringReader type, which (as you would expect) functions identically to the related StreamReader class. In fact, the StringReader class does nothing more than override the inherited members to read from a block of character data, rather than a file, as shown here: static void Main(string[] args) { Console.WriteLine(”***** Fun with StringWriter / StringReader *****n”); // Create a StringWriter and emit character data to memory. StringWriter strWriter = new StringWriter(); … // Read data from the StringWriter. StringReader strReader = new StringReader(writer.ToString()); string input = null; while ((input = strReader.ReadLine()) != null) { Console.WriteLine (input); } strReader.Close(); } Source Code The StringReaderWriterApp is included under the Chapter 16 subdirectory.
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.

CHAPTER 16 536 THE SYSTEM.IO NAMESPACE static (Web hosting domains)

Wednesday, February 27th, 2008

CHAPTER 16 536 THE SYSTEM.IO NAMESPACE static void Main(string[] args) { Console.WriteLine(”***** Fun with StreamWriter / StreamReader *****n”); … // Now read data from file. Console.WriteLine(”Here are your thoughts:n”); StreamReader sr = File.OpenText(”reminders.txt”); string input = null; while ((input = sr.ReadLine()) != null) { Console.WriteLine (input); } } Once you run the program, you will see the character data within Thoughts.txt displayed to the console. Directly Creating StreamWriter/StreamReader Types One of the slightly confusing aspects of working with the types within System.IO is that you can often achieve an identical result using numerous approaches. For example, you have already seen that you can obtain a StreamWriter via the File or FileInfo type using the CreateText() method. In reality, there is yet another way in which you can work with StreamWriters and StreamReaders: create them directly. For example, the current application could be retrofitted as so: static void Main(string[] args) { Console.WriteLine(”***** Fun with StreamWriter / StreamReader *****n”); // Get a StreamWriter and write string data. StreamWriter writer = new StreamWriter(”reminders.txt”); … // Now read data from file. StreamReader sr = new StreamReader(”reminders.txt”); … } Although it can be a bit confusing to see so many seemingly identical approaches to file I/O, keep in mind that the end result is greater flexibility. In any case, now that you have seen how to move character data to and from a given file using the StreamWriter and StreamReader types, you will next examine the role of the StringWriter and StringReader classes. Source Code The StreamWriterReaderApp project is included under the Chapter 16 subdirectory. Working with StringWriters and StringReaders Using the StringWriter and StringReader types, you can treat textual information as a stream of in-memory characters. This can prove helpful when you wish to append character-based information to an underlying buffer. To illustrate, the following example writes a block of string data to a StringWriter object rather than a file on the local hard drive:
Please visit Domain Name Hosting services for high quality webhost to host and run your jsp applications.

CHAPTER 16 THE SYSTEM.IO NAMESPACE 535 static (Adult web hosting)

Tuesday, February 26th, 2008

CHAPTER 16 THE SYSTEM.IO NAMESPACE 535 static void Main(string[] args) { Console.WriteLine(”***** Fun with StreamWriter / StreamReader *****n”); // Get a StreamWriter and write string data. StreamWriter writer = File.CreateText(”reminders.txt”); writer.WriteLine(”Don’t forget Mother’s Day this year…”); writer.WriteLine(”Don’t forget Father’s Day this year…”); writer.WriteLine(”Don’t forget these numbers:”); for(int i = 0; i < 10; i++) writer.Write(i + " "); // Insert a new line. writer.Write(writer.NewLine); // Closing automatically flushes! writer.Close(); Console.WriteLine("Created file and wrote some thoughts..."); } Once you run this program, you can examine the contents of this new file (see Figure 16-8). Figure 16-8. The contents of your *.txt file Reading from a Text File Now you need to understand how to programmatically read data from a file using the corresponding StreamReader type. As you recall, this class derives from TextReader, which offers the functionality described in Table 16-8. Table 16-8. TextReader Core Members Member Meaning in Life Peek() Returns the next available character without actually changing the position of the reader. A value of 1 indicates you are at the end of the stream. Read() Reads data from an input stream. ReadBlock() Reads a maximum of count characters from the current stream and writes the data to a buffer, beginning at index. ReadLine() Reads a line of characters from the current stream and returns the data as a string (a null string indicates EOF). ReadToEnd() Reads all characters from the current position to the end of the stream and returns them as a single string. If you now extend the current MyStreamWriterReader class to use a StreamReader, you can read in the textual data from the reminders.txt file as shown here:
Searching for affordable and reliable webhost to host and run your web applications? Go to our java web server services and you will be pleased.

Web hosting script - CHAPTER 16 534 THE SYSTEM.IO NAMESPACE To

Monday, February 25th, 2008

CHAPTER 16 534 THE SYSTEM.IO NAMESPACE To aid in your understanding of the core writing capabilities of the StreamWriter and StringWriter classes, Table 16-7 describes the core members of the abstract TextWriter base class. Table 16-7. Core Members of TextWriter Member Meaning in Life Close() Closes the writer and frees any associated resources. In the process, the buffer is automatically flushed. Flush() Clears all buffers for the current writer and causes any buffered data to be written to the underlying device, but does not close the writer. NewLine Indicates the newline constant for the derived writer class. The default line terminator is a carriage return followed by a line feed (rn). Write() Writes a line to the text stream without a newline constant. WriteLine() Writes a line to the text stream with a newline constant. Note The last two members of the TextWriter class probably look familiar to you. If you recall, the System. Console type has Write() and WriteLine() members that push textual data to the standard output device. In fact, the Console.In property wraps a TextWriter, and the Console.Out property wraps a TextReader. The derived StreamWriter class provides an appropriate implementation for the Write(), Close(), and Flush() methods, and it defines the additional AutoFlush property. This property, when set to true, forces StreamWriter to flush all data every time you perform a write operation. Be aware that you can gain better performance by setting AutoFlush to false, provided you always call Close() when you are done writing with a StreamWriter. Writing to a Text File Now for an example of working with the StreamWriter type. The following class creates a new file named reminders.txt using the File.CreateText() method. Using the obtained StreamWriter object, you add some textual data to the new file, as shown here: Figure 16-7. Readers and writers
In case you need quality webspace to host and run your web applications, try our personal web hosting services.

Web hosting unlimited bandwidth - CHAPTER 16 THE SYSTEM.IO NAMESPACE 533 //

Sunday, February 24th, 2008

CHAPTER 16 THE SYSTEM.IO NAMESPACE 533 // Encode a string as an array of bytes. string msg = “Hello!”; byte[] msgAsByteArray = Encoding.Default.GetBytes(msg); // Write byte[] to file. fStream.Write(msgAsByteArray, 0, msgAsByteArray.Length); // Reset internal position of stream. fStream.Position = 0; // Read the types from file and display to console. Console.Write(”Your message as an array of bytes: “); byte[] bytesFromFile = new byte[msgAsByteArray.Length]; for (int i = 0; i < msgAsByteArray.Length; i++) { bytesFromFile[i] = (byte)fStream.ReadByte(); Console.Write(bytesFromFile[i]); } // Display decoded messages. Console.Write("nDecoded Message: "); Console.WriteLine(Encoding.Default.GetString(bytesFromFile)); // Close stream. fStream.Close(); } While this example does indeed populate the file with data, it punctuates the major downfall of working directly with the FileStream type: it demands to operate on raw bytes. Other Stream-derived types operate in a similar manner. For example, if you wish to write a sequence of bytes to a region of memory, you can allocate a MemoryStream. Likewise, if you wish to push an array of bytes through a network connection, you can make use of the NetworkStream type. Thankfully, the System.IO namespace provides a number of reader and writer types that encapsulate the details of working with Stream-derived types. Source Code The FileStreamApp project is included under the Chapter 16 subdirectory. Working with StreamWriters and StreamReaders The StreamWriter and StreamReader classes are useful whenever you need to read or write characterbased data (e.g., strings). Both of these types work by default with Unicode characters; however, you can change this by supplying a properly configured System.Text.Encoding object reference. To keep things simple, let s assume that the default Unicode encoding fits the bill. StreamReader derives from an abstract type named TextReader, as does the related StringReader type (discussed later in this chapter). The TextReader base class provides a very limited set of functionality to each of these descendents, specifically the ability to read and peek into a character stream. The StreamWriter type (as well as StringWriter, also examined later in this chapter) derives from an abstract base class named TextWriter. This class defines members that allow derived types to write textual data to a given character stream. The relationship between each of these new I/Ocentric types is shown in Figure 16-7.
We recommend high quality webhost to host and run your jsp application: christian web host services.

CHAPTER 16 532 THE SYSTEM.IO (Web hosting billing) NAMESPACE Table

Saturday, February 23rd, 2008

CHAPTER 16 532 THE SYSTEM.IO NAMESPACE Table 16-6. Abstract StreamMembers Members Meaning in Life CanRead Determine whether the current stream supports reading, seeking, and/or CanSeek writing. CanWrite Close() Closes the current stream and releases any resources (such as sockets and file handles) associated with the current stream. Flush() Updates the underlying data source or repository with the current state of the buffer and then clears the buffer. If a stream does not implement a buffer, this method does nothing. Length Returns the length of the stream, in bytes. Position Determines the position in the current stream. Read() Read a sequence of bytes (or a single byte) from the current stream and advance ReadByte() the current position in the stream by the number of bytes read. Seek() Sets the position in the current stream. SetLength() Sets the length of the current stream. Write() Write a sequence of bytes (or a single byte) to the current stream and advance WriteByte() the current position in this stream by the number of bytes written. Working with FileStreams The FileStream class provides an implementation for the abstract Stream members in a manner appropriate for file-based streaming. It is a fairly primitive stream; it can read or write only a single byte or an array of bytes. In reality, you will not often need to directly interact with the members of the FileStream type. Rather, you will most likely make use of various stream wrappers, which make it easier to work with textual data or .NET types. Nevertheless, for illustrative purposes, let s experiment with the synchronous read/write capabilities of the FileStream type. Assume you have a new console application named FileStreamApp. Your goal is to write a simple text message to a new file named myMessage.dat. However, given that FileStream can operate only on raw bytes, you will be required to encode the System.String type into a corresponding byte array. Luckily, the System.Text namespace defines a type named Encoding, which provides members that encode and decode strings to (or from) an array of bytes (check out the .NET Framework 2.0 SDK documentation for full details of the Encoding type). Once encoded, the byte array is persisted to file using the FileStream.Write() method. To read the bytes back into memory, you must reset the internal position of the stream (via the Position property) and call the ReadByte() method. Finally, you display the raw byte array and the decoded string to the console. Here is the complete Main() method: // Don’t forget to ‘use’ System.Text. static void Main(string[] args) { Console.WriteLine(”***** Fun with FileStreams *****n”); // Obtain a FileStream object. FileStream fStream = File.Open(@”C:myMessage.dat”, FileMode.Create);
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.

CHAPTER 16 THE SYSTEM.IO NAMESPACE 531 (Free web hosting with ftp) static

Friday, February 22nd, 2008

CHAPTER 16 THE SYSTEM.IO NAMESPACE 531 static void Main(string[] args) { // Display info about boot.ini and then open // for read-only access. FileInfo bootFile = new FileInfo(@”C:boot.ini”); Console.WriteLine(bootFile.CreationTime); Console.WriteLine(bootFile.LastAccessTime); FileStream readOnlyStream = bootFile.OpenRead(); readOnlyStream.Close(); } The Abstract Stream Class At this point, you have seen numerous ways to obtain FileStream, StreamReader, and StreamWriter objects, but you have yet to read data from, or written data to, a file using these types. To understand how to do so, you ll need to become familiar with the concept of a stream. In the world of I/O manipulation, a stream represents a chunk of data. Streams provide a common way to interact with a sequence of bytes, regardless of what kind of device (file, network connection, printer, etc.) is storing or displaying the bytes in question. The abstract System.IO.Stream class defines a number of members that provide support for synchronous and asynchronous interactions with the storage medium (e.g., an underlying file or memory location). Figure 16-6 shows a few descendents of the Stream type. Figure 16-6. Stream-derived types Note Be aware that the concept of a stream is not limited to files or memory locations. To be sure, the .NET libraries provide stream access to networks and other stream-centric abstractions. Again, Stream descendents represent data as a raw stream of bytes; therefore, working with raw streams can be quite cryptic. Some Stream-derived types support seeking, which refers to the process of obtaining and adjusting the current position in the stream. To begin understanding the functionality provided by the Stream class, take note of the core members described in Table 16-6.
Searching for affordable and reliable webhost to host and run your web applications? Go to our java web server services and you will be pleased.

Best web design - // Get some StreamWriters. StreamWriter swriter = File.CreateText(@”C:Test3.txt”);

Thursday, February 21st, 2008

// Get some StreamWriters. StreamWriter swriter = File.CreateText(@”C:Test3.txt”); swriter.Close(); StreamWriter swriterAppend = File.AppendText(@”C:FinalTest.txt”); swriterAppend.Close(); } New .NET 2.0 File Members Unlike FileInfo, the File type supports a few unique members (as of .NET 2.0) shown in Table 16-5, which can greatly simplify the processes of reading and writing textual data. Table 16-5. Methods of the File Type Method Meaning in Life ReadAllBytes() Opens the specified file, returns the binary data as an array of bytes, and then closes the file ReadAllLines() Opens a specified file, returns the character data as an array of strings, and then closes the file ReadAllText() Opens a specified file, returns the character data as a System.String, and then closes the file WriteAllBytes() Opens the specified file, writes out the byte array, and then closes the file WriteAllLines() Opens a specified file, writes out an array of strings, and then closes the file WriteAllText() Opens a specified file, writes the character data, and then closes the file Using these new methods of the File type, you are able to read and write batches of data in just a few lines of code. Even better, each of these new members automatically closes down the underlying file handle, for example: class Program { static void Main(string[] args) { string[] myTasks = { “Fix bathroom sink”, “Call Dave”, “Call Mom and Dad”, “Play XBox”}; // Write out all data to file on C drive. File.WriteAllLines(@”C:tasks.txt”, myTasks); // Read it all back and print out. foreach (string task in File.ReadAllLines(@”C:tasks.txt”)) { Console.WriteLine(”TODO: {0}”, task); } } } Clearly, when you wish to quickly obtain a file handle, the File type will save you some keystrokes. However, one benefit of first creating a FileInfo object is that you are able to investigate the file using the members of the abstract FileSystemInfo base class: CHAPTER 16 530 THE SYSTEM.IO NAMESPACE
We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.