/*
C# A Beginner's Guide
By Schildt
Publisher: Osborne McGraw-Hill
ISBN: 0072133295
*/
/*
Project 11-1
Compare two files.
To use this program, specify the names
of the files to be compared on the command line.
For example:
CompFile FIRST.TXT SECOND.TXT
*/
using System;
using System.IO;
public class CompFiles {
public static void Main(string[] args) {
int i=0, j=0;
FileStream f1;
FileStream f2;
try {
// open first file
try {
f1 = new FileStream(args[0], FileMode.Open);
} catch(FileNotFoundException exc) {
Console.WriteLine(exc.Message);
return;
}
// open second file
try {
f2 = new FileStream(args[1], FileMode.Open);
} catch(FileNotFoundException exc) {
Console.WriteLine(exc.Message);
return;
}
} catch(IndexOutOfRangeException exc) {
Console.WriteLine(exc.Message + "
Usage: CompFile f1 f2");
return;
}
// Compare files
try {
do {
i = f1.ReadByte();
j = f2.ReadByte();
if(i != j) break;
} while(i != -1 && j != -1);
} catch(IOException exc) {
Console.WriteLine(exc.Message);
}
if(i != j)
Console.WriteLine("Files differ.");
else
Console.WriteLine("Files are the same.");
f1.Close();
f2.Close();
}
}
Software Development
software development notes, main category for Cs, Java, and other programming experiences.
A simple disk-to-screen utility that demonstrates a FileReader
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
/* A simple disk-to-screen utility that
demonstrates a FileReader. */
using System;
using System.IO;
public class DtoS {
public static void Main() {
FileStream fin;
string s;
try {
fin = new FileStream("test.txt", FileMode.Open);
}
catch(FileNotFoundException exc) {
Console.WriteLine(exc.Message + "Cannot open file.");
return ;
}
StreamReader fstr_in = new StreamReader(fin);
// Read the file line-by-line.
while((s = fstr_in.ReadLine()) != null) {
Console.WriteLine(s);
}
fstr_in.Close();
}
}
illustrates use of FileStreams 2
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example15_11.cs illustrates use of FileStreams
*/
using System;
using System.Windows.Forms;
using System.IO;
public class Example15_11
{
[STAThread]
public static void Main()
{
// use an open file dialog to get a filename
OpenFileDialog dlgOpen = new OpenFileDialog();
dlgOpen.Title="Select file to back up";
if (dlgOpen.ShowDialog() == DialogResult.OK)
{
FileStream inStream = File.OpenRead(dlgOpen.FileName);
FileStream outStream =
File.OpenWrite(dlgOpen.FileName + ".bak");
byte[] buf = new byte[4096];
int bytesRead;
// copy all data from in to out
while ((bytesRead = inStream.Read(buf, 0, 4096)) > 0)
outStream.Write(buf, 0, bytesRead);
// clean up
outStream.Flush();
outStream.Close();
inStream.Close();
}
}
}
illustrates use of FileStreams
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example15_10.cs illustrates use of FileStreams
*/
using System;
using System.Windows.Forms;
using System.IO;
public class Example15_10
{
[STAThread]
public static void Main()
{
// use an open file dialog to get a filename
OpenFileDialog dlgOpen = new OpenFileDialog();
dlgOpen.Title="Select file to back up";
if (dlgOpen.ShowDialog() == DialogResult.OK)
{
FileStream inStream = File.OpenRead(dlgOpen.FileName);
FileStream outStream =
File.OpenWrite(dlgOpen.FileName + ".bak");
int b;
// copy all data from in to out
while ((b = inStream.ReadByte()) > -1)
outStream.WriteByte( (byte) b);
// clean up
outStream.Flush();
outStream.Close();
inStream.Close();
}
}
}
illustrates reading and writing binary data
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example15_15.cs illustrates reading and writing binary data
*/
using System;
using System.IO;
public class Example15_15
{
public static void Main()
{
// create a new file to work with
FileStream outStream = File.Create("c:BinaryTest.dat");
// use a BinaryWriter to write formatted data to the file
BinaryWriter bw = new BinaryWriter(outStream);
// write various data to the file
bw.Write( (int) 32);
bw.Write( (decimal) 4.567);
string s = "Test String";
bw.Write(s);
// flush and close
bw.Flush();
bw.Close();
// now open the file for reading
FileStream inStream = File.OpenRead("c:BinaryTest.dat");
// use a BinaryReader to read formatted data and dump it to the screen
BinaryReader br = new BinaryReader(inStream);
int i = br.ReadInt32();
decimal d = br.ReadDecimal();
string s2 = br.ReadString();
Console.WriteLine(i);
Console.WriteLine(d);
Console.WriteLine(s2);
// clean up
br.Close();
}
}
Create StreamReader from File Stream
using System;
using System.IO;
public class IOExample
{
static void Main() {
FileStream fs;
StreamReader srIn;
try {
fs = new FileStream("practice.txt", FileMode.Open );
srIn = new StreamReader(fs);
string line = srIn.ReadLine();
Console.WriteLine("line = "+line);
srIn.Close();
} catch (IOException ioe) {
Console.WriteLine("IOException occurred: "+ioe.Message);
}
}
}
Reads strings from a file created in a text editor
using System;
using System.IO;
public class FileReadStrings {
public static void Main( ) {
String line;
StreamReader f = new StreamReader("test.data");
while((line=f.ReadLine()) != null)
Console.WriteLine(line);
f.Close();
}
}
//File: test.data
/*
4665 Street|Toronto|ON|90048
*/