B
beginwithl
hi
1)
From MSDN site:
“FileStream buffers input and output for better performance.”
a) So essentially, FileStream is buffered class?! But if that is the
case, then why do bytes get written to the underlying file
immediately? In other words, even if we never call Flush() or Close(),
bytes do get written to a file?
b) Does FileStream also buffer input bytes? If so, then what exactly
does that mean? That bytes may not get immediately read into, say, byte
[] variable?
c) Does Flush() also work when reading from a file? Meaning it somehow
forces bytes buffered inside FileStream to get written into variable?
2)
a) StreamReader also buffers data?
b) I assume StreamWriter also buffers data internally, waiting for a
buffer to be full before it gets written to the underlying stream?
3) But if both StreamWriter and FileReader already buffer data
internally, then why would we also need special classes for
buffering?
4) I assume that if WriteStream ( or any other stream buffer ) object
buffers data, then data will only get written to a file when we either
call Flush(), Close() or when internal buffer is full?
5)
a) Does OS( or its disk buffer) also buffer data from our streams?
b) If so, then I assume that calling Flush() from any of the I/O
objects ( FileStream or StreamWriter etc ) forces the underlying OS
buffer to flush its contets also?
c) Is it possible that we would write some text to a disk ( via
StreamWriter class), but since we’d never call Flush() or Close() on
that stream, that this text would get buffered by a disk buffer, which
would only write it to a file at some later time ( perhaps when our
app has already stopped )?
6)
static void Main()
{
FileStream fs = new FileStream(@"D:\test.txt",
FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
StreamWriter sw1 = new StreamWriter(fs);
sw.Write("sw");
sw1.Write("sw1");
fs.Close();
sw.Close(); // exception
sw1.Close(); // exception
}
a) It seems that closing ‘fs’ doesn’t automatically close its wrapping
classes ‘sw’ and ‘sw1’ ( I’m assuming this because if ‘sw’ and ‘sw1’
were also closed, then the two would flush their buffered data to the
underlying stream )? So how do we close ‘sw1’ and ‘sw’, if closing
them ( when we already closed underlying stream ) causes “cannot
access closed file” exception to be thrown? We could, instead of
closing ‘fs’ directly, close one of its wrapper classes, say ‘sw’. But
we’d still need to somehow close ‘sw1’?!
7) What does closing a stream mean? Just that OS releases the
resources previouslly occupied by a stream ( lock on files, etc )?
thank you
1)
From MSDN site:
“FileStream buffers input and output for better performance.”
a) So essentially, FileStream is buffered class?! But if that is the
case, then why do bytes get written to the underlying file
immediately? In other words, even if we never call Flush() or Close(),
bytes do get written to a file?
b) Does FileStream also buffer input bytes? If so, then what exactly
does that mean? That bytes may not get immediately read into, say, byte
[] variable?
c) Does Flush() also work when reading from a file? Meaning it somehow
forces bytes buffered inside FileStream to get written into variable?
2)
a) StreamReader also buffers data?
b) I assume StreamWriter also buffers data internally, waiting for a
buffer to be full before it gets written to the underlying stream?
3) But if both StreamWriter and FileReader already buffer data
internally, then why would we also need special classes for
buffering?
4) I assume that if WriteStream ( or any other stream buffer ) object
buffers data, then data will only get written to a file when we either
call Flush(), Close() or when internal buffer is full?
5)
a) Does OS( or its disk buffer) also buffer data from our streams?
b) If so, then I assume that calling Flush() from any of the I/O
objects ( FileStream or StreamWriter etc ) forces the underlying OS
buffer to flush its contets also?
c) Is it possible that we would write some text to a disk ( via
StreamWriter class), but since we’d never call Flush() or Close() on
that stream, that this text would get buffered by a disk buffer, which
would only write it to a file at some later time ( perhaps when our
app has already stopped )?
6)
static void Main()
{
FileStream fs = new FileStream(@"D:\test.txt",
FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
StreamWriter sw1 = new StreamWriter(fs);
sw.Write("sw");
sw1.Write("sw1");
fs.Close();
sw.Close(); // exception
sw1.Close(); // exception
}
a) It seems that closing ‘fs’ doesn’t automatically close its wrapping
classes ‘sw’ and ‘sw1’ ( I’m assuming this because if ‘sw’ and ‘sw1’
were also closed, then the two would flush their buffered data to the
underlying stream )? So how do we close ‘sw1’ and ‘sw’, if closing
them ( when we already closed underlying stream ) causes “cannot
access closed file” exception to be thrown? We could, instead of
closing ‘fs’ directly, close one of its wrapper classes, say ‘sw’. But
we’d still need to somehow close ‘sw1’?!
7) What does closing a stream mean? Just that OS releases the
resources previouslly occupied by a stream ( lock on files, etc )?
thank you