SlideShare a Scribd company logo
1 of 12
Application on File Program
Session 8
Objectives
 Typically example of file read & write
 Simple File Handling Program
 Several Home work
Typical example of file read & write
try
{
FileStream aFile = new FileStream("Log.txt",
FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(aFile);
bool truth = true // Write data to file.
sw.WriteLine("Hello to you.");
sw.WriteLine("It is now {0} and things are looking
good.“,DateTime.Now.ToLongDateString());
sw.Write("More than that,");
sw.Write(" it’s {0} that C# is fun.", truth);
sw.Close();
}
catch (IOException ee)
{
Console.WriteLine("An IO exception has been
thrown!");
Console.WriteLine(ee.ToString());
Console.ReadLine();
return;
}
• Go to the application directory and find the Log.txt
file. It is located in the binDebug folder because you
used a relative path.
• The WriteLine() method writes the string passed to
it, followed immediately by a newline character.
• You can see in the example that this causes the next
write operation to begin on a new line.
• The Write() method simply writes the string passed
to it to the file, without a newline character appended.
• Two sentence is in one line.
The StreamReader Object
• try
• {
• FileStream aFile = new FileStream("Log.txt", FileMode.Open);
• StreamReader sr = new StreamReader(aFile);
• line = sr.ReadLine();
• // Read data in line by line.
• while(line != null)
• {
• Console.WriteLine(line);
• line = sr.ReadLine();
• }
• sr.Close();
• }
• catch(IOException e)
• {
• Console.WriteLine("An IO exception has been thrown!");
• Console.WriteLine(e.ToString());
• return;
• }
• Console.ReadKey();
 ReadLine() method to read text from the file.
 This method reads text until a new line is found,and returns
the resulting text as a string.
 The method returns a null when the end of the file has been
reached, which you use to test for the end of the file.
 Note that you use a while loop, which ensures that the line
read isn’t null before any code in the body of the loop is
executed.
Simple File Handling Program
String line,k="";
StreamReader sr = new
StreamReader("E:trainingreadsample.txt");
StreamWriter sw = new
StreamWriter("E:trainingWritesample.txt");
line = sr.ReadLine();
while (line != null)
{
k += line;
line = sr.ReadLine();
}
sw.WriteLine(k);
sw.Close();
sr.Dispose();
Direct using streamReader & streamWriter
Input :
readsample.txt Contain
• welcome TO .NET WORLD.
• Asp.Net IS Microsoft Own Product.
• File handling is done by Streamreader & streamwriter
• Simple program about File.
• OutPut:
• Writesample.txt Contain
• welcome TO .NET WORLD.Asp.Net IS Microsoft
Own Product.File handling is done by Streamreader
& streamwriter .Simple program about File.
Tasks(to be done in the class session):
• 1)Print first 5 characters from a file in a single line.
• 2)Print number of each line.
• 3)Create two files where 1st
file will contain 1st
&3rd
lines and the other file will contain 2nd
&4th
lines.
ASP.NET Session 8

More Related Content

What's hot

2013 CrossRef Workshops System Update: Guarding Your Data, Andrew Gilmartin
2013 CrossRef Workshops System Update: Guarding Your Data, Andrew Gilmartin2013 CrossRef Workshops System Update: Guarding Your Data, Andrew Gilmartin
2013 CrossRef Workshops System Update: Guarding Your Data, Andrew Gilmartin
Crossref
 
Logstash + Elasticsearch + Kibana Presentation on Startit Tech Meetup
Logstash + Elasticsearch + Kibana Presentation on Startit Tech MeetupLogstash + Elasticsearch + Kibana Presentation on Startit Tech Meetup
Logstash + Elasticsearch + Kibana Presentation on Startit Tech Meetup
Startit
 
Cassandra Codebase 2011
Cassandra Codebase 2011Cassandra Codebase 2011
Cassandra Codebase 2011
gdusbabek
 
Data Processing with Cascading Java API on Apache Hadoop
Data Processing with Cascading Java API on Apache HadoopData Processing with Cascading Java API on Apache Hadoop
Data Processing with Cascading Java API on Apache Hadoop
Hikmat Dhamee
 
