BIM313 – Advanced Programming File Operations 1. Contents Structure of a File Reading/Writing Texts from/to Files File and Directory Operations 2.

Slides:



Advertisements
Similar presentations
Reading and Writing Text Files Svetlin Nakov Telerik Corporation
Advertisements

Alford Academy Business Education and Computing1 Advanced Higher Computing Based on Heriot-Watt University Scholar Materials File Handling.
C# - Files and Streams Outline Files and Streams Classes File and Directory Creating a Sequential-Access File Reading Data from a Sequential-Access.
Files & Streams. Files Introduction Files are used for long-term retention of large amounts of data, even after the program that created the data terminates.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
C# Programming: From Problem Analysis to Program Design1 2 Your First C# Program.
C# Programming: From Problem Analysis to Program Design1 Working with Files C# Programming: From Problem Analysis to Program Design 3 rd Edition 13.
File and Streams There are many ways of organizing records in a file. There are many ways of organizing records in a file. The most common type of organization.
MIS316 – BUSINESS APPLICATION DEVELOPMENT – Chapter 14 – Files and Streams 1Microsoft Visual C# 2012, Fifth Edition.
Understanding Input/Output (I/O) Classes Lesson 5.
Computer and Programming File I/O File Input/Output Author: Chaiporn Jaikaeo, Jittat Fakcharoenphol Edited by Supaporn Erjongmanee Lecture 13.
Lecture Set 12 Sequential Files and Structures Part B – Reading and Writing Sequential Files.
Visual C Sharp – File I/O - 1 Variables and arrays only temporary - lost when a program terminates Files used for long term storage (Data bases considered.
Chapter 12 Working with Files CIS 3260 Introduction to Programming using C# Hiro Takeda.
Files and Streams. Java I/O File I/O I/O streams provide data input/output solutions to the programs. A stream can represent many different kinds of sources.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
File I/O 11_file_processing.ppt
Methods Session 04 Mata kuliah: M0874 – Programming II Tahun: 2010.
File I/O Static void Main( ) {... } data. Topics I/O Streams Reading and Writing Text Files Formatting Text Files Handling Stream Errors File Pointers.
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
File Systems CSCI What is a file? A file is information that is stored on disks or other external media.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
Files and Streams File Types, Using Streams, Manipulating Files SoftUni Team Technical Trainers Software University
Reference: Lecturer Lecturer Reham O. Al-Abdul Jabba lectures for cap211 Files and Streams- I.
Object Oriented Software Development 10. Persistent Storage.
Files and Streams. Objectives Learn about the classes that support file input/output Understand the concept of abstraction and how it related to the file.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
CS360 Windows Programming
Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer.
Copyright © 2012 Pearson Education, Inc. Chapter 5 Loops, File, and Random Numbers.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
Text Files and String Processing
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
Input and Output 23: Input and Output
CSC 298 Streams and files.
Ajay Tripathi Input Output. Ajay Tripathi Input/output (IO) refers to the operations for reading and writing data to streams and files. In the.NET Framework,
McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 11 Data Files.
COMP 110: Spring Announcements Program 5 Milestone 1 was due today Program 4 has been graded.
Files and Streams. What is a file? Up until now, any stored data within a program is lost when the program closes. A file is a permanent way to store.
Strings in C++/CLI us/library/system.string.aspxhttp://msdn.microsoft.com/en- us/library/system.string.aspx public: static.
Files and Streams. Objectives Learn about the classes that support file input/output Understand the concept of abstraction and how it related to the file.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
Searching, Modifying, and Encoding Text. Parts: 1) Forming Regular Expressions 2) Encoding and Decoding.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
File Output Writing data out to a file 1. Output to files  StreamWriter : An object in the System.IO namespace that lets you print output to a destination.
Chapter 6: Creating Windows–based Applications 1 Microsoft® Visual C# 2008.
Lecture 18 File I/O Richard Gesick. File Input / Output Consider that a program may want to make its data persistent (or not rely upon the user for input)
Module 6: Building .NET–based Applications with C#
Input and Output 23: Input and Output
C# Programming: From Problem Analysis to Program Design
CHAPTER 5 JAVA FILE INPUT/OUTPUT
Defiana Arnaldy, M.Si File and Stream I/O Defiana Arnaldy, M.Si
How to work with files and data streams
Files and Streams.
Files and Streams Lect3 CT1411.
Chapter 2.
File Input/Output (I/O)
Chapter 17 Binary I/O Dr. Clincy - Lecture.
Topics Introduction to File Input and Output
Files & Streams.
Files and Streams Lect10 GC201 12/1/2015.
Lecture 16 File I/O Richard Gesick.
Fundaments of Game Design
BO65: PROGRAMMING WRITING TO TEXT FILES.
How to work with files and data streams
Names of variables, functions, classes
Files and Streams.
Topics Introduction to File Input and Output
Lecture 18 File I/O CSE /5/2019.
Presentation transcript:

BIM313 – Advanced Programming File Operations 1

Contents Structure of a File Reading/Writing Texts from/to Files File and Directory Operations 2

