Csharp/C Sharp/File Stream/Text File Read Write — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 15:31, 26 мая 2010
Содержание
- 1 Read and Write a Text File
- 2 Read ASCII string from byte buffer
- 3 Reads and displays bytes until end-of-file
- 4 Read text file line by line
- 5 Read text file line by line with exception catch
- 6 Read whole text file to the end
- 7 Text file Write with format and write boolean value to a text file
- 8 Write string to a text file
Read and Write a Text File
using System;
using System.IO;
using System.Text;
public class TextFileTest {
private static void Main() {
FileStream fs = new FileStream("test.txt", FileMode.Create);
StreamWriter w = new StreamWriter(fs, Encoding.UTF8);
w.WriteLine(1.2M);
w.WriteLine("string");
w.WriteLine("!");
w.Flush();
w.Close();
fs.Close();
fs = new FileStream("test.txt", FileMode.Open);
StreamReader r = new StreamReader(fs, Encoding.UTF8);
Console.WriteLine(Decimal.Parse(r.ReadLine()));
Console.WriteLine(r.ReadLine());
Console.WriteLine(Char.Parse(r.ReadLine()));
r.Close();
fs.Close();
}
}
Read ASCII string from byte buffer
using System;
using System.Globalization;
using System.Windows.Forms;
using System.Data;
using System.IO;
public class EntryPoint
{
static void Main( string[] args ) {
FileStream file = File.OpenRead("test.cs");
byte[] buffer = new byte[1024];
int c = file.Read(buffer, 0, buffer.Length);
while (c > 0)
{
Console.WriteLine(System.Text.ASCIIEncoding.ASCII.GetString(buffer));
c = file.Read(buffer, 0, buffer.Length);
}
file.Close();
}
}
Reads and displays bytes until end-of-file
using System;
using System.IO;
public class ReadBytes {
public static void Main(string[] args) {
Stream input = new FileStream("test.cs", FileMode.Open);
int i;
while((i = input.ReadByte()) != -1)
Console.Write(i + " ");
input.Close();
}
}
Read text file line by line
using System;
using System.IO;
public class ReadWriteApp
{
public static void Main(string[] args)
{
FileStream s = new FileStream("Bar.txt", FileMode.Create);
StreamWriter w = new StreamWriter(s);
w.Write("Hello World");
w.Close();
s = new FileStream("Bar.txt", FileMode.Open);
StreamReader r = new StreamReader(s);
string t;
while ((t = r.ReadLine()) != null)
{
Console.WriteLine(t);
}
w.Close();
}
}
Read text file line by line with exception catch
using System;
using System.IO;
class Class1{
static void Main(string[] args){
string strLine;
try
{
FileStream aFile = new FileStream("books.xml",FileMode.Open);
StreamReader sr = new StreamReader(aFile);
strLine = sr.ReadLine();
while(strLine != null)
{
Console.WriteLine(strLine);
strLine = sr.ReadLine();
}
aFile.Close();
sr.Close();
}
catch(IOException e)
{
Console.WriteLine("An IO exception has been thrown!");
Console.WriteLine(e.ToString());
return;
}
return;
}
}
Read whole text file to the end
using System;
using System.Data;
using System.IO;
class Class1{
static void Main(string[] args){
StreamReader sr = new StreamReader("practice.txt");
Console.WriteLine(sr.ReadToEnd());
sr.Close();
}
}
Text file Write with format and write boolean value to a text file
using System;
using System.IO;
class Class1{
static void Main(string[] args){
try
{
FileStream aFile = new FileStream("practice.txt",FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(aFile);
bool truth = true;
sw.WriteLine("A");
sw.WriteLine("{0}",System.DateTime.Now.ToShortDateString());
sw.Write("A");
sw.Write("www.nfex.ru",truth);
sw.Close();
}
catch(IOException e)
{
Console.WriteLine("An IO exception has been thrown!");
Console.WriteLine(e.ToString());
Console.ReadLine();
return;
}
return;
}
}
Write string to a text file
using System;
using System.Data;
using System.IO;
class Class1{
static void Main(string[] args){
StreamWriter sw = new StreamWriter("practice.txt");
sw.Write("www.nfex.ru");
sw.Close();
}
}