Asynchronous programming
Asynchronous programmingAsynchronous programming
Asynchronous programming
Chester Hartin
 

What's hot (20)

2013 CrossRef Workshops System Update: Guarding Your Data, Andrew Gilmartin
2013 CrossRef Workshops System Update: Guarding Your Data, Andrew Gilmartin2013 CrossRef Workshops System Update: Guarding Your Data, Andrew Gilmartin
2013 CrossRef Workshops System Update: Guarding Your Data, Andrew Gilmartin
 
Secure code 3rd_party_libs
Secure code 3rd_party_libsSecure code 3rd_party_libs
Secure code 3rd_party_libs
 
Logstash + Elasticsearch + Kibana Presentation on Startit Tech Meetup
Logstash + Elasticsearch + Kibana Presentation on Startit Tech MeetupLogstash + Elasticsearch + Kibana Presentation on Startit Tech Meetup
Logstash + Elasticsearch + Kibana Presentation on Startit Tech Meetup
 
ENT302 Deploying Microsoft Exchange and SharePoint on AWS - AWS re: Invent 2012
ENT302 Deploying Microsoft Exchange and SharePoint on AWS - AWS re: Invent 2012ENT302 Deploying Microsoft Exchange and SharePoint on AWS - AWS re: Invent 2012
ENT302 Deploying Microsoft Exchange and SharePoint on AWS - AWS re: Invent 2012
 
Moving towards unified logging
Moving towards unified loggingMoving towards unified logging
Moving towards unified logging
 
2310 b 12
2310 b 122310 b 12
2310 b 12
 
Compilation
CompilationCompilation
Compilation
 
Scaling an ELK stack at bol.com
Scaling an ELK stack at bol.comScaling an ELK stack at bol.com
Scaling an ELK stack at bol.com
 
C# p4
C# p4C# p4
C# p4
 
Logging, Serilog, Structured Logging, Seq
Logging, Serilog, Structured Logging, SeqLogging, Serilog, Structured Logging, Seq
Logging, Serilog, Structured Logging, Seq
 
Customer Intelligence: Using the ELK Stack to Analyze ForgeRock OpenAM Audit ...
Customer Intelligence: Using the ELK Stack to Analyze ForgeRock OpenAM Audit ...Customer Intelligence: Using the ELK Stack to Analyze ForgeRock OpenAM Audit ...
Customer Intelligence: Using the ELK Stack to Analyze ForgeRock OpenAM Audit ...
 
Heap
HeapHeap
Heap
 
Iron python
Iron pythonIron python
Iron python
 
ELK Stack
ELK StackELK Stack
ELK Stack
 
Code
CodeCode
Code
 
Cassandra Codebase 2011
Cassandra Codebase 2011Cassandra Codebase 2011
Cassandra Codebase 2011
 
Data Processing with Cascading Java API on Apache Hadoop
Data Processing with Cascading Java API on Apache HadoopData Processing with Cascading Java API on Apache Hadoop
Data Processing with Cascading Java API on Apache Hadoop
 
Getting to Know the Cassandra Codebase
Getting to Know the Cassandra CodebaseGetting to Know the Cassandra Codebase
Getting to Know the Cassandra Codebase
 
Asynchronous programming
Asynchronous programmingAsynchronous programming
Asynchronous programming
 
Reactive Extensions
Reactive ExtensionsReactive Extensions
Reactive Extensions
 

Viewers also liked

ASP.NET Session 10
ASP.NET Session 10ASP.NET Session 10
ASP.NET Session 10
Sisir Ghosh
 
ASP.NET Session 4
ASP.NET Session 4ASP.NET Session 4
ASP.NET Session 4
Sisir Ghosh
 
ASP.NET Session 2
ASP.NET Session 2ASP.NET Session 2
ASP.NET Session 2
Sisir Ghosh
 
ASP.NET Session 9
ASP.NET Session 9ASP.NET Session 9
ASP.NET Session 9
Sisir Ghosh
 
ASP.NET Session 5
ASP.NET Session 5ASP.NET Session 5
ASP.NET Session 5
Sisir Ghosh
 
