Skip to content

Instantly share code, notes, and snippets.

@ChrisLundquist
Forked from NikkiBuck/gist:2915803
Created June 12, 2012 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChrisLundquist/2918213 to your computer and use it in GitHub Desktop.
Save ChrisLundquist/2918213 to your computer and use it in GitHub Desktop.
public void SavingFile()
{
StreamWriter writer = new StreamWriter("books.txt");
foreach (Book book in books)
{
writer.WriteLine(book.ToString());
}
}
public void LoadingFile()
{
const char DELIM = '\t';
string recordIn = "";
string[] part;
// Open the file
FileStream file = new FileStream("books.txt", FileMode.Open, FileAccess.Read);
StreamReader reader = new StreamReader(file);
// Read each line
while (reader.Peek() != -1)
{
Book book = new Book();
// Read this line
recordIn = reader.ReadLine();
// Split the line into fields
part = recordIn.Split(DELIM);
// Put the information in our book object
book.Isbn = new ISBN(part[0]);
book.LastName = part[1];
book.FirstName = part[2];
book.Title = part[3];
book.Year = Convert.ToInt32(part[4]);
book.Price = Convert.ToDouble(part[5]);
// if it is valid and it is unique
if (book.Valid() && NewBook(book))
{
// Add our book to our books array
books.Add(book)
}
}
// Close our file handles
reader.Close();
file.Close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment