Reputation: 17
I looked online for a simple example of file reading and writing. Being a python graduate and moving into C# was a big step for me, but I do really love the language.
I've seen people do file I/O with StringReader, FileSystem, and I hear that Stream is also used. As a beginner to the language, one who doesn't know too many complicated concepts (but has the basic stuff down, methods, classes, functions) how would I go about reading a file correctly? I see people using the "using" keyword, and then treating it like a class or method. I have no clue what thats all about. I'm looking for something like
FileSystem() f = file.read("test.txt")
f.DoStuff()
In python, it was really easy to use a dictionary stored in another file. All I had to do was:
import Dict.py
# Now i have access to all of Dict's dictionaries. We'll say the name of one
# of the dictioanries is "f".
print Dict.f[1] # Or whatever. Now i can use the dictionary f like it was local.
My goal is to make a hangman game. I'm trying to pick a random word from a list of words (English dictionary) and have them guess that word, but I don't know how to do such things like read the file, and import its contents. Again, as simple as you can make it so a beginner can understand would be really helpful. Thanks a million!
Upvotes: 0
Views: 354
Reputation: 292565
Here are some of the most important classes to do IO in C#:
Raw binary IO
Stream
: it's the base class for all classes that represent a sequential stream of bytes. The stream can be read-only, write-only or read-write. It provides methods like Read
, Write
and Seek
. Keep in mind that a stream is used to access raw, binary data; depending on what you want to do, it might not be convenient to use it directly.
FileStream
: a stream to read and/or write data in a file.MemoryStream
: a stream to read and write data in memory.Text IO
TextReader
: it's the base class for all classes used to read a sequence of characters. It provides methods like Read
, ReadLine
, and ReadToEnd
.
StreamReader
: a TextReader
that reads text from a Stream
. A StreamReader
always has an Encoding
(UTF-8, ASCII, etc) that specifies how to convert the binary data from the stream into text.StringReader
: a TextReader
that reads text from a String
(useful to process an in-memory string as if it was the content of a file)TextWriter
: it's the base class for all classes used to write a sequence of characters. It provides methods like Write
and WriteLine
.
StreamWriter
: a TextWriter
that writes text to a Stream
. Like StreamReader
, it has an Encoding
that specifies how to convert the text to binary data.StringWriter
: a TextWriter that writes text to a StringBuilder
.The .NET framework also provides a helper class named File
for simplified IO operations:
ReadAllBytes
method returns all the content of a file as an array of bytesReadAllText
method returns all the content of a file as a stringReadAllLines
method returns all the lines of text from a file as an array of stringsWriteAllBytes
method writes the specified array of bytes to a file (replacing the existing content)WriteAllText
method writes the specified string to a file (replacing the existing content)WriteAllLines
method writes the specified lines of text to a file (replacing the existing content)For more details, read the documentation of the System.IO
namespace on MSDN
Upvotes: 2
Reputation: 101701
C#
has nice built-in methods that allows you to easily read & manipulate file contents. For example if you want to read all the lines from a file you can simply use File.ReadAllLines
method:
var lines = File.ReadAllLines("your file path");
If you want to exclude some lines, you could use File.ReadLines
method with Linq
:
var filteredLines = File.ReadLines("your file path")
.Where(line => yourCondition)
.ToArray();
Those are just two simple example. See the documentation for more details:
Upvotes: 1