ASP.NET Session 7
ASP.NET Session 7ASP.NET Session 7
ASP.NET Session 7
Sisir Ghosh
 
ASP.NET Session 6
ASP.NET Session 6ASP.NET Session 6
ASP.NET Session 6
Sisir Ghosh
 
ASP.NET Session 3
ASP.NET Session 3ASP.NET Session 3
ASP.NET Session 3
Sisir Ghosh
 

Viewers also liked (8)

ASP.NET Session 10
ASP.NET Session 10ASP.NET Session 10
ASP.NET Session 10
 
ASP.NET Session 4
ASP.NET Session 4ASP.NET Session 4
ASP.NET Session 4
 
ASP.NET Session 2
ASP.NET Session 2ASP.NET Session 2
ASP.NET Session 2
 
ASP.NET Session 9
ASP.NET Session 9ASP.NET Session 9
ASP.NET Session 9
 
ASP.NET Session 5
ASP.NET Session 5ASP.NET Session 5
ASP.NET Session 5
 
ASP.NET Session 7
ASP.NET Session 7ASP.NET Session 7
ASP.NET Session 7
 
ASP.NET Session 6
ASP.NET Session 6ASP.NET Session 6
ASP.NET Session 6
 
ASP.NET Session 3
ASP.NET Session 3ASP.NET Session 3
ASP.NET Session 3
 

Similar to ASP.NET Session 8

Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01
Rex Joe
 
Data file handling
Data file handlingData file handling
Data file handling
TAlha MAlik
 
Input File dalam C++
Input File dalam C++Input File dalam C++
Input File dalam C++
Teguh Nugraha
 

Similar to ASP.NET Session 8 (20)

15. text files
15. text files15. text files
15. text files
 
Files and streams
Files and streamsFiles and streams
Files and streams
 
working with files
working with filesworking with files
working with files
 
15 Text files
15 Text files15 Text files
15 Text files
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File Handling
 
Files in c++
Files in c++Files in c++
Files in c++
 
Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01
 
Get docs from sp doc library
Get docs from sp doc libraryGet docs from sp doc library
Get docs from sp doc library
 
7 Data File Handling
7 Data File Handling7 Data File Handling
7 Data File Handling
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
Java file
Java fileJava file
Java file
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
Reading and Writing Files
Reading and Writing FilesReading and Writing Files
Reading and Writing Files
 
Data file handling
Data file handlingData file handling
Data file handling
 
Chapter 11
Chapter 11Chapter 11
Chapter 11
 
Python - Lecture 9
Python - Lecture 9Python - Lecture 9
Python - Lecture 9
 
Data file handling
Data file handlingData file handling
Data file handling
 
COM1407: File Processing
COM1407: File Processing COM1407: File Processing
COM1407: File Processing
 
CSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdfCSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdf
 
Input File dalam C++
Input File dalam C++Input File dalam C++
Input File dalam C++
 

More from Sisir Ghosh

ASP.NET Session 11 12
ASP.NET Session 11 12ASP.NET Session 11 12
ASP.NET Session 11 12
Sisir Ghosh
 
ASP.NET Session 13 14
ASP.NET Session 13 14ASP.NET Session 13 14
ASP.NET Session 13 14
Sisir Ghosh
 
ASP.NET Session 16
ASP.NET Session 16ASP.NET Session 16
ASP.NET Session 16
Sisir Ghosh
 
ASP.NET System design 2
ASP.NET System design 2ASP.NET System design 2
ASP.NET System design 2
Sisir Ghosh
 
ASP.NET Session 1
ASP.NET Session 1ASP.NET Session 1
ASP.NET Session 1
Sisir Ghosh
 
Network security
Network securityNetwork security
Network security
Sisir Ghosh
 
Module ii physical layer
Module ii physical layerModule ii physical layer
Module ii physical layer
Sisir Ghosh
 
Error detection and correction
Error detection and correctionError detection and correction
Error detection and correction
Sisir Ghosh
 
Overview of data communication and networking
Overview of data communication and networkingOverview of data communication and networking
Overview of data communication and networking
Sisir Ghosh
 
Application layer
Application layerApplication layer
Application layer
Sisir Ghosh
 

More from Sisir Ghosh (12)

