BACS 387 - University of Northern Colorado

Download Report

Transcript BACS 387 - University of Northern Colorado

Basic Data Access
Files , Streams, & I/O Classes
Learn the basic terminology associated with files
and file processing.
 Learn about streams, serial devices, and basic C#
classes for input / output.
 Learn how to create and use FileStream objects.
 Learn how to create and use StreamReader and
StreamWriter objects.

Last update: 7/16/2015
OO Programming
2






A stream is an abstract representation of a serial device.
A serial device is something that stores data in the order
that it arrives (i.e., serially or linearly).
Serial devices also access data in the order it was stored,
one byte at a time.
There are 2 types of streams:
 Input
 Output
.NET provides special classes to deal with streams.
When using these classes, you need to remember to
include the Using System.IO; statement.
Last update: 7/16/2015
OO Programming
3
Class
Description
File
A static class that provides methods to move, copy, and
delete files.
Directory
A static class that provides methods to move, copy, and
delete directories.
Path
A class used to manipulate path names.
FileStream
Represents a file that can be written to and read from. Both
asynchronous and synchronous I/O are allowed.
StreamReader
Reads character data from a stream. Can be created using a
FileStream as a base.
StreamWriter
Writes character data to a stream. Can be created using a
FileStream as a base.
Last update: 7/16/2015
OO Programming
4
Method
Description
Copy()
Copies a file from a source location to a target location.
Create()
Creates a file in a specified path.
Delete()
Deletes a file.
Open()
Returns a FileStream object at the specified path.
Move()
Moves a file to a new location .
Last update: 7/16/2015
OO Programming
5
Method
Description
CreateDirectory()
Create a directory with the specified path.
Delete()
Deletes a directory and all files within it.
GetDirectories()
Returns an array of string objects that represent
the names of the directories below the specified
directory.
GetFiles()
Returns an array of string objects that represent
the names of the files in the specified directory.
GetFileSystemEntries() Returns an array of string objects that represent
the names of the files and directories in the
specified directory.
Move()
Last update: 7/16/2015
Moves a directory to a new location.
OO Programming
6






The FileStream object represents a stream
pointing to a file on a disk drive.
This disk drive may be local or on a network.
This object is used most often on bytes and byte
arrays.
This type of data is used in random file access.
No other I/O class can perform these operations on
this type of data as well as the FileStream object.
Example Syntax:
FileStream myFile = new FileStream(“MyFile.txt”, FileMode.Create);
Last update: 7/16/2015
OO Programming
7
The FileStream class keeps a pointer to the location
where the next I/O operation will occur.
 You can manipulate this with the Seek method.
Examples of its use are:

myFile.Seek(10, SeekOrigin.Begin);
myFile.Seek(14, SeekOrigin.Current);
myFile.Seek(-23, SeekOrigin.End);

The first example starts 10 bytes after the origin
(beginning) of the file. The second example starts
14 bytes after the current position, and the third
example starts 23 bytes from the end of the file.
Last update: 7/16/2015
OO Programming
8
...
static void Main(string[] args)
{
byte[] myByteData = new byte[200];
char[] myCharData = new Char[200];
try
{
FileStream aFile = new FileStream("../../Program.cs",
FileMode.Open);
aFile.Seek(113, SeekOrigin.Begin);
aFile.Read(myByteData, 0, 200);
}
catch(IOException e)
{
Console.WriteLine("An I/O exception has been thrown");
Console.WriteLine(e.ToString());
Console.ReadKey();
return;
}
Decoder d = Encoding.UTF8.GetDecoder();
d.GetChars(myByteData, 0, myByteData.Length, myCharData, 0);
Console.WriteLine(myCharData);
Console.ReadKey();
...
Last update: 7/16/2015
OO Programming
9





The StreamWriter object is designed specifically to deal
with writing streams of character data.
It performs many of the underlying conversion tasks
automatically.
It is easier to use, but cannot handle as many types of
data as the FileStream object.
It also cannot be used for random access.
The easiest way to create a StreamWriter is:
StreamWriter sw = new StreamWriter(“myFile.txt”, true);

The ‘true’ parameter says that it is to create a new file if one
does not exist or append to the file if one does exist.
Last update: 7/16/2015
OO Programming
10
namespace StreamWriter_Example
{
class Program
{
static void Main(string[] args)
{
StreamWriter s = new StreamWriter("myFile.txt", true);
s.WriteLine("{0} {1}", "test", 55);
s.Close();
}
}
}
Last update: 7/16/2015
OO Programming
11
The StreamReader object allows you to read data
from an external source.
 Normally it will be from a disk drive or a network
device.
 Like the StreamWriter, the StreamReader is
designed for character data, so it performs many of
the background tasks for you.
 You can create a StreamReader as shown below:

StreamReader sr = new StreamReader(“Myfile.txt”);
Last update: 7/16/2015
OO Programming
12
...
static void Main(string[] args)
{
String line;
StreamReader sr = new StreamReader("../../Program.cs");
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
sr.Close();
Console.ReadKey();
}
...
Last update: 7/16/2015
OO Programming
13
...
using System;
using System.IO;
public class MainClass
{
static void Main()
{
StreamReader MyStreamReader = new StreamReader
("../../Program.cs");
string str = MyStreamReader.ReadToEnd();
Console.WriteLine(str);
}
MyStreamReader.Close();
}
. . .
Last update: 7/16/2015
OO Programming
14
using System;
using System.IO;
class MainClass
{
public static void Main()
{
StringWriter strwtr = new StringWriter();
for (int i = 0; i < 10; i++)
strwtr.WriteLine("This is i: " + i);
StringReader strrdr = new StringReader(strwtr.ToString());
}
}
string str = strrdr.ReadLine();
while (str != null)
{
str = strrdr.ReadLine();
Console.WriteLine(str);
}
Console.ReadKey();
Last update: 7/16/2015
OO Programming
15

The output of the program from the previous slide
would be:
Last update: 7/16/2015
OO Programming
16






Streams are an abstract representation of a serial device.
Serial devices are something that stores data in the order
that it arrives.
The two types of streams are input and output.
C# (and .NET in general) provides special classes designed to
handle stream input/output.
FileStream objects handle raw data. They are more versatile
than the other stream classes we discuss, but also require
you to do more work behind the scenes.
One unique thing they can do is access data in random order.
StreamReader and StreamWriter objects are designed to
work with character data. They cannot access data
randomly.
Last update: 7/16/2015
OO Programming
17