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
 

Recently uploaded (20)

Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
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
 
"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 ...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
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
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
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
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
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
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 

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.