ASP.NET Session 11 12
ASP.NET Session 11 12ASP.NET Session 11 12
ASP.NET Session 11 12
 
ASP.NET Session 13 14
ASP.NET Session 13 14ASP.NET Session 13 14
ASP.NET Session 13 14
 
ASP.NET Session 16
ASP.NET Session 16ASP.NET Session 16
ASP.NET Session 16
 
ASP.NET System design 2
ASP.NET System design 2ASP.NET System design 2
ASP.NET System design 2
 
ASP.NET Session 1
ASP.NET Session 1ASP.NET Session 1
ASP.NET Session 1
 
Transport layer
Transport layerTransport layer
Transport layer
 
Routing
RoutingRouting
Routing
 
Network security
Network securityNetwork security
Network security
 
Module ii physical layer
Module ii physical layerModule ii physical layer
Module ii physical layer
 
Error detection and correction
Error detection and correctionError detection and correction
Error detection and correction
 
Overview of data communication and networking
Overview of data communication and networkingOverview of data communication and networking
Overview of data communication and networking
 
Application layer
Application layerApplication layer
Application layer
 

Recently uploaded

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Recently uploaded (20)

MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 

ASP.NET Session 8

  • 1. Application on File Program Session 8
  • 2. Objectives  Typically example of file read & write  Simple File Handling Program  Several Home work
  • 3. Typical example of file read & write try { FileStream aFile = new FileStream("Log.txt", FileMode.OpenOrCreate); StreamWriter sw = new StreamWriter(aFile); bool truth = true // Write data to file. sw.WriteLine("Hello to you."); sw.WriteLine("It is now {0} and things are looking good.“,DateTime.Now.ToLongDateString());
  • 4. sw.Write("More than that,"); sw.Write(" it’s {0} that C# is fun.", truth); sw.Close(); } catch (IOException ee) { Console.WriteLine("An IO exception has been thrown!"); Console.WriteLine(ee.ToString()); Console.ReadLine(); return; }
  • 5. • Go to the application directory and find the Log.txt file. It is located in the binDebug folder because you used a relative path. • The WriteLine() method writes the string passed to it, followed immediately by a newline character. • You can see in the example that this causes the next write operation to begin on a new line. • The Write() method simply writes the string passed to it to the file, without a newline character appended. • Two sentence is in one line.
  • 6. The StreamReader Object • try • { • FileStream aFile = new FileStream("Log.txt", FileMode.Open); • StreamReader sr = new StreamReader(aFile); • line = sr.ReadLine(); • // Read data in line by line. • while(line != null) • { • Console.WriteLine(line); • line = sr.ReadLine(); • } • sr.Close(); • }
  • 7. • catch(IOException e) • { • Console.WriteLine("An IO exception has been thrown!"); • Console.WriteLine(e.ToString()); • return; • } • Console.ReadKey();  ReadLine() method to read text from the file.  This method reads text until a new line is found,and returns the resulting text as a string.  The method returns a null when the end of the file has been reached, which you use to test for the end of the file.
  • 8.  Note that you use a while loop, which ensures that the line read isn’t null before any code in the body of the loop is executed. Simple File Handling Program String line,k=""; StreamReader sr = new StreamReader("E:trainingreadsample.txt"); StreamWriter sw = new StreamWriter("E:trainingWritesample.txt"); line = sr.ReadLine();
  • 9. while (line != null) { k += line; line = sr.ReadLine(); } sw.WriteLine(k); sw.Close(); sr.Dispose(); Direct using streamReader & streamWriter
  • 10. Input : readsample.txt Contain • welcome TO .NET WORLD. • Asp.Net IS Microsoft Own Product. • File handling is done by Streamreader & streamwriter • Simple program about File. • OutPut: • Writesample.txt Contain • welcome TO .NET WORLD.Asp.Net IS Microsoft Own Product.File handling is done by Streamreader & streamwriter .Simple program about File.
  • 11. Tasks(to be done in the class session): • 1)Print first 5 characters from a file in a single line. • 2)Print number of each line. • 3)Create two files where 1st file will contain 1st &3rd lines and the other file will contain 2nd &4th lines.