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

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
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
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Recently uploaded (20)

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
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
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
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
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, ...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
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
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
"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 ...
 

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.