Lec15 - Lecture 15: File I/O PIC 10C Todd Wittman Text File...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Lecture 15: File I/O PIC 10C Todd Wittman Text File Classes b The classes for file I/O are not automatically included. You should add: using System.IO; b Important IO classes: s File -- used for manipulating files (open, close, delete) s FileStream -- used to open a IO stream for reading/writing files with the StreamReader/StreamWriter classes s StreamReader -- reads bytes from specified stream s StreamWriter -- writes bytes to specified stream s FileSystemWatcher -- notifies you when a file is changed
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 File I/O b The basic steps for reading or writing a file are: b 1. Open a file stream. We have to give the full path of the file name. Note we put @ at the front to get the raw string and ignore escape characters. Without full path, assumes it's in project folder. FileStream myStream = new FileStream(@"C:\myfile.txt", FileMode.Open); b 2. Create a StreamReader (or StreamWriter) StreamReader myReader = new StreamReader(myStream); StreamWriter myWriter = new StreamWriter(myStream); b 3. Read (or write) your text. while (!myReader.EndOfStream) myWriter.WriteLine("Hello!"); myString = myReader.ReadLine(); b 4. Close the file stream when done. Flush the output writer. myStream.Close(); myWriter.Flush(); File I/O Example b Ex Copy the file input.txt into output.txt. FileStream fileToRead = new FileStream(@"C:\input.txt, FileMode.Open);
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 6

Lec15 - Lecture 15: File I/O PIC 10C Todd Wittman Text File...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online