Definition of File A file is a logically contiguous stream of bytes with certain properties – Name – Size (Number of bytes in the file) – Owner (The user who owns the file) – Permission (Who can access the file) – Creation date – Last modification date – etc. 3

File Types Text Files – In a text file, the byte represent characters making it possible for a human to examine the file or edit it using a text editor Files which can be opened by Notepad C# source codes Binary Files – In a binary file, bytes do not necessarily represent characters. Groups of bytes might represent an int, float, double, etc. Executable files Word, Excel, PowerPoint files Files which can’t be opened by Notepad 4

5 Consider how we can store short int = 0x7530, which occupies 2 bytes in memory – One option is to store the number in text form as chars ‘3’, ‘0’, ‘0’, ‘0’, ‘0’ Using ASCII chars, we need 5 bytes to store this number Text File vs. Binary File ‘3’‘0’ TextFileEx.txt 5 bytes long Byte # –The other option is to store the number in binary, which would take as few as 2 bytes 0x300x75 BinaryFileEx.dat 2 bytes long 01 Byte # Assumes little-endian representation

Text File vs. Binary File Why distinguish between text and binary files? – The reason is some operating systems, e.g., Windows stores text files and binary files in different ways – Text files are divided into lines, so there must be some special way o mark the end of each line – Binary files are easy to use by programs and text files are easy to understand for humans 6

File Classes in.NET Framework 7

File Operations Creating a new file Appending to an existing file Reading an existing file 8

Creating a Text File Create a StreamWriter object by specifying a file path Use functions Write and WriteLine to write into the file, just like printing on the screen Close the file 9

Example using System.IO; namespace FileOpDeneme { class Program { static void Main(string[] args) { StreamWriter sw = new StreamWriter("Test.txt"); sw.WriteLine("Roses are red"); sw.WriteLine("Violets are blue"); sw.Close(); } 10

Appending to a Text File When you create a StreamWriter object by new StreamWriter(filename), and file exists, then the file contents are deleted If you want to append to an existing file, use another constructor of StreamWriter class, which is StreamWriter(string path, bool append) 11

Example using System.IO; namespace FileOpDeneme { class Program { static void Main(string[] args) { StreamWriter sw = new StreamWriter("Test.txt", true); sw.WriteLine("Sugar is sweet"); sw.WriteLine("And so are you"); sw.Close(); } 12

Reading an Existing File Create a StreamReader object by specifying the file path Use ReadLine() method to read a single line, or ReadToEnd() method to read all contents of the file Close the file 13

Example using System; using System.IO; namespace FileOpDeneme { class Program { static void Main(string[] args) { StreamReader sr = new StreamReader("Test.txt"); string contents = sr.ReadToEnd(); Console.WriteLine(contents); sr.Close(); } 14

using The files should be closed immediately after finishing work on them If you forget to close a file, some problems may occur If you use the file inside a using block, it is automatically closed, i.e. all of its resources are released 15

Example using System; using System.IO; namespace FileOpDeneme { class Program { static void Main(string[] args) { string contents; using (StreamReader sr = new StreamReader("Test.txt")) { contents = sr.ReadToEnd(); } Console.WriteLine(contents); } 16

Path Names and Relative Paths If you don’t specify the path of a file, it is searched in the current directory, i.e. the directory where your executable file is located You can learn the current directory by the method Directory.GetCurrentDirectory() You can set the working directory by Directory.SetCurrentDirectory() You can specify the full path of the file 17

The FileStream Object With StreamReader and StreamWriter objects, you can work only on characters If you need to work on bytes, you have to use a FileStream object On a FileStream object, you can go forward or backward on the file, and read from or write to anywhere on the file 18

Obtaining File and Directory Information The FileInfo class can be used to obtain information about a specific file FileInfo info = new FileInfo(“Test.txt”); info.Attributes, info.CreationTime, info.LastAccessTime, info.LastWriteTime, info.Length, etc. If you want to obtain information about a directory, you can use the DirectoryInfo class DirectoryInfo info = new info.Parent, info.Root, info.GetFiles(), info.GetDirectories(), etc. 19

File System Operations File.Copy(src, dest): src is copied to dest File.Move(src, dest): src is moved to dest File.Delete(filename): Deletes the file File.Exists(file): Returns true if file exists Check for other static methods in the File class 20

Directory Operations Directory.CreateDirectory() Directory.Delete() Directory.Exists() Directory.Move() Directory.GetCurrentDirectory() Directory.SetCurrentDirectory() Directory.GetLogicalDrives() etc. 21

Some Advanced Topics 22

Monitoring the File System Your program can be notified when a file or directory is being modified For this purpose, you can use the FileSystemWatcher class For more information, you may refer to the textbook, MSDN help, or Google 23

Compressed Files You can use DeflateStream and GZipStream classes to read and write compressed data from and to files These classes work with byte data much like FileStream The classes are located under the System.IO.Compression namespace For more information, refer to the textbook, MSDN help, or Google 24

Binary Files You can use BinaryReader and BinaryWriter classes to work on binary files You can serialize objects so that they can be saved to or read from files – Refer as Serialization For more information, refer to the textbook, MSDN help, or Google 25