SlideShare a Scribd company logo
Text FilesText Files
Reading andWritingText FilesReading andWritingText Files
Svetlin NakovSvetlin Nakov
Telerik CorporationTelerik Corporation
www.telerik.comwww.telerik.com
Table of ContentsTable of Contents
1.1. What is Stream?What is Stream?
Stream BasicsStream Basics
1.1. Reading Text FilesReading Text Files
TheThe StreamReaderStreamReader ClassClass
1.1. Writing Text FilesWriting Text Files
TheThe StreamStreamWritWriterer ClassClass
1.1. Handling I/O ExceptionsHandling I/O Exceptions
What Is Stream?What Is Stream?
Streams Basic ConceptsStreams Basic Concepts
What is Stream?What is Stream?
 Stream is the natural way to transfer data inStream is the natural way to transfer data in
the computer worldthe computer world
 To read or write a file, we open a streamTo read or write a file, we open a stream
connected to the file and access the dataconnected to the file and access the data
through the streamthrough the stream
Input streamInput stream
Output streamOutput stream
Streams BasicsStreams Basics
 Streams are used for reading and writing dataStreams are used for reading and writing data
into and from devicesinto and from devices
 Streams areStreams are ordered sequences of bytesordered sequences of bytes
Provide consecutive access to its elementsProvide consecutive access to its elements
 Different types of streams are available toDifferent types of streams are available to
access different data sources:access different data sources:
File access, network access, memory streamsFile access, network access, memory streams
and othersand others
 Streams are open before using them andStreams are open before using them and
closed after thatclosed after that
ReadingText FilesReadingText Files
Using theUsing the StreamReaderStreamReader ClassClass
TheThe StreamReaderStreamReader ClassClass
 System.IO.StreamReaderSystem.IO.StreamReader
The easiest way to read a text fileThe easiest way to read a text file
Implements methods for reading text lines andImplements methods for reading text lines and
sequences of characterssequences of characters
Constructed by file name or other streamConstructed by file name or other stream
 Can specify the text encoding (for Cyrillic useCan specify the text encoding (for Cyrillic use
windows-1251windows-1251))
Works likeWorks like Console.Read()Console.Read() // ReadLine()ReadLine() butbut
over text filesover text files
StreamReaderStreamReader MethodsMethods
 new StreamReader(fileName)new StreamReader(fileName)
 Constructor for creating reader from given fileConstructor for creating reader from given file
 ReadLine()ReadLine()
Reads a single text line from the streamReads a single text line from the stream
ReturnsReturns nullnull when end-of-file is reachedwhen end-of-file is reached
 ReadToEnd()ReadToEnd()
Reads all the text until the end of the streamReads all the text until the end of the stream
 Close()Close()
Closes the stream readerCloses the stream reader
 Reading a text file and printing its content toReading a text file and printing its content to
the console:the console:
 Specifying the text encoding:Specifying the text encoding:
