Académique Documents
Professionnel Documents
Culture Documents
Contents
Introduction
Sequential file I/O
Classes File and Directory
Serialization
Summary
Introduction
Spreadsheets
Word processing
etc
Introduction
Introduction
FileStream object
byte stream
FileStream object
byte stream
class FileCopy
{
public static void Main(String[] args){
try{
FileStream fromStream =
new FileStream(args[0], FileMode.Open,
FileAccess.Read);
FileStream toStream =
new FileStream(args[1], FileMode.Create,
FileAccess.Write);
fromStream.Read(buffer, 0, buffer.Length);
toStream.Write(buffer, 0, buffer.Length);
}
catch
{
Console.WriteLine("Usage: FileCopy [FromFile]
[ToFile]");
}
}
}
using System;
using System.IO;
class CreateTextFile
{
public static void Main(String[] args)
{
try
{
FileStream toStream =
new FileStream(args[0], FileMode.Create, FileAccess.Write);
StreamWriter fileWriter = new StreamWriter(toStream);
for (int i = 0; i < 10; i++)
fileWriter.WriteLine("i= " + i);
fileWriter.Close();
toStream.Close();
FileStream fromStream =
new FileStream(args[0], FileMode.Open, FileAccess.Read);
StreamReader fileReader = new StreamReader(fromStream);
for (int i = 0; i < 10; i++)
{
String input = fileReader.ReadLine();
Console.WriteLine(input);
}
}
catch{Console.WriteLine("Usage: CreateTextFile OutputFile");
}
}
using System;
using System.Windows.Forms;
using System.IO;
public partial class FileTestForm1 : Form
{
public FileTestForm1()
{
InitializeComponent(); }
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
string fileName = openFileDialog1.FileName;
if (File.Exists(fileName))
displayFileInfo(fileName);
}
private void displayFileInfo(string fileName)
{
// Displays file information }
private void displayDirectoryInfo(string pathName)
{
// Displays directory information }
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
}
private void button2_Click(object sender, EventArgs e)
{
folderBrowserDialog1.ShowDialog();
string pathName = folderBrowserDialog1.SelectedPath;
if (Directory.Exists(pathName))
displayDirectoryInfo(pathName);
}
}
Serialization
Serialization
Record file
string string int string
StudentInfo
Name
Address
ID number
Course Info
Serialization
In this example, any program that needs to read the file needs to
know the format of the data
2 strings, an int and then a string
Also whether each item is on a separate line
Object serialization allows complete objects to be read or written
with a single statement
A serialized object is an object represented as a sequence of bytes
Information is stored about the data types of the objects instance
fields as well as their values
Allows the object to be reconstructed (de-serialized) from the
file
Serialization
Record file
StudentInfo object
StudentInfo
Name
Address
ID number
Course Info
Serialization
Serialization
Serialization
The user inputs student info. details in the
corresponding textboxes on the left which through
the serialization will be stored in a binary file
The information in the binary file will be
deserialized and displayed in the right hand
textboxes
We will create a simple StudentInfo class and tag
it as Serializable
Serialization
[Serializable]
class StudentInfo
{
public string Name, Address, CourseInfo;
public int ID;
public StudentInfo()
{}
public StudentInfo(String n, String a, String ci, int id)
{
Name = n; Address = a; CourseInfo = ci;
ID = id;
}
}
Serialization
using
using
using
using
System.Windows.Forms;
System.IO;
System.Runtime.Serialization;
System.Runtime.Serialization.Formatters.Binary;
Serialization
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
String name = textBox1.Text;
String address = textBox2.Text;
int id = Convert.ToInt32(textBox3.Text);
String courseInfo = textBox4.Text;
StudentInfo s = new StudentInfo(name,address,courseInfo,id);
FileStream filestream = new FileStream(fileName, FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(filestream, s);
filestream.Close();
button1.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
FileStream filestream2 = new FileStream(fileName,FileMode.Open);
BinaryFormatter bf2 = new BinaryFormatter();
StudentInfo si= new StudentInfo();
si = (StudentInfo)bf2.Deserialize(filestream2);
textBox8.Text = si.Name;
textBox7.Text = si.Address;
textBox6.Text = ""+si.ID;
textBox5.Text = si.CourseInfo;
filestream2.Close();
}
Serialization
Serialization
Serialization
Binary serialization is limited as only .NET
applications can deserialize the data stream
For more general inter-operability especially across a
network, XML serialization is used
XML is text based and self describing and
universal
Comprised name/attribute pairs
XML serialization easy to implement and uses text
streams
Serialization
We need to insert XML tags into our
StudentInfo class
Only public properties and fields can be
serialized
[XmlRoot("studentInfo")]
public class StudentInfo
{
[XmlAttribute("name")] public string Name;
[XmlAttribute("address")] public string Address;
[XmlAttribute("course")] public string CourseInfo;
[XmlAttribute("id")] public int ID;
public StudentInfo()
{ }
public StudentInfo(String n, String a, String ci, int id)
{
Name = n; Address = a; CourseInfo = ci;
ID = id;
}
}
using
using
using
using
using
System;
System.Windows.Forms;
System.IO;
System.Xml;
System.Xml.Serialization;
Serialization
Summary
We have seen how we can use streams to do
simple sequential file I/O for binary and
text files
We have looked at how we can use the File
and Directory classes
We have looked at object serialization to
binary and XML