Csharp/C Sharp/File Stream/Ascii String Read Write — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 11:45, 26 мая 2010
Содержание
Demonstrate StringReader and StringWriter
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate StringReader and StringWriter
using System;
using System.IO;
public class StrRdrDemo {
public static void Main() {
// Create a StringWriter
StringWriter strwtr = new StringWriter();
// Write to StringWriter.
for(int i=0; i < 10; i++)
strwtr.WriteLine("This is i: " + i);
// Create a StringReader
StringReader strrdr = new StringReader(strwtr.ToString());
// Now, read from StringReader.
string str = strrdr.ReadLine();
while(str != null) {
str = strrdr.ReadLine();
Console.WriteLine(str);
}
}
}
Manipulating a string read as a line from a file
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
//
// Builder.cs -- Demonstrates manipulating a string read as a line from a file
// Compile this program with the following command line:
// C:>csc Builder.cs
//
namespace nsBuilder
{
using System;
using System.Text;
using System.IO;
public class Builder
{
static public int Main ()
{
// Create a stream object and open the input file
FileStream istream;
try
{
istream = new FileStream ("sample.txt", FileMode.Open, FileAccess.Read);
}
catch (Exception)
{
Console.WriteLine ("Could not open sample.txt for reading");
return (-1);
}
// Associate a reader with the stream
StreamReader reader = new StreamReader (istream);
// Declare a new StringBuilder
StringBuilder strb = new StringBuilder ();
// Counter for the lines
int Lines = 0;
while (reader.Peek() > 0)
{
++Lines;
// Clear out the string builder
strb.Length = 0;
// Read a line from the file
string str = reader.ReadLine();
// Split the line into words
string [] Data = str.Split (new char [] {" "});
// Build the output line to show line, word and character count
strb.AppendFormat ("Line {0} contains {1} words and {2} characters:\r\n{3}", Lines, Data.Length, str.Length, str);
// Write the string to the console
Console.WriteLine (strb.ToString ());
}
istream.Close ();
return (0);
}
}
}
//File: sample.txt
/*
The quick red fox jumps over the lazy brown dog.
Now is the time for all good men to come to the aid of their Teletype.
Peter Piper picked a peck of peppered pickles.
*/
Reads an ASCII encoded file and writes the text to another file in wide character format
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
// Convert.cs -- Reads an ASCII encoded file and writes the text to another file
// in wide character format.
// Compile this program with the following command line:
// C:>csc Convert.cs
//
namespace nsConvert
{
using System;
using System.Text;
using System.IO;
public class Convert
{
static public int Main ()
{
// First, make sure both the input and output files can be opened
FileStream ostream;
FileStream istream;
try
{
istream = new FileStream ("Sample.asc", FileMode.Open, FileAccess.Read);
}
catch (Exception)
{
Console.WriteLine ("Cannot open Sample.asc for reading");
return (-1);
}
try
{
ostream = new FileStream ("Sample.wcs", FileMode.Create, FileAccess.ReadWrite);
}
catch (Exception)
{
Console.WriteLine ("Cannot open Sample.wcs for writing");
istream.Close ();
return (-1);
}
// Create a stream reader and attach the input stream with ASCII encoding
StreamReader reader = new StreamReader (istream, new ASCIIEncoding());
string str = reader.ReadToEnd ();
// Create a stream writer and attach the output stream using Unicode encoding
StreamWriter writer = new StreamWriter (ostream, new UnicodeEncoding());
// Write the text to the file.
writer.Write (str);
// Flush the output stream
writer.Flush ();
// Close the streams
ostream.Close ();
istream.Close ();
return (0);
}
}
}
//File: Sample.asc
/*
The quick red fox jumps over the lazy brown dog.
Now is the time for all good men to come to the aid of their Teletype.
Peter Piper picked a peck of peppered pickles.
*/
Text Reader
/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury,
Zach Greenvoss, Shripad Kulkarni, Neil Whitlow
Publisher: Peer Information
ISBN: 1861007663
*/
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Text;
namespace Wrox.WindowsGUIProgramming.Chapter10
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class TextReader : System.Windows.Forms.Form
{
private System.Windows.Forms.RichTextBox rtbText;
private System.Windows.Forms.Button butSomeText;
private System.Windows.Forms.Button butSomeMoreText;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ruponentModel.Container components = null;
public TextReader()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.rtbText = new System.Windows.Forms.RichTextBox();
this.butSomeText = new System.Windows.Forms.Button();
this.butSomeMoreText = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// rtbText
//
this.rtbText.Name = "rtbText";
this.rtbText.Size = new System.Drawing.Size(296, 232);
this.rtbText.TabIndex = 0;
this.rtbText.Text = "";
//
// butSomeText
//
this.butSomeText.Location = new System.Drawing.Point(16, 240);
this.butSomeText.Name = "butSomeText";
this.butSomeText.Size = new System.Drawing.Size(88, 23);
this.butSomeText.TabIndex = 1;
this.butSomeText.Text = "Some Text";
this.butSomeText.Click += new System.EventHandler(this.butSomeText_Click);
//
// butSomeMoreText
//
this.butSomeMoreText.Location = new System.Drawing.Point(168, 240);
this.butSomeMoreText.Name = "butSomeMoreText";
this.butSomeMoreText.Size = new System.Drawing.Size(104, 23);
this.butSomeMoreText.TabIndex = 2;
this.butSomeMoreText.Text = "Some More Text";
this.butSomeMoreText.Click += new System.EventHandler(this.butSomeMoreText_Click);
//
// TextReader
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.butSomeMoreText,
this.butSomeText,
this.rtbText});
this.Name = "TextReader";
this.Text = "TextReader";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new TextReader());
}
private void butSomeText_Click(object sender, System.EventArgs e)
{
Class1 c = new Class1();
rtbText.Text = c.ReadText();
}
private void butSomeMoreText_Click(object sender, System.EventArgs e)
{
Class1 c = new Class1();
rtbText.Text = c.ReadMoreText();
}
}
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Class1
{
public Class1()
{
//
// TODO: Add constructor logic here
//
}
public string ReadText()
{
//FileStream fs = File.OpenRead("C:\Documents and Settings\richard.conway\My Documents\Visual Studio Projects\TextReader\bin\Debug\readtext.rtf")
FileStream f = File.OpenRead(System.Environment.CurrentDirectory+"\\readtext.rtf");
StreamReader s = new StreamReader(f);
return s.ReadToEnd();
}
public string ReadMoreText()
{
FileStream f = File.OpenRead(System.Environment.CurrentDirectory+"\\readmoretext.rtf");
StreamReader s = new StreamReader(f);
return s.ReadToEnd();
}
}
}
<A href="http://www.nfex.ru/Code/CSharpDownload/TextReader.zip">TextReader.zip( 2 k)</a>
Using String Reader
/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
* Version: 1
*/
using System;
using System.IO;
namespace Client.Chapter_10___Interop_Services
{
public class UsingStringReader {
static void Main(string[] args)
{
// Create a string to read characters from.
String MyString = "Hello World";
// Size the array to hold all the characters of the string,
// so that they are all accessible.
char[] MyChar = new char[12];
// Create a StringReader and attach it to the string.
StringReader MyStringReader = new StringReader(MyString);
// Read 5 characters from the array that holds the string, starting
// from the first array member.
MyStringReader.Read(MyChar, 0, 5);
// Display the output.
Console.WriteLine(MyChar);
// Close the StringReader.
MyStringReader.Close();
}
}
}