
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Different ways of Reading a file in C#
Here, we are reading two different files −
Reading text file −
Example
using System; using System.IO; namespace FileApplication { class Program { static void Main(string[] args) { try { using (StreamReader sr = new StreamReader("d:/new.txt")) { string line; // Read and display lines from the file until // the end of the file is reached. while ((line = sr.ReadLine()) != null) { Console.WriteLine(line); } } } catch (Exception e) { // Let the user know what went wrong. Console.WriteLine("The file could not be read:"); Console.WriteLine(e.Message); } Console.ReadKey(); } } }
Output
The file could not be read: Could not find a part of the path "/home/cg/root/4281363/d:/new.txt".
Reading binary file −
Example
using System.IO; namespace BinaryFileApplication { class Program { static void Main(string[] args) { BinaryWriter bw; BinaryReader br; int i = 25; double d = 3.14157; bool b = true; string s = "I am happy"; //create the file try { bw = new BinaryWriter(new FileStream("mydata", FileMode.Create)); } catch (IOException e) { Console.WriteLine(e.Message + "
Cannot create file."); return; } //writing into the file try { bw.Write(i); bw.Write(d); bw.Write(b); bw.Write(s); } catch (IOException e) { Console.WriteLine(e.Message + "
Cannot write to file."); return; } bw.Close(); //reading from the file try { br = new BinaryReader(new FileStream("mydata", FileMode.Open)); } catch (IOException e) { Console.WriteLine(e.Message + "
Cannot open file."); return; } try { i = br.ReadInt32(); Console.WriteLine("Integer data: {0}", i); d = br.ReadDouble(); Console.WriteLine("Double data: {0}", d); b = br.ReadBoolean(); Console.WriteLine("Boolean data: {0}", b); s = br.ReadString(); Console.WriteLine("String data: {0}", s); } catch (IOException e) { Console.WriteLine(e.Message + "
Cannot read from file."); return; } br.Close(); Console.ReadKey(); } } }
- Related Articles
- Reading and writing binary file in C/C++
- Reading a Text file in java
- Reading a JSON file using Swift
- Reading/Writing a MS Word file in PHP
- Different ways to start a Task in C#
- Opening and reading a file with askopenfilename in Tkinter?
- Reading a text file into an Array in Node.js
- Reading UTF8 data from a file using Java
- Different Ways to Add Parentheses in C++
- Three Different ways to calculate factorial in C#
- Print system time in C++ (3 different ways)
- Reading and Writing CSV File using Python
- Different ways for Integer to String Conversions in C#
- Different ways to declare variable as constant in C and C++
- Number of Ways to Wear Different Hats to Each Other in C++

Advertisements