Csharp/C Sharp/File Stream/Text File Read Write

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Read and Write a Text File

<source lang="csharp"> 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();
   }

}

</source>


Read ASCII string from byte buffer

<source lang="csharp"> 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();
   }

}

      </source>


Reads and displays bytes until end-of-file

<source lang="csharp"> 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();
 }

}

      </source>


Read text file line by line

<source lang="csharp"> 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();
   }

}

</source>


Read text file line by line with exception catch

<source lang="csharp"> 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;
    }

}


      </source>


Read whole text file to the end

<source lang="csharp"> 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();   
   }

}


      </source>


Text file Write with format and write boolean value to a text file

<source lang="csharp"> 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;
     }

}


      </source>


Write string to a text file

<source lang="csharp"> 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();
   }

}

      </source>