// Note:
// In computer programming, a stream is a Class Object. Input and Output operations on many files are provided by class member functions.
// The stream in the computer is actually a type of information conversion. It is an ordered stream. Therefore, compared to an object, we usually call input as an input stream for receiving external information from an object, and output from an object accordingly) the information is the output stream, which is also called the input/output stream (I/O streams ). When information or data is exchanged between objects, the object or data is always first converted to a certain form of stream, and then transferred to the target object. Therefore, we can regard a stream as a data carrier, through which we can exchange and transmit data.
// The concept of program streaming is different from that of the operating system streaming. The stream in the operating system can be regarded as a data stream, while the concept of program streaming is actually a class used to process the stream.
// Stream is a way to operate data in the memory
//. Net has a variety of stream types
// C # There are a lot of encapsulation classes that do not need to use streams
Test code:
Using system;
Using system. Collections. Generic;
Using system. text;
Namespace fileandfileinfo
{
Class Program
{
Static void main (string [] ARGs)
{
Console. writeline ("Enter saved directory ");
String sbasedir = console. Readline ();
Fileandfileinfo. listfile (sbasedir );
Console. writeline ("Enter create directory ");
String snewfile = console. Readline ();
Fileandfileinfo. creatfile (snewfile );
// Read the text file content in the directory
Fileandfileinfo. readflie (snewfile );
// Delete the specified directory
// Fileandfileinfo. delectfile (snewfile );
}
}
}
Class Code:
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. IO;
Namespace fileandfileinfo
{
Class fileandfileinfo
{
// List objects in the directory
Public static void listfile (string sdir)
{
If (directory. exists (sdir ))
{
// Use an absolute path to initialize an object that represents a directory
Directoryinfo di = new directoryinfo (sdir );
// Traverse the files under the directory (excluding subdirectories). Note that subdirectories are not included. This is very important, and subfolders are not displayed.
Console. writeline ("File Creation Time/T file name/T file size ");
// Foreach (fileinfo file in Di. getfiles) // The foreach statement cannot be executed on the method group. Do you want to call "method group?
Foreach (fileinfo file in Di. getfiles ())
Console. writeline (string. Format ("{0}/t {1}/T {2}", file. creationtime, file. Name, file. Length ));
}
Else
Console. writeline ("file {0} does not exist. ", Sdir );
}
// Create a text file. Note that it is a text file instead of a folder.
Public static void creatfile (string sfile)
{
// Obtain a file stream object for reading and writing files
Filestream FS = file. Create (sfile); // here, a text file with the specified name is created in the relevant directory.
// Obtain a stream editor pointing to a file
Streamwriter Sw = new streamwriter (FS); // here, the object of the volume stream that operates the text file is created.
// Write text
Console. writeline ("input text content ");
String STR = console. Readline ();
Sw. writeline (STR); // write the input text content to the previously created text file
// Close the object and release the resource
Sw. Close ();
FS. Close ();
Console. writeline (string. Format ("Create a text file >>{ 0}", sfile ));
}
// Read text files
Public static void readflie (string sfile)
{
String STR = "";
Streamreader sr = new streamreader (sfile );
STR = Sr. readtoend (); // here, the object of the response stream that operates the text file is created.
Sr. Close ();
Console. writeline (string. Format ("Read text files >>{ 0}", STR ));
}
// Delete an object
Public static void delectfile (string sfile)
{
File. Delete (sfile );
Console. writeline (string. Format ("delete file >>{ 0}", sfile ));
}
}
}