Reading a Text FileReading a Text File
StreamReader reader = new StreamReader("test.txt");StreamReader reader = new StreamReader("test.txt");
string fileContents = streamReader.ReadToEnd();string fileContents = streamReader.ReadToEnd();
Console.WriteLine(fileContents);Console.WriteLine(fileContents);
streamReader.Close();streamReader.Close();
StreamReader reader = new StreamReader(StreamReader reader = new StreamReader(
"cyr.txt", Encoding.GetEncoding(""cyr.txt", Encoding.GetEncoding("windows-1251windows-1251"));"));
// Read the file contents here ...// Read the file contents here ...
reader.Close();reader.Close();
UsingUsing StreamReaderStreamReader – Practices– Practices
 TheThe StreamReaderStreamReader instances should alwaysinstances should always
be closed by calling thebe closed by calling the Close()Close() methodmethod
Otherwise system resources can be lostOtherwise system resources can be lost
 In C# the preferable way to close streams andIn C# the preferable way to close streams and
readers is by the "readers is by the "usingusing" construction" construction
It automatically calls theIt automatically calls the Close()Close()afterafter
thethe usingusing construction is completedconstruction is completed
using (<stream object>)using (<stream object>)
{{
// Use the stream here. It will be closed at the end// Use the stream here. It will be closed at the end
}}
Reading a Text File – ExampleReading a Text File – Example
 Read and display a text file line by line:Read and display a text file line by line:
StreamReader reader =StreamReader reader =
new StreamReader("somefile.txt");new StreamReader("somefile.txt");
using (reader)using (reader)
{{
int lineNumber = 0;int lineNumber = 0;
string line = reader.ReadLine();string line = reader.ReadLine();
while (line != null)while (line != null)
{{
lineNumber++;lineNumber++;
Console.WriteLine("Line {0}: {1}",Console.WriteLine("Line {0}: {1}",
lineNumber, line);lineNumber, line);
line = reader.ReadLine();line = reader.ReadLine();
}}
}}
ReadingText FilesReadingText Files
Live DemoLive Demo
WritingText FilesWritingText Files
Using theUsing the StreamStreamWriterWriter ClassClass
TheThe StreamWriterStreamWriter ClassClass
 System.IO.StreamWriterSystem.IO.StreamWriter
Similar toSimilar to StringReaderStringReader, but instead of, but instead of
reading, it provides writing functionalityreading, it provides writing functionality
 Constructed by file name or other streamConstructed by file name or other stream
Can define encodingCan define encoding
For Cyrillic use "For Cyrillic use "windows-1251windows-1251""
StreamWriter streamWriter = new StreamWriter("test.txt",StreamWriter streamWriter = new StreamWriter("test.txt",
false, Encoding.GetEncoding("windows-1251"));false, Encoding.GetEncoding("windows-1251"));
StreamWriter streamWriter = new StreamWriter("test.txt");StreamWriter streamWriter = new StreamWriter("test.txt");
StreamWriterStreamWriter MethodsMethods
 Write()Write()
Writes string or other object to the streamWrites string or other object to the stream
LikeLike Console.WriteConsole.Write()()
 WriteLineWriteLine()()
LikeLike Console.WriteConsole.WriteLine()Line()
 AutoFlushAutoFlush
Indicates whether to flush the internal bufferIndicates whether to flush the internal buffer
after each writingafter each writing
Writing to a Text File –Writing to a Text File –
ExampleExample
 Create text file named "Create text file named "numbers.txtnumbers.txt" and print" and print
in it the numbers from 1 to 20 (one per line):in it the numbers from 1 to 20 (one per line):
StreamWriter streamWriter =StreamWriter streamWriter =
new StreamWriter("numbers.txt");new StreamWriter("numbers.txt");
using (streamWriter)using (streamWriter)
{{
for (int number = 1; number <= 20; number++)for (int number = 1; number <= 20; number++)
{{
streamWriter.WriteLine(number);streamWriter.WriteLine(number);
}}
}}
WritingText FilesWritingText Files
Live DemoLive Demo
Handling I/O ExceptionsHandling I/O Exceptions
IntroductionIntroduction
What is Exception?What is Exception?
 "An event that occurs during the execution of the"An event that occurs during the execution of the
program that disrupts the normal flow ofprogram that disrupts the normal flow of
instructions“ – definition by Googleinstructions“ – definition by Google
 Occurs when an operation can not be completedOccurs when an operation can not be completed
 Exceptions tell that something unusual wasExceptions tell that something unusual was
happened, e. g. error or unexpected eventhappened, e. g. error or unexpected event
 I/O operations throw exceptions when operationI/O operations throw exceptions when operation
cannot be performed (e.g. missing file)cannot be performed (e.g. missing file)
 When an exception is thrown, all operations after itWhen an exception is thrown, all operations after it
are not processedare not processed
How to Handle Exceptions?How to Handle Exceptions?
 UsingUsing try{}try{},, catch{}catch{} andand finally{}finally{} blocks:blocks:
trytry
{{
// Some exception is thrown here// Some exception is thrown here
}}
catch (<exception type>)catch (<exception type>)
{{
// Exception is handled here// Exception is handled here
}}
finallyfinally
{{
// The code here is always executed, no// The code here is always executed, no
// matter if an exception has occurred or not// matter if an exception has occurred or not
}}
Catching ExceptionsCatching Exceptions
 Catch block specifies the type of exceptionsCatch block specifies the type of exceptions
that is caughtthat is caught
IfIf catchcatch doesn’t specify its type, it catches alldoesn’t specify its type, it catches all
types of exceptionstypes of exceptions
trytry
{{
StreamReader reader = new StreamReader("somefile.txt");StreamReader reader = new StreamReader("somefile.txt");
Console.WriteLine("File successfully open.");Console.WriteLine("File successfully open.");
}}
catch (FileNotFoundException)catch (FileNotFoundException)
{{
Console.Error.WriteLine("Can not find 'somefile.txt'.");Console.Error.WriteLine("Can not find 'somefile.txt'.");
}}
Handling ExceptionsHandling Exceptions
When Opening a FileWhen Opening a File
trytry
{{
StreamReader streamReader = new StreamReader(StreamReader streamReader = new StreamReader(
"c:NotExistingFileName.txt");"c:NotExistingFileName.txt");
}}
catch (System.NullReferenceException exc)catch (System.NullReferenceException exc)
{{
Console.WriteLine(exc.Message);Console.WriteLine(exc.Message);
}}
catch (System.IO.FileNotFoundException exc)catch (System.IO.FileNotFoundException exc)
{{
Console.WriteLine(Console.WriteLine(
"File {0} is not found!", exc.FileName);"File {0} is not found!", exc.FileName);
}}
catchcatch
{{
Console.WriteLine("Fatal error occurred.");Console.WriteLine("Fatal error occurred.");
}}
Handling I/OHandling I/O
ExceptionsExceptions
Live DemoLive Demo
Reading andReading and
WritingText FilesWritingText Files
More ExamplesMore Examples
Counting WordCounting Word
Occurrences – ExampleOccurrences – Example
 Counting the number of occurrences of theCounting the number of occurrences of the
word "word "foundmefoundme" in a text file:" in a text file:
StreamReader streamReader =StreamReader streamReader =
new StreamReader(@"....somefile.txt");new StreamReader(@"....somefile.txt");
int count = 0;int count = 0;
string text = streamReader.ReadToEnd();string text = streamReader.ReadToEnd();
int index = text.IndexOf("foundme", 0);int index = text.IndexOf("foundme", 0);
while (index != -1)while (index != -1)
{{
count++;count++;
index = text.IndexOf("foundme", index + 1);index = text.IndexOf("foundme", index + 1);
}}
Console.WriteLine(count);Console.WriteLine(count);
What isWhat is
missing in thismissing in this
code?code?
Counting Word OccurrencesCounting Word Occurrences
Live DemoLive Demo
Reading Subtitles – ExampleReading Subtitles – Example
..........
{2757}{2803} Allen, Bomb Squad, Special Services...{2757}{2803} Allen, Bomb Squad, Special Services...
{2804}{2874} State Police and the FBI!{2804}{2874} State Police and the FBI!
{2875}{2963} Lieutenant! I want you to go to St. John's{2875}{2963} Lieutenant! I want you to go to St. John's
Emergency...Emergency...
{2964}{3037} in case we got any walk-ins from the street.{2964}{3037} in case we got any walk-ins from the street.
{3038}{3094} Kramer, get the city engineer!{3038}{3094} Kramer, get the city engineer!
{3095}{3142} I gotta find out a damage report. It's very{3095}{3142} I gotta find out a damage report. It's very
important.important.
{3171}{3219} Who the hell would want to blow up a department{3171}{3219} Who the hell would want to blow up a department
store?store?
..........
 We are given a standard movie subtitles file:We are given a standard movie subtitles file:
Fixing Subtitles – ExampleFixing Subtitles – Example
 Read subtitles file and fix it’s timing:Read subtitles file and fix it’s timing:
static void Main()static void Main()
{{
trytry
{{
// Obtaining the Cyrillic encoding// Obtaining the Cyrillic encoding
System.Text.Encoding encodingCyr =System.Text.Encoding encodingCyr =
System.Text.Encoding.GetEncoding(1251);System.Text.Encoding.GetEncoding(1251);
// Create reader with the Cyrillic encoding// Create reader with the Cyrillic encoding
StreamReader streamReader =StreamReader streamReader =
new StreamReader("source.sub", encodingCyr);new StreamReader("source.sub", encodingCyr);
// Create writer with the Cyrillic encoding// Create writer with the Cyrillic encoding
StreamWriter streamWriter =StreamWriter streamWriter =
new StreamWriter("fixed.sub",new StreamWriter("fixed.sub",
false, encodingCyr);false, encodingCyr);
(example continues)(example continues)
Fixing Subtitles – ExampleFixing Subtitles – Example
trytry
{{
string line;string line;
while (while (
(line = streamReader.ReadLine()) != null)(line = streamReader.ReadLine()) != null)
{{
streamWriter.WriteLine(FixLine(line));streamWriter.WriteLine(FixLine(line));
}}
}}
finallyfinally
{{
streamReader.Close();streamReader.Close();
streamWriter.Close();streamWriter.Close();
}}
}}
catch (System.Exception exc)catch (System.Exception exc)
{{
Console.WriteLine(exc.Message);Console.WriteLine(exc.Message);
}}
}}
FixLine(line)FixLine(line)
perform fixes on theperform fixes on the
time offsets:time offsets:
multiplication or/andmultiplication or/and
addition with constantaddition with constant
Fixing Movie SubtitlesFixing Movie Subtitles
Live DemoLive Demo
SummarySummary
 Streams are the main I/O mechanismsStreams are the main I/O mechanisms
in .NETin .NET
 TheThe StreamReaderStreamReader class andclass and ReadLine()ReadLine()
method are used to read text filesmethod are used to read text files
 TheThe StreamWriterStreamWriter class andclass and WriteLine()WriteLine()
method are used to write text filesmethod are used to write text files
 Exceptions are unusual events or errorExceptions are unusual events or error
conditionsconditions
Can be handled byCan be handled by try-catch-finallytry-catch-finally blocksblocks
Text FilesText Files
Questions?Questions?
http://academy.telerik.com
ExercisesExercises
1.1. Write a program that reads a text file and prints onWrite a program that reads a text file and prints on
the console its odd lines.the console its odd lines.
2.2. Write a program that concatenates two text filesWrite a program that concatenates two text files
into another text file.into another text file.
3.3. Write a program that reads a text file and inserts lineWrite a program that reads a text file and inserts line
numbers in front of each of its lines. The resultnumbers in front of each of its lines. The result
should be written to another text file.should be written to another text file.
4.4. Write a program that compares two text files line byWrite a program that compares two text files line by
line and prints the number of lines that are the sameline and prints the number of lines that are the same
and the number of lines that are different. Assumeand the number of lines that are different. Assume
the files have equal number of lines.the files have equal number of lines.
Exercises (2)Exercises (2)
5.5. Write a program that reads a text file containing aWrite a program that reads a text file containing a
square matrix of numbers and finds in the matrix ansquare matrix of numbers and finds in the matrix an
area of size 2 x 2 with a maximal sum of its elements.area of size 2 x 2 with a maximal sum of its elements.
The first line in the input file contains the size ofThe first line in the input file contains the size of
matrix N. Each of the next N lines contain Nmatrix N. Each of the next N lines contain N
numbers separated by space. The output should be anumbers separated by space. The output should be a
single number in a separate text file. Example:single number in a separate text file. Example:
44
2 3 3 42 3 3 4
0 2 3 40 2 3 4 1717
3 73 7 1 21 2
4 34 3 3 23 2
Exercises (3)Exercises (3)
6.6. Write a program that reads a text file containing aWrite a program that reads a text file containing a
list of strings, sorts them and saves them to anotherlist of strings, sorts them and saves them to another
text file. Example:text file. Example:
IvanIvan GeorgeGeorge
PeterPeter IvanIvan
MariaMaria MariaMaria
GeorgeGeorge PeterPeter
6.6. Write a program that replaces all occurrences of theWrite a program that replaces all occurrences of the
substring "start" with the substring "finish" in a textsubstring "start" with the substring "finish" in a text
file. Ensure it will work with large files (e.g. 100 MB).file. Ensure it will work with large files (e.g. 100 MB).
7.7. Modify the solution of the previous problem toModify the solution of the previous problem to
replace only whole words (not substrings).replace only whole words (not substrings).
Exercises (4)Exercises (4)
9.9. Write a program that deletes from given text file allWrite a program that deletes from given text file all
odd lines. The result should be in the same file.odd lines. The result should be in the same file.
10.10. Write a program that extracts from given XML fileWrite a program that extracts from given XML file
all the text without the tags. Example:all the text without the tags. Example:
11.11. Write a program that deletes from a text file allWrite a program that deletes from a text file all
words that start with the prefix "test". Wordswords that start with the prefix "test". Words
contain only the symbols 0...9, a...z, A…Z, _.contain only the symbols 0...9, a...z, A…Z, _.
<?xml version="1.0"><student><name>Pesho</name>
<age>21</age><interests count="3"><interest>
Games</instrest><interest>C#</instrest><interest>
Java</instrest></interests></student>
Exercises (5)Exercises (5)
12.12. Write a program that removes from a text file allWrite a program that removes from a text file all
words listed in given another text file. Handle allwords listed in given another text file. Handle all
possible exceptions in your methods.possible exceptions in your methods.
13.13. Write a program that reads a list of words from a fileWrite a program that reads a list of words from a file
words.txtwords.txt and finds how many times each of theand finds how many times each of the
words is contained in another filewords is contained in another file test.txttest.txt. The. The
result should be written in the fileresult should be written in the file result.txtresult.txt andand
the words should be sorted by the number of theirthe words should be sorted by the number of their
occurrences in descending order. Handle all possibleoccurrences in descending order. Handle all possible
exceptions in your methods.exceptions in your methods.

More Related Content

PPTX
15. text files
PPT
7 streams and error handling in java
PPTX
Java file
PPS
Files & IO in Java
PDF
Java IO
PDF
PPT
Java Input Output and File Handling
PDF
Java I/o streams
15. text files
7 streams and error handling in java
Java file
Files & IO in Java
Java IO
Java Input Output and File Handling
Java I/o streams

What's hot (20)

PPT
Chapter 12 - File Input and Output
PPTX
Handling I/O in Java
PPTX
L21 io streams
PPT
Java stream
PDF
Java Course 8: I/O, Files and Streams
PPTX
Understanding java streams
PDF
Ppl for students unit 4 and 5
PDF
Java Programming - 06 java file io
PPTX
Java I/O
PDF
Java - File Input Output Concepts
ODP
IO In Java
PPTX
PPT
File Input & Output
PPTX
Java Input Output (java.io.*)
PPTX
PPSX
PDF
Java 8 - Stamped Lock
PPT
Jedi Slides Intro2 Chapter12 Advanced Io Streams
PPTX
.NET Multithreading/Multitasking
PPT
Java Streams
Chapter 12 - File Input and Output
Handling I/O in Java
L21 io streams
Java stream
Java Course 8: I/O, Files and Streams
Understanding java streams
Ppl for students unit 4 and 5
Java Programming - 06 java file io
Java I/O
Java - File Input Output Concepts
IO In Java
File Input & Output
Java Input Output (java.io.*)
Java 8 - Stamped Lock
Jedi Slides Intro2 Chapter12 Advanced Io Streams
.NET Multithreading/Multitasking
Java Streams
Ad

Viewers also liked (7)

PPT
03 Operators and expressions
PPT
05 Conditional statements
PPT
19 Algorithms and complexity
PPT
01 Introduction to programming
PPT
For Beginners - C#
PPTX
C# overview part 1
PDF
Responding to Academically Distressed Students
03 Operators and expressions
05 Conditional statements
19 Algorithms and complexity
01 Introduction to programming
For Beginners - C#
C# overview part 1
Responding to Academically Distressed Students
Ad

Similar to 15 Text files (20)

PPT
Basic input-output-v.1.1
PDF
Ppl for students unit 4 and 5
PDF
IOstreams hgjhsgfdjyggckhgckhjxfhbuvobunciu
PDF
Java sockets
PDF
File Handling in Java.pdf
PPTX
Introduction to files management systems
PDF
11_Str11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.eams.pdf
PPTX
File Input and output.pptx
PDF
CSE3146-ADV JAVA M2.pdf
PPTX
Input output files in java
PPTX
IOStream.pptx
PPT
Java development development Files lectur6.ppt
PDF
Java IO Stream, the introduction to Streams
PDF
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
PDF
Basic i/o & file handling in java
PPT
File handling in_c
PPT
Filehandlinging cp2
PPT
Itp 120 Chapt 19 2009 Binary Input & Output
PPT
ASP.NET Session 8
PDF
Files in C++.pdf is the notes of cpp for reference
Basic input-output-v.1.1
Ppl for students unit 4 and 5
IOstreams hgjhsgfdjyggckhgckhjxfhbuvobunciu
Java sockets
File Handling in Java.pdf
Introduction to files management systems
11_Str11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.eams.pdf
File Input and output.pptx
CSE3146-ADV JAVA M2.pdf
Input output files in java
IOStream.pptx
Java development development Files lectur6.ppt
Java IO Stream, the introduction to Streams
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
Basic i/o & file handling in java
File handling in_c
Filehandlinging cp2
Itp 120 Chapt 19 2009 Binary Input & Output
ASP.NET Session 8
Files in C++.pdf is the notes of cpp for reference

More from maznabili (19)

PPT
22 Methodology of problem solving
PPT
21 High-quality programming code construction part-ii
PPT
21 high-quality programming code construction part-i
PPT
20 Object-oriented programming principles
PPT
18 Hash tables and sets
PPT
17 Trees and graphs
PPT
16 Linear data structures
PPT
14 Defining classes
PPT
13 Strings and text processing
PPT
12 Exceptions handling
PPT
11 Using classes and objects
PPT
10 Recursion
PPT
09 Methods
PPT
08 Numeral systems
PPT
07 Arrays
PPT
06 Loops
PPT
04 Console input output-
PPT
02 Primitive data types and variables
PPT
00 Fundamentals of csharp course introduction
22 Methodology of problem solving
21 High-quality programming code construction part-ii
21 high-quality programming code construction part-i
20 Object-oriented programming principles
18 Hash tables and sets
17 Trees and graphs
16 Linear data structures
14 Defining classes
13 Strings and text processing
12 Exceptions handling
11 Using classes and objects
10 Recursion
09 Methods
08 Numeral systems
07 Arrays
06 Loops
04 Console input output-
02 Primitive data types and variables
00 Fundamentals of csharp course introduction

Recently uploaded (20)

PDF
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
PDF
Zenith AI: Advanced Artificial Intelligence
PPTX
observCloud-Native Containerability and monitoring.pptx
PDF
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Architecture types and enterprise applications.pdf
PDF
Web App vs Mobile App What Should You Build First.pdf
PDF
Developing a website for English-speaking practice to English as a foreign la...
PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PPTX
Tartificialntelligence_presentation.pptx
PPTX
TLE Review Electricity (Electricity).pptx
PDF
WOOl fibre morphology and structure.pdf for textiles
PDF
Getting Started with Data Integration: FME Form 101
PPTX
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
PPTX
Final SEM Unit 1 for mit wpu at pune .pptx
PPTX
O2C Customer Invoices to Receipt V15A.pptx
PDF
NewMind AI Weekly Chronicles - August'25-Week II
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
Zenith AI: Advanced Artificial Intelligence
observCloud-Native Containerability and monitoring.pptx
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Assigned Numbers - 2025 - Bluetooth® Document
Architecture types and enterprise applications.pdf
Web App vs Mobile App What Should You Build First.pdf
Developing a website for English-speaking practice to English as a foreign la...
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
Univ-Connecticut-ChatGPT-Presentaion.pdf
Tartificialntelligence_presentation.pptx
TLE Review Electricity (Electricity).pptx
WOOl fibre morphology and structure.pdf for textiles
Getting Started with Data Integration: FME Form 101
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
Final SEM Unit 1 for mit wpu at pune .pptx
O2C Customer Invoices to Receipt V15A.pptx
NewMind AI Weekly Chronicles - August'25-Week II

15 Text files

  • 1. Text FilesText Files Reading andWritingText FilesReading andWritingText Files Svetlin NakovSvetlin Nakov Telerik CorporationTelerik Corporation www.telerik.comwww.telerik.com
  • 2. Table of ContentsTable of Contents 1.1. What is Stream?What is Stream? Stream BasicsStream Basics 1.1. Reading Text FilesReading Text Files TheThe StreamReaderStreamReader ClassClass 1.1. Writing Text FilesWriting Text Files TheThe StreamStreamWritWriterer ClassClass 1.1. Handling I/O ExceptionsHandling I/O Exceptions
  • 3. What Is Stream?What Is Stream? Streams Basic ConceptsStreams Basic Concepts
  • 4. What is Stream?What is Stream?  Stream is the natural way to transfer data inStream is the natural way to transfer data in the computer worldthe computer world  To read or write a file, we open a streamTo read or write a file, we open a stream connected to the file and access the dataconnected to the file and access the data through the streamthrough the stream Input streamInput stream Output streamOutput stream
  • 5. Streams BasicsStreams Basics  Streams are used for reading and writing dataStreams are used for reading and writing data into and from devicesinto and from devices  Streams areStreams are ordered sequences of bytesordered sequences of bytes Provide consecutive access to its elementsProvide consecutive access to its elements  Different types of streams are available toDifferent types of streams are available to access different data sources:access different data sources: File access, network access, memory streamsFile access, network access, memory streams and othersand others  Streams are open before using them andStreams are open before using them and closed after thatclosed after that
  • 6. ReadingText FilesReadingText Files Using theUsing the StreamReaderStreamReader ClassClass
  • 7. TheThe StreamReaderStreamReader ClassClass  System.IO.StreamReaderSystem.IO.StreamReader The easiest way to read a text fileThe easiest way to read a text file Implements methods for reading text lines andImplements methods for reading text lines and sequences of characterssequences of characters Constructed by file name or other streamConstructed by file name or other stream  Can specify the text encoding (for Cyrillic useCan specify the text encoding (for Cyrillic use windows-1251windows-1251)) Works likeWorks like Console.Read()Console.Read() // ReadLine()ReadLine() butbut over text filesover text files
  • 8. StreamReaderStreamReader MethodsMethods  new StreamReader(fileName)new StreamReader(fileName)  Constructor for creating reader from given fileConstructor for creating reader from given file  ReadLine()ReadLine() Reads a single text line from the streamReads a single text line from the stream ReturnsReturns nullnull when end-of-file is reachedwhen end-of-file is reached  ReadToEnd()ReadToEnd() Reads all the text until the end of the streamReads all the text until the end of the stream  Close()Close() Closes the stream readerCloses the stream reader
  • 9.  Reading a text file and printing its content toReading a text file and printing its content to the console:the console:  Specifying the text encoding:Specifying the text encoding: Reading a Text FileReading a Text File StreamReader reader = new StreamReader("test.txt");StreamReader reader = new StreamReader("test.txt"); string fileContents = streamReader.ReadToEnd();string fileContents = streamReader.ReadToEnd(); Console.WriteLine(fileContents);Console.WriteLine(fileContents); streamReader.Close();streamReader.Close(); StreamReader reader = new StreamReader(StreamReader reader = new StreamReader( "cyr.txt", Encoding.GetEncoding(""cyr.txt", Encoding.GetEncoding("windows-1251windows-1251"));")); // Read the file contents here ...// Read the file contents here ... reader.Close();reader.Close();
  • 10. UsingUsing StreamReaderStreamReader – Practices– Practices  TheThe StreamReaderStreamReader instances should alwaysinstances should always be closed by calling thebe closed by calling the Close()Close() methodmethod Otherwise system resources can be lostOtherwise system resources can be lost  In C# the preferable way to close streams andIn C# the preferable way to close streams and readers is by the "readers is by the "usingusing" construction" construction It automatically calls theIt automatically calls the Close()Close()afterafter thethe usingusing construction is completedconstruction is completed using (<stream object>)using (<stream object>) {{ // Use the stream here. It will be closed at the end// Use the stream here. It will be closed at the end }}
  • 11. Reading a Text File – ExampleReading a Text File – Example  Read and display a text file line by line:Read and display a text file line by line: StreamReader reader =StreamReader reader = new StreamReader("somefile.txt");new StreamReader("somefile.txt"); using (reader)using (reader) {{ int lineNumber = 0;int lineNumber = 0; string line = reader.ReadLine();string line = reader.ReadLine(); while (line != null)while (line != null) {{ lineNumber++;lineNumber++; Console.WriteLine("Line {0}: {1}",Console.WriteLine("Line {0}: {1}", lineNumber, line);lineNumber, line); line = reader.ReadLine();line = reader.ReadLine(); }} }}
  • 13. WritingText FilesWritingText Files Using theUsing the StreamStreamWriterWriter ClassClass
  • 14. TheThe StreamWriterStreamWriter ClassClass  System.IO.StreamWriterSystem.IO.StreamWriter Similar toSimilar to StringReaderStringReader, but instead of, but instead of reading, it provides writing functionalityreading, it provides writing functionality  Constructed by file name or other streamConstructed by file name or other stream Can define encodingCan define encoding For Cyrillic use "For Cyrillic use "windows-1251windows-1251"" StreamWriter streamWriter = new StreamWriter("test.txt",StreamWriter streamWriter = new StreamWriter("test.txt", false, Encoding.GetEncoding("windows-1251"));false, Encoding.GetEncoding("windows-1251")); StreamWriter streamWriter = new StreamWriter("test.txt");StreamWriter streamWriter = new StreamWriter("test.txt");
  • 15. StreamWriterStreamWriter MethodsMethods  Write()Write() Writes string or other object to the streamWrites string or other object to the stream LikeLike Console.WriteConsole.Write()()  WriteLineWriteLine()() LikeLike Console.WriteConsole.WriteLine()Line()  AutoFlushAutoFlush Indicates whether to flush the internal bufferIndicates whether to flush the internal buffer after each writingafter each writing
  • 16. Writing to a Text File –Writing to a Text File – ExampleExample  Create text file named "Create text file named "numbers.txtnumbers.txt" and print" and print in it the numbers from 1 to 20 (one per line):in it the numbers from 1 to 20 (one per line): StreamWriter streamWriter =StreamWriter streamWriter = new StreamWriter("numbers.txt");new StreamWriter("numbers.txt"); using (streamWriter)using (streamWriter) {{ for (int number = 1; number <= 20; number++)for (int number = 1; number <= 20; number++) {{ streamWriter.WriteLine(number);streamWriter.WriteLine(number); }} }}
  • 18. Handling I/O ExceptionsHandling I/O Exceptions IntroductionIntroduction
  • 19. What is Exception?What is Exception?  "An event that occurs during the execution of the"An event that occurs during the execution of the program that disrupts the normal flow ofprogram that disrupts the normal flow of instructions“ – definition by Googleinstructions“ – definition by Google  Occurs when an operation can not be completedOccurs when an operation can not be completed  Exceptions tell that something unusual wasExceptions tell that something unusual was happened, e. g. error or unexpected eventhappened, e. g. error or unexpected event  I/O operations throw exceptions when operationI/O operations throw exceptions when operation cannot be performed (e.g. missing file)cannot be performed (e.g. missing file)  When an exception is thrown, all operations after itWhen an exception is thrown, all operations after it are not processedare not processed
  • 20. How to Handle Exceptions?How to Handle Exceptions?  UsingUsing try{}try{},, catch{}catch{} andand finally{}finally{} blocks:blocks: trytry {{ // Some exception is thrown here// Some exception is thrown here }} catch (<exception type>)catch (<exception type>) {{ // Exception is handled here// Exception is handled here }} finallyfinally {{ // The code here is always executed, no// The code here is always executed, no // matter if an exception has occurred or not// matter if an exception has occurred or not }}
  • 21. Catching ExceptionsCatching Exceptions  Catch block specifies the type of exceptionsCatch block specifies the type of exceptions that is caughtthat is caught IfIf catchcatch doesn’t specify its type, it catches alldoesn’t specify its type, it catches all types of exceptionstypes of exceptions trytry {{ StreamReader reader = new StreamReader("somefile.txt");StreamReader reader = new StreamReader("somefile.txt"); Console.WriteLine("File successfully open.");Console.WriteLine("File successfully open."); }} catch (FileNotFoundException)catch (FileNotFoundException) {{ Console.Error.WriteLine("Can not find 'somefile.txt'.");Console.Error.WriteLine("Can not find 'somefile.txt'."); }}
  • 22. Handling ExceptionsHandling Exceptions When Opening a FileWhen Opening a File trytry {{ StreamReader streamReader = new StreamReader(StreamReader streamReader = new StreamReader( "c:NotExistingFileName.txt");"c:NotExistingFileName.txt"); }} catch (System.NullReferenceException exc)catch (System.NullReferenceException exc) {{ Console.WriteLine(exc.Message);Console.WriteLine(exc.Message); }} catch (System.IO.FileNotFoundException exc)catch (System.IO.FileNotFoundException exc) {{ Console.WriteLine(Console.WriteLine( "File {0} is not found!", exc.FileName);"File {0} is not found!", exc.FileName); }} catchcatch {{ Console.WriteLine("Fatal error occurred.");Console.WriteLine("Fatal error occurred."); }}
  • 24. Reading andReading and WritingText FilesWritingText Files More ExamplesMore Examples
  • 25. Counting WordCounting Word Occurrences – ExampleOccurrences – Example  Counting the number of occurrences of theCounting the number of occurrences of the word "word "foundmefoundme" in a text file:" in a text file: StreamReader streamReader =StreamReader streamReader = new StreamReader(@"....somefile.txt");new StreamReader(@"....somefile.txt"); int count = 0;int count = 0; string text = streamReader.ReadToEnd();string text = streamReader.ReadToEnd(); int index = text.IndexOf("foundme", 0);int index = text.IndexOf("foundme", 0); while (index != -1)while (index != -1) {{ count++;count++; index = text.IndexOf("foundme", index + 1);index = text.IndexOf("foundme", index + 1); }} Console.WriteLine(count);Console.WriteLine(count); What isWhat is missing in thismissing in this code?code?
  • 26. Counting Word OccurrencesCounting Word Occurrences Live DemoLive Demo
  • 27. Reading Subtitles – ExampleReading Subtitles – Example .......... {2757}{2803} Allen, Bomb Squad, Special Services...{2757}{2803} Allen, Bomb Squad, Special Services... {2804}{2874} State Police and the FBI!{2804}{2874} State Police and the FBI! {2875}{2963} Lieutenant! I want you to go to St. John's{2875}{2963} Lieutenant! I want you to go to St. John's Emergency...Emergency... {2964}{3037} in case we got any walk-ins from the street.{2964}{3037} in case we got any walk-ins from the street. {3038}{3094} Kramer, get the city engineer!{3038}{3094} Kramer, get the city engineer! {3095}{3142} I gotta find out a damage report. It's very{3095}{3142} I gotta find out a damage report. It's very important.important. {3171}{3219} Who the hell would want to blow up a department{3171}{3219} Who the hell would want to blow up a department store?store? ..........  We are given a standard movie subtitles file:We are given a standard movie subtitles file:
  • 28. Fixing Subtitles – ExampleFixing Subtitles – Example  Read subtitles file and fix it’s timing:Read subtitles file and fix it’s timing: static void Main()static void Main() {{ trytry {{ // Obtaining the Cyrillic encoding// Obtaining the Cyrillic encoding System.Text.Encoding encodingCyr =System.Text.Encoding encodingCyr = System.Text.Encoding.GetEncoding(1251);System.Text.Encoding.GetEncoding(1251); // Create reader with the Cyrillic encoding// Create reader with the Cyrillic encoding StreamReader streamReader =StreamReader streamReader = new StreamReader("source.sub", encodingCyr);new StreamReader("source.sub", encodingCyr); // Create writer with the Cyrillic encoding// Create writer with the Cyrillic encoding StreamWriter streamWriter =StreamWriter streamWriter = new StreamWriter("fixed.sub",new StreamWriter("fixed.sub", false, encodingCyr);false, encodingCyr); (example continues)(example continues)
  • 29. Fixing Subtitles – ExampleFixing Subtitles – Example trytry {{ string line;string line; while (while ( (line = streamReader.ReadLine()) != null)(line = streamReader.ReadLine()) != null) {{ streamWriter.WriteLine(FixLine(line));streamWriter.WriteLine(FixLine(line)); }} }} finallyfinally {{ streamReader.Close();streamReader.Close(); streamWriter.Close();streamWriter.Close(); }} }} catch (System.Exception exc)catch (System.Exception exc) {{ Console.WriteLine(exc.Message);Console.WriteLine(exc.Message); }} }} FixLine(line)FixLine(line) perform fixes on theperform fixes on the time offsets:time offsets: multiplication or/andmultiplication or/and addition with constantaddition with constant
  • 30. Fixing Movie SubtitlesFixing Movie Subtitles Live DemoLive Demo
  • 31. SummarySummary  Streams are the main I/O mechanismsStreams are the main I/O mechanisms in .NETin .NET  TheThe StreamReaderStreamReader class andclass and ReadLine()ReadLine() method are used to read text filesmethod are used to read text files  TheThe StreamWriterStreamWriter class andclass and WriteLine()WriteLine() method are used to write text filesmethod are used to write text files  Exceptions are unusual events or errorExceptions are unusual events or error conditionsconditions Can be handled byCan be handled by try-catch-finallytry-catch-finally blocksblocks
  • 33. ExercisesExercises 1.1. Write a program that reads a text file and prints onWrite a program that reads a text file and prints on the console its odd lines.the console its odd lines. 2.2. Write a program that concatenates two text filesWrite a program that concatenates two text files into another text file.into another text file. 3.3. Write a program that reads a text file and inserts lineWrite a program that reads a text file and inserts line numbers in front of each of its lines. The resultnumbers in front of each of its lines. The result should be written to another text file.should be written to another text file. 4.4. Write a program that compares two text files line byWrite a program that compares two text files line by line and prints the number of lines that are the sameline and prints the number of lines that are the same and the number of lines that are different. Assumeand the number of lines that are different. Assume the files have equal number of lines.the files have equal number of lines.
  • 34. Exercises (2)Exercises (2) 5.5. Write a program that reads a text file containing aWrite a program that reads a text file containing a square matrix of numbers and finds in the matrix ansquare matrix of numbers and finds in the matrix an area of size 2 x 2 with a maximal sum of its elements.area of size 2 x 2 with a maximal sum of its elements. The first line in the input file contains the size ofThe first line in the input file contains the size of matrix N. Each of the next N lines contain Nmatrix N. Each of the next N lines contain N numbers separated by space. The output should be anumbers separated by space. The output should be a single number in a separate text file. Example:single number in a separate text file. Example: 44 2 3 3 42 3 3 4 0 2 3 40 2 3 4 1717 3 73 7 1 21 2 4 34 3 3 23 2
  • 35. Exercises (3)Exercises (3) 6.6. Write a program that reads a text file containing aWrite a program that reads a text file containing a list of strings, sorts them and saves them to anotherlist of strings, sorts them and saves them to another text file. Example:text file. Example: IvanIvan GeorgeGeorge PeterPeter IvanIvan MariaMaria MariaMaria GeorgeGeorge PeterPeter 6.6. Write a program that replaces all occurrences of theWrite a program that replaces all occurrences of the substring "start" with the substring "finish" in a textsubstring "start" with the substring "finish" in a text file. Ensure it will work with large files (e.g. 100 MB).file. Ensure it will work with large files (e.g. 100 MB). 7.7. Modify the solution of the previous problem toModify the solution of the previous problem to replace only whole words (not substrings).replace only whole words (not substrings).
  • 36. Exercises (4)Exercises (4) 9.9. Write a program that deletes from given text file allWrite a program that deletes from given text file all odd lines. The result should be in the same file.odd lines. The result should be in the same file. 10.10. Write a program that extracts from given XML fileWrite a program that extracts from given XML file all the text without the tags. Example:all the text without the tags. Example: 11.11. Write a program that deletes from a text file allWrite a program that deletes from a text file all words that start with the prefix "test". Wordswords that start with the prefix "test". Words contain only the symbols 0...9, a...z, A…Z, _.contain only the symbols 0...9, a...z, A…Z, _. <?xml version="1.0"><student><name>Pesho</name> <age>21</age><interests count="3"><interest> Games</instrest><interest>C#</instrest><interest> Java</instrest></interests></student>
  • 37. Exercises (5)Exercises (5) 12.12. Write a program that removes from a text file allWrite a program that removes from a text file all words listed in given another text file. Handle allwords listed in given another text file. Handle all possible exceptions in your methods.possible exceptions in your methods. 13.13. Write a program that reads a list of words from a fileWrite a program that reads a list of words from a file words.txtwords.txt and finds how many times each of theand finds how many times each of the words is contained in another filewords is contained in another file test.txttest.txt. The. The result should be written in the fileresult should be written in the file result.txtresult.txt andand the words should be sorted by the number of theirthe words should be sorted by the number of their occurrences in descending order. Handle all possibleoccurrences in descending order. Handle all possible exceptions in your methods.exceptions in your methods.

Editor's Notes

  • #26: The missing code is the &amp;quot;using&amp;quot; block. The file should be properly closed.