SlideShare a Scribd company logo
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
File Input & Output 1
Q3M1
Dudy Fathan Ali, S.Kom (DFA)
2015
CEP - CCIT
Fakultas Teknik Universitas Indonesia
File Input & Output
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
o The stream is a sequence of bytes travelling from a source to a
destination over a communication path.
o The two basic streams used are the input and output streams.
o Input stream is used for a read operation.
o Output stream is used for performing a write operation.
o The System.IO namespace includes various classes, which are
used to perform operations, such as file creation, file deletion,
and the read-write operations to files.
File Input & Output
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
Common Class of System.IO Namespace
The following table describes some commonly used classes in the System.IO namespace.
File Input & Output
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
FileStream Class
Most file Input/Output (I/O) support in the .NET Framework is implemented in
the System.IO namespace. You can use the FileStream class in the
System.IO namespace to read from, to write, and to close files.
FileStream [ObjName] = new FileStream([FileName],
[FileMode], [FileAccess])
Code Structure:
FileStream fs = new FileStream(ā€œMyFile.txtā€,
FileMode.Open, FileAccess.Read);
Example:
File Input & Output
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
StreamReader and StreamWriter Class
The Stream class is used to read from and to write data in the text files. If data
of a file is only text, then you can use the StreamReader class and the
StreamWriter class to accomplish the reading and writing tasks
StreamReader sr = new StreamReader([ObjFileStream])
Code Structure:
FileStream fs = new FileStream(ā€œMyFile.txtā€,
FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
Example:
File Input & Output
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
Process of input data with FileStream and StreamWriter
File
Input.txt
FileStream StreamWriter
Write Data to
Stream
Mengosongkan
memori
Pembacaan data bisa
dari input user atau
dari cara yang lain
Data di simpan ke
dalam file Input.txt
Buka
StreamWriter
Buka
FileStream
Read Data
From User
Close SW &
FS
Tutup
StreamWriter dan
FileStream
Flush
File Input & Output
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
Process of input data with FileStream and StreamWriter
Consider the following code:
File Input & Output
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
Latihan Mandiri
Diketahui nama dosen sebagai berikut :
Masukkan lah nama-nama tersebut kedalam satu file dengan nama datadosen.txt
dengan menggunakan FileStream.
Catatan:
Nama dosen harus dimasukkan berdasarkan inputan user (bukan nilai statis dari
variable)
Dudy Fathan Ali S.Kom
Fachran Nazarullah S.Kom
Tri Agus Riyadi S.Kom
Riza Muhammad Nurman S.Kom
Musawarman S.Kom
File Input & Output
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
Process of reading data with FileStream and StreamReader
File
Output.txt
FileStream
Tampilkan
Data
Next Data
Y
N
Close SR
Close FS
Tutup
StreamReader
Tutup
FileStream
(b) Buka
StreamReader
(a) Buka
FileStream
(a)  Membuka File
(b)  Memuat Isi File ke dalam object
sr !=
null
StreamReader
File Input & Output
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
Process of reading data with FileStream and StreamWriter
Consider the following code:
File Input & Output
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
Process of search data with FileStream and StreamReader
File
Output.txt
FileStream StreamReader
sr !=
null
Y
N
Close SR
Close FS
Tutup
StreamReader
Tutup
FileStream
Buka StreamReader
Buka
FileReader
Data Search
Data yang di
cari
Variabel dataUntuk menampung
hasil pencarian data
Hasil cari
simpan ke
variabel data
Y
a
Cetak data
Apakah text yang di cari ada,
Jika iya maka akan di simpan
ke variabel data
Untuk pengecekan, anda bisa
menggunakan Contains
aNext Data
Y
File Input & Output
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
Process of search data with FileStream and StreamWriter
File Input & Output
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
Latihan Mandiri
Anda ditugaskan untuk mencari nama lengkap dari orang yang bernama ā€œFachranā€
dari file datadosen.txt yang berisi data sebagai berikut :
Tampilkan data yang dicari sehingga menghasilkan output sebagai berikut :
Catatan :
Parameter pencarian harus dari inputan user (bukan nilai statis dari variable)
Data yang dicari : Fachran Nazarullah S.Kom
Dudy Fathan Ali S.Kom
Fachran Nazarullah S.Kom
Tri Agus Riyadi S.Kom
Riza Muhammad Nurman S.Kom
Musawarman S.Kom
File Input & Output
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
Delimiter
o Delimiter = Pemisah
o Simbol bisa digunakan untuk delimiter (#, $, &, ~)
o Implementasinya menggunakan array
File Input & Output
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
Delimiter
o Penggunaan Delimiter :
o Contoh input data pelanggan:
o ID Pelanggan : 8934
o Nama Pelanggan : Joni Simanjuntak
o Jenis Kelamin : Pria
o Alamat : Jakarta
8934#Joni Simanjuntak#Pria#Jakarta
8934;Joni Simanjuntak;Pria;Jakarta
Memiliki delimiter
dengan simbol ā€˜#’
Memiliki delimiter
dengan simbol ā€˜;’
File Input & Output
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
Mengapa harus menggunakan delimiter?
Berikut data pelanggan
---------------------------------
ID Pelanggan : 8934
Nama Pelanggan : Joni Simanjuntak
Jenis Kelamin : Pria
Alamat : Jakarta
Terkadang developer membutuhkan tampilan seperti ini:
Dengan menggunakan
delimiter, developer dapat
lebih mudah memecah data
yang satu dengan yang lain.
8934#Joni Simanjuntak#Pria#Jakarta
array[0] = 8394 array[1] = Joni
Simanjuntak
array[2] = Pria array[3] =
Jakarta
File Input & Output
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
Process of input data with delimiter format.
Consider the following code:
File Input & Output
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
Process of reading data with delimiter format.
Consider the following code:
Q3M1 – OOP C# Dudy Fathan Ali S.Kom
Thank You!
Dudy Fathan Ali S.Kom
dudy.fathan@eng.ui.ac.id

More Related Content

PPTX
Estrutura de Dados em Java (FunƧƵes e Procedimentos)
Adriano Teixeira de Souza
Ā 
ODP
Linguagem C 09 Ponteiros
Regis Magalhães
Ā 
PDF
Binary tree
Rajendran
Ā 
PDF
Formato articulos ieee
Lucho Castagneto
Ā 
PPTX
Linguagem C - Estruturas
Elaine CecĆ­lia Gatto
Ā 
PDF
Desenho TƩcnico
Elton Magno
Ā 
PPT
1.8 splay tree
Krish_ver2
Ā 
PPTX
Context Free Grammar 1 - Materi 6 - TBO
ahmad haidaroh
Ā 
Estrutura de Dados em Java (FunƧƵes e Procedimentos)
Adriano Teixeira de Souza
Ā 
Linguagem C 09 Ponteiros
Regis Magalhães
Ā 
Binary tree
Rajendran
Ā 
Formato articulos ieee
Lucho Castagneto
Ā 
Linguagem C - Estruturas
Elaine CecĆ­lia Gatto
Ā 
Desenho TƩcnico
Elton Magno
Ā 
1.8 splay tree
Krish_ver2
Ā 
Context Free Grammar 1 - Materi 6 - TBO
ahmad haidaroh
Ā 

What's hot (20)

DOCX
Tipos de algoritmos
Edmilson Coreria
Ā 
PDF
Lógica e algoritmos
Jocelma Rios
Ā 
PPTX
Algoritmos - Procedimentos
Elaine CecĆ­lia Gatto
Ā 
PPTX
Php famous built in functions
Maaz Shamim
Ā 
DOCX
Modelos de base de dados
Daiana de Ɓvila
Ā 
PDF
POO - 14 - Vetores
Ludimila Monjardim Casagrande
Ā 
PPTX
Algoritmo e lógica de programação - Aula 1
engenhariadecomputacao
Ā 
PPTX
Algoritmos - Aula 15 - Modularizacao e Escopo de Variaveis
Rodrigo Kiyoshi Saito
Ā 
PDF
6 estruturas de dados heterogĆŖneas
EmĆ­lia Alves Nogueira
Ā 
ODP
VariƔveis e portugol
Carlos Wagner Costa
Ā 
PPTX
Introdução a Algoritmos: Conceitos BÔsicos
Elaine CecĆ­lia Gatto
Ā 
PPTX
PRIM’S AND KRUSKAL’S ALGORITHM
JaydeepDesai10
Ā 
PDF
Aula 7 expressão regular
wab030
Ā 
PPTX
Algoritmos - Formas de Representação de Algoritmos
Elaine CecĆ­lia Gatto
Ā 
TXT
Exercicios resolvidos visuAlg
Willians Miyabara
Ā 
PDF
Apostila completa desenho tecnico telecurso 2000
paulojadacosta
Ā 
DOCX
Exercƭcios PrƔticos de WORD E EXCEL
Fernanda Fruguli
Ā 
PPTX
Algoritmo Heap Sort
Jonathan Garcia
Ā 
PDF
Minicurso PostgreSQl
Cezar Souza
Ā 
Tipos de algoritmos
Edmilson Coreria
Ā 
Lógica e algoritmos
Jocelma Rios
Ā 
Algoritmos - Procedimentos
Elaine CecĆ­lia Gatto
Ā 
Php famous built in functions
Maaz Shamim
Ā 
Modelos de base de dados
Daiana de Ɓvila
Ā 
POO - 14 - Vetores
Ludimila Monjardim Casagrande
Ā 
Algoritmo e lógica de programação - Aula 1
engenhariadecomputacao
Ā 
Algoritmos - Aula 15 - Modularizacao e Escopo de Variaveis
Rodrigo Kiyoshi Saito
Ā 
6 estruturas de dados heterogĆŖneas
EmĆ­lia Alves Nogueira
Ā 
VariƔveis e portugol
Carlos Wagner Costa
Ā 
Introdução a Algoritmos: Conceitos BÔsicos
Elaine CecĆ­lia Gatto
Ā 
PRIM’S AND KRUSKAL’S ALGORITHM
JaydeepDesai10
Ā 
Aula 7 expressão regular
wab030
Ā 
Algoritmos - Formas de Representação de Algoritmos
Elaine CecĆ­lia Gatto
Ā 
Exercicios resolvidos visuAlg
Willians Miyabara
Ā 
Apostila completa desenho tecnico telecurso 2000
paulojadacosta
Ā 
Exercƭcios PrƔticos de WORD E EXCEL
Fernanda Fruguli
Ā 
Algoritmo Heap Sort
Jonathan Garcia
Ā 
Minicurso PostgreSQl
Cezar Souza
Ā 
Ad

Viewers also liked (19)

PPTX
Object Oriented Programming - Inheritance
Dudy Ali
Ā 
PPTX
Object Oriented Programming - Abstraction & Encapsulation
Dudy Ali
Ā 
PPTX
Java CRUD Mechanism with SQL Server Database
Dudy Ali
Ā 
PPTX
Object Oriented Programming - Value Types & Reference Types
Dudy Ali
Ā 
PPTX
Object Oriented Programming - Constructors & Destructors
Dudy Ali
Ā 
PPTX
Database Introduction - Akses Data dengan SQL Server
Dudy Ali
Ā 
PPTX
Network Socket Programming with JAVA
Dudy Ali
Ā 
PPTX
Information System Security - Akuntabilitas dan Akses Kontrol
Dudy Ali
Ā 
PPTX
Information System Security - Teknik Akses Kontrol
Dudy Ali
Ā 
PPTX
Information System Security - Komponen Intranet dan Ekstranet
Dudy Ali
Ā 
PPTX
Information System Security - Prinsip Manajemen Keamanan
Dudy Ali
Ā 
PPTX
Information System Security - Konsep Manajemen Keamanan
Dudy Ali
Ā 
DOCX
Diagram Konteks dan DFD Sistem Informasi Penjualan
Ricky Kusriana Subagja
Ā 
PPSX
MIS BAB 10
Riza Nurman
Ā 
PPTX
Information System Security - Konsep dan Kebijakan Keamanan
Dudy Ali
Ā 
PPTX
Information System Security - Serangan dan Pengawasan
Dudy Ali
Ā 
DOCX
Perancangan (diagram softekz, dfd level 0,1,2)
Joel Marobo
Ā 
PPTX
Information System Security - Kriptografi
Dudy Ali
Ā 
PPTX
Information System Security - Keamanan Komunikasi dan Jaringan
Dudy Ali
Ā 
Object Oriented Programming - Inheritance
Dudy Ali
Ā 
Object Oriented Programming - Abstraction & Encapsulation
Dudy Ali
Ā 
Java CRUD Mechanism with SQL Server Database
Dudy Ali
Ā 
Object Oriented Programming - Value Types & Reference Types
Dudy Ali
Ā 
Object Oriented Programming - Constructors & Destructors
Dudy Ali
Ā 
Database Introduction - Akses Data dengan SQL Server
Dudy Ali
Ā 
Network Socket Programming with JAVA
Dudy Ali
Ā 
Information System Security - Akuntabilitas dan Akses Kontrol
Dudy Ali
Ā 
Information System Security - Teknik Akses Kontrol
Dudy Ali
Ā 
Information System Security - Komponen Intranet dan Ekstranet
Dudy Ali
Ā 
Information System Security - Prinsip Manajemen Keamanan
Dudy Ali
Ā 
Information System Security - Konsep Manajemen Keamanan
Dudy Ali
Ā 
Diagram Konteks dan DFD Sistem Informasi Penjualan
Ricky Kusriana Subagja
Ā 
MIS BAB 10
Riza Nurman
Ā 
Information System Security - Konsep dan Kebijakan Keamanan
Dudy Ali
Ā 
Information System Security - Serangan dan Pengawasan
Dudy Ali
Ā 
Perancangan (diagram softekz, dfd level 0,1,2)
Joel Marobo
Ā 
Information System Security - Kriptografi
Dudy Ali
Ā 
Information System Security - Keamanan Komunikasi dan Jaringan
Dudy Ali
Ā 
Ad

Similar to Object Oriented Programming - File Input & Output (20)

PPT
Cs 1114 - lecture-29
Zeeshan Sabir
Ā 
PPTX
Stream classes in C++
Shyam Gupta
Ā 
PPTX
file handling final3333.pptx
radhushri
Ā 
PPT
Savitch ch 06
Terry Yoast
Ā 
PPS
Aae oop xp_12
Niit Care
Ā 
DOCX
Student Lab Activity CIS170 Week 6 Lab Instructions.docx
florriezhamphrey3065
Ā 
PPT
Csc1100 lecture15 ch09
IIUM
Ā 
PPTX
Introduction to files management systems
araba8
Ā 
PPTX
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
RashidFaridChishti
Ā 
PPTX
Object Oriented Programming Using C++: Ch12 Streams and Files.pptx
RashidFaridChishti
Ā 
PDF
chapter-12-data-file-handling.pdf
study material
Ā 
PPT
File in cpp 2016
Dr .Ahmed Tawwab
Ā 
PPTX
Pf cs102 programming-8 [file handling] (1)
Abdullah khawar
Ā 
PPTX
Input output files in java
Kavitha713564
Ā 
PPT
Savitch Ch 06
Terry Yoast
Ā 
PPT
Savitch Ch 06
Terry Yoast
Ā 
PDF
Chpater29 operation-on-file
Deepak Singh
Ā 
PDF
VIT351 Software Development VI Unit5
YOGESH SINGH
Ā 
PDF
Data file handling
Prof. Dr. K. Adisesha
Ā 
Cs 1114 - lecture-29
Zeeshan Sabir
Ā 
Stream classes in C++
Shyam Gupta
Ā 
file handling final3333.pptx
radhushri
Ā 
Savitch ch 06
Terry Yoast
Ā 
Aae oop xp_12
Niit Care
Ā 
Student Lab Activity CIS170 Week 6 Lab Instructions.docx
florriezhamphrey3065
Ā 
Csc1100 lecture15 ch09
IIUM
Ā 
Introduction to files management systems
araba8
Ā 
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
RashidFaridChishti
Ā 
Object Oriented Programming Using C++: Ch12 Streams and Files.pptx
RashidFaridChishti
Ā 
chapter-12-data-file-handling.pdf
study material
Ā 
File in cpp 2016
Dr .Ahmed Tawwab
Ā 
Pf cs102 programming-8 [file handling] (1)
Abdullah khawar
Ā 
Input output files in java
Kavitha713564
Ā 
Savitch Ch 06
Terry Yoast
Ā 
Savitch Ch 06
Terry Yoast
Ā 
Chpater29 operation-on-file
Deepak Singh
Ā 
VIT351 Software Development VI Unit5
YOGESH SINGH
Ā 
Data file handling
Prof. Dr. K. Adisesha
Ā 

More from Dudy Ali (20)

PDF
Understanding COM+
Dudy Ali
Ā 
PDF
Distributed Application Development (Introduction)
Dudy Ali
Ā 
PPTX
Review Materi ASP.NET
Dudy Ali
Ā 
PPTX
XML Schema Part 2
Dudy Ali
Ā 
PPTX
XML Schema Part 1
Dudy Ali
Ā 
PPTX
Rendering XML Document
Dudy Ali
Ā 
PPTX
Pengantar XML
Dudy Ali
Ā 
PPTX
Pengantar XML DOM
Dudy Ali
Ā 
PPTX
Pengantar ADO.NET
Dudy Ali
Ā 
PPTX
Database Connectivity with JDBC
Dudy Ali
Ā 
PPTX
XML - Displaying Data ith XSLT
Dudy Ali
Ā 
PPTX
Algorithm & Data Structure - Algoritma Pengurutan
Dudy Ali
Ā 
PPTX
Algorithm & Data Structure - Pengantar
Dudy Ali
Ā 
PPTX
Web Programming Syaria - Pengenalan Halaman Web
Dudy Ali
Ā 
PPTX
Web Programming Syaria - PHP
Dudy Ali
Ā 
PPTX
Software Project Management - Project Management Knowledge
Dudy Ali
Ā 
PPTX
Software Project Management - Proses Manajemen Proyek
Dudy Ali
Ā 
PPTX
Software Project Management - Pengenalan Manajemen Proyek
Dudy Ali
Ā 
PPTX
System Analysis and Design - Unified Modeling Language (UML)
Dudy Ali
Ā 
PPTX
System Analysis and Design - Tinjauan Umum Pengembangan Sistem
Dudy Ali
Ā 
Understanding COM+
Dudy Ali
Ā 
Distributed Application Development (Introduction)
Dudy Ali
Ā 
Review Materi ASP.NET
Dudy Ali
Ā 
XML Schema Part 2
Dudy Ali
Ā 
XML Schema Part 1
Dudy Ali
Ā 
Rendering XML Document
Dudy Ali
Ā 
Pengantar XML
Dudy Ali
Ā 
Pengantar XML DOM
Dudy Ali
Ā 
Pengantar ADO.NET
Dudy Ali
Ā 
Database Connectivity with JDBC
Dudy Ali
Ā 
XML - Displaying Data ith XSLT
Dudy Ali
Ā 
Algorithm & Data Structure - Algoritma Pengurutan
Dudy Ali
Ā 
Algorithm & Data Structure - Pengantar
Dudy Ali
Ā 
Web Programming Syaria - Pengenalan Halaman Web
Dudy Ali
Ā 
Web Programming Syaria - PHP
Dudy Ali
Ā 
Software Project Management - Project Management Knowledge
Dudy Ali
Ā 
Software Project Management - Proses Manajemen Proyek
Dudy Ali
Ā 
Software Project Management - Pengenalan Manajemen Proyek
Dudy Ali
Ā 
System Analysis and Design - Unified Modeling Language (UML)
Dudy Ali
Ā 
System Analysis and Design - Tinjauan Umum Pengembangan Sistem
Dudy Ali
Ā 

Recently uploaded (20)

PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
Ā 
PDF
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
Ā 
PDF
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
Ā 
PDF
Software Development Company | KodekX
KodekX
Ā 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
Ā 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
Ā 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
Ā 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
Ā 
PDF
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
Ā 
PDF
This slide provides an overview Technology
mineshkharadi333
Ā 
PPTX
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
Ā 
PDF
Beyond Automation: The Role of IoT Sensor Integration in Next-Gen Industries
Rejig Digital
Ā 
PDF
Software Development Methodologies in 2025
KodekX
Ā 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
Ā 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
Ā 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
Ā 
PPTX
IoT Sensor Integration 2025 Powering Smart Tech and Industrial Automation.pptx
Rejig Digital
Ā 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
Ā 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
Ā 
PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
Ā 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
Ā 
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
Ā 
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
Ā 
Software Development Company | KodekX
KodekX
Ā 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
Ā 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
Ā 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
Ā 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
Ā 
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
Ā 
This slide provides an overview Technology
mineshkharadi333
Ā 
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
Ā 
Beyond Automation: The Role of IoT Sensor Integration in Next-Gen Industries
Rejig Digital
Ā 
Software Development Methodologies in 2025
KodekX
Ā 
cloud computing vai.pptx for the project
vaibhavdobariyal79
Ā 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
Ā 
Brief History of Internet - Early Days of Internet
sutharharshit158
Ā 
IoT Sensor Integration 2025 Powering Smart Tech and Industrial Automation.pptx
Rejig Digital
Ā 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
Ā 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
Ā 
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
Ā 

Object Oriented Programming - File Input & Output

  • 1. Q3M1 – OOP C# Dudy Fathan Ali S.Kom File Input & Output 1 Q3M1 Dudy Fathan Ali, S.Kom (DFA) 2015 CEP - CCIT Fakultas Teknik Universitas Indonesia
  • 2. File Input & Output Q3M1 – OOP C# Dudy Fathan Ali S.Kom o The stream is a sequence of bytes travelling from a source to a destination over a communication path. o The two basic streams used are the input and output streams. o Input stream is used for a read operation. o Output stream is used for performing a write operation. o The System.IO namespace includes various classes, which are used to perform operations, such as file creation, file deletion, and the read-write operations to files.
  • 3. File Input & Output Q3M1 – OOP C# Dudy Fathan Ali S.Kom Common Class of System.IO Namespace The following table describes some commonly used classes in the System.IO namespace.
  • 4. File Input & Output Q3M1 – OOP C# Dudy Fathan Ali S.Kom FileStream Class Most file Input/Output (I/O) support in the .NET Framework is implemented in the System.IO namespace. You can use the FileStream class in the System.IO namespace to read from, to write, and to close files. FileStream [ObjName] = new FileStream([FileName], [FileMode], [FileAccess]) Code Structure: FileStream fs = new FileStream(ā€œMyFile.txtā€, FileMode.Open, FileAccess.Read); Example:
  • 5. File Input & Output Q3M1 – OOP C# Dudy Fathan Ali S.Kom StreamReader and StreamWriter Class The Stream class is used to read from and to write data in the text files. If data of a file is only text, then you can use the StreamReader class and the StreamWriter class to accomplish the reading and writing tasks StreamReader sr = new StreamReader([ObjFileStream]) Code Structure: FileStream fs = new FileStream(ā€œMyFile.txtā€, FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(fs); Example:
  • 6. File Input & Output Q3M1 – OOP C# Dudy Fathan Ali S.Kom Process of input data with FileStream and StreamWriter File Input.txt FileStream StreamWriter Write Data to Stream Mengosongkan memori Pembacaan data bisa dari input user atau dari cara yang lain Data di simpan ke dalam file Input.txt Buka StreamWriter Buka FileStream Read Data From User Close SW & FS Tutup StreamWriter dan FileStream Flush
  • 7. File Input & Output Q3M1 – OOP C# Dudy Fathan Ali S.Kom Process of input data with FileStream and StreamWriter Consider the following code:
  • 8. File Input & Output Q3M1 – OOP C# Dudy Fathan Ali S.Kom Latihan Mandiri Diketahui nama dosen sebagai berikut : Masukkan lah nama-nama tersebut kedalam satu file dengan nama datadosen.txt dengan menggunakan FileStream. Catatan: Nama dosen harus dimasukkan berdasarkan inputan user (bukan nilai statis dari variable) Dudy Fathan Ali S.Kom Fachran Nazarullah S.Kom Tri Agus Riyadi S.Kom Riza Muhammad Nurman S.Kom Musawarman S.Kom
  • 9. File Input & Output Q3M1 – OOP C# Dudy Fathan Ali S.Kom Process of reading data with FileStream and StreamReader File Output.txt FileStream Tampilkan Data Next Data Y N Close SR Close FS Tutup StreamReader Tutup FileStream (b) Buka StreamReader (a) Buka FileStream (a)  Membuka File (b)  Memuat Isi File ke dalam object sr != null StreamReader
  • 10. File Input & Output Q3M1 – OOP C# Dudy Fathan Ali S.Kom Process of reading data with FileStream and StreamWriter Consider the following code:
  • 11. File Input & Output Q3M1 – OOP C# Dudy Fathan Ali S.Kom Process of search data with FileStream and StreamReader File Output.txt FileStream StreamReader sr != null Y N Close SR Close FS Tutup StreamReader Tutup FileStream Buka StreamReader Buka FileReader Data Search Data yang di cari Variabel dataUntuk menampung hasil pencarian data Hasil cari simpan ke variabel data Y a Cetak data Apakah text yang di cari ada, Jika iya maka akan di simpan ke variabel data Untuk pengecekan, anda bisa menggunakan Contains aNext Data Y
  • 12. File Input & Output Q3M1 – OOP C# Dudy Fathan Ali S.Kom Process of search data with FileStream and StreamWriter
  • 13. File Input & Output Q3M1 – OOP C# Dudy Fathan Ali S.Kom Latihan Mandiri Anda ditugaskan untuk mencari nama lengkap dari orang yang bernama ā€œFachranā€ dari file datadosen.txt yang berisi data sebagai berikut : Tampilkan data yang dicari sehingga menghasilkan output sebagai berikut : Catatan : Parameter pencarian harus dari inputan user (bukan nilai statis dari variable) Data yang dicari : Fachran Nazarullah S.Kom Dudy Fathan Ali S.Kom Fachran Nazarullah S.Kom Tri Agus Riyadi S.Kom Riza Muhammad Nurman S.Kom Musawarman S.Kom
  • 14. File Input & Output Q3M1 – OOP C# Dudy Fathan Ali S.Kom Delimiter o Delimiter = Pemisah o Simbol bisa digunakan untuk delimiter (#, $, &, ~) o Implementasinya menggunakan array
  • 15. File Input & Output Q3M1 – OOP C# Dudy Fathan Ali S.Kom Delimiter o Penggunaan Delimiter : o Contoh input data pelanggan: o ID Pelanggan : 8934 o Nama Pelanggan : Joni Simanjuntak o Jenis Kelamin : Pria o Alamat : Jakarta 8934#Joni Simanjuntak#Pria#Jakarta 8934;Joni Simanjuntak;Pria;Jakarta Memiliki delimiter dengan simbol ā€˜#’ Memiliki delimiter dengan simbol ā€˜;’
  • 16. File Input & Output Q3M1 – OOP C# Dudy Fathan Ali S.Kom Mengapa harus menggunakan delimiter? Berikut data pelanggan --------------------------------- ID Pelanggan : 8934 Nama Pelanggan : Joni Simanjuntak Jenis Kelamin : Pria Alamat : Jakarta Terkadang developer membutuhkan tampilan seperti ini: Dengan menggunakan delimiter, developer dapat lebih mudah memecah data yang satu dengan yang lain. 8934#Joni Simanjuntak#Pria#Jakarta array[0] = 8394 array[1] = Joni Simanjuntak array[2] = Pria array[3] = Jakarta
  • 17. File Input & Output Q3M1 – OOP C# Dudy Fathan Ali S.Kom Process of input data with delimiter format. Consider the following code:
  • 18. File Input & Output Q3M1 – OOP C# Dudy Fathan Ali S.Kom Process of reading data with delimiter format. Consider the following code:
  • 19. Q3M1 – OOP C# Dudy Fathan Ali S.Kom Thank You! Dudy Fathan Ali S.Kom [email protected]