Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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