Top Banner
BIM313 – Advanced Programming File Operations 1
25

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

Jan 17, 2016

Download

Documents

Bridget Bailey
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: BIM313 – Advanced Programming File Operations 1. Contents Structure of a File Reading/Writing Texts from/to Files File and Directory Operations 2.

BIM313 – Advanced Programming

File Operations

1

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

Contents

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

2

Page 3: 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

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

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

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

5

• Consider how we can store short int 30000 = 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’ ‘0’ ‘0’ ‘0’ TextFileEx.txt5 bytes long

0 1 2 3 4Byte #

– The other option is to store the number in binary, which would take as few as 2 bytes

0x30 0x75 BinaryFileEx.dat2 bytes long

0 1Byte #

Assumes little-endian representation

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

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

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

File Classes in .NET Framework

7

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

File Operations

• Creating a new file• Appending to an existing file• Reading an existing file

8

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

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

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

Exampleusing 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

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

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

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

Exampleusing 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

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

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

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

Exampleusing 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

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

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

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

Exampleusing 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

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

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 like @“C:\MyPrograms\BIM313\Test.txt”

17

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

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

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

Obtaining File and Directory Information

• The FileInfo class can be used to obtain information about a specific fileFileInfo 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 classDirectoryInfo info = new DirectoryInfo(@“c:\”);info.Parent, info.Root, info.GetFiles(), info.GetDirectories(), etc.

19

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

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

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

Directory Operations

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

21

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

Some Advanced Topics

22

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

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

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

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

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

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