C # stream files (Stream & File & byte [])

Original: https: //www.cnblogs.com/long-gengyun/archive/2010/03/28/1698681.html

 

  • File Overview 
In operation of the performance of the file stream, i.e. the stream is the sequence of bytes read from a number of inputs.

Press information file on the external memory can be divided according to coding text files and binary files .

 

Stream class is a class System.IO namespace in System.IO namespace contains all allow synchronous and asynchronous reading and writing of the data stream and based on the file, following a brief commonly used classes.

1. Directory class: contains static methods for all directory operations, such as creating a directory, move, copy, delete, etc.

2. DirectoryInfo categories: contains instance method all directory operations, such as various directory attributes (name, creation time), various operations directory (created directory exists, move, delete, etc.)

3. File types: a typical file operations, provide text to create, open, copy, delete, move, and other static methods. It can also be used to obtain basic information files and settings files.

4. FileInfo categories: Typical file operations, provide text to create, open, copy, delete, move, etc. Examples of methods. When the file needs to be reused many times, using the FileInfo class provides instance methods, static methods can not use the File provided.

5. FileStream class: This class implements the file to read, write, open, close operation, supports random access files, may be used to open files for reading and writing in a synchronized manner and to be opened for reading and writing a file asynchronously.

6. Path class: the class of the String instance that contains the file or directory path information of the operations that can be cross-platform mode of execution.

7. MemoryStream class: This class is created from the storage area to support memory stream.

8. StreamReader class: class can read the contents of a standard text file. Namely to achieve a TextReader. Default encoding format is UTF-8.

9. StreamWriter class: class can be written to the standard text file content. Namely to achieve a TextWriter. Default encoding format is UTF-8.

10. StringReader class: This class implements a character string read from TextReader.

11. StringWriter class: This class implements a string write information to, the information stored in the base StringBuilder.

12. TextReader Class: This class represents a continuous read character reader system.

13. TextWriter class: This class represents can write a series of ordered character editor, is an abstract class. 

 

 Code sample: Creating the content of the input and output files

 

if(File.Exists(filePath))
{
    File.Delete(filePath);
}
FS the FileStream = File.Create (filePath, 1024 );      // create the file 
Byte [] info = new new UTF8Encoding ( to true ) .GetBytes ( " test content " );
fs.Write (info, 0 , info.Length);      // write to the newly created file 
fs.Close ();

using(StreamReader sr=File.OpenText(filePath))
{
    While(sr.ReadLine()!=null)
        MessageBox.Show(sr.ReadLine());
}

 

Reference Code:

Acquisition and setting file attributes: /Files/long-gengyun/FileAttribute.rar

Display files in the folder with subfolders: /Files/long-gengyun/GetDirectory.rar

Print the output file: /Files/long-gengyun/FilePrint.rar

Computer systems access to information: /Files/long-gengyun/GetSystemInfo.rar 

 

A, byte [] and Stream

/// <summary>
/// byte[]转换成Stream
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public Stream BytesToStream(byte[] bytes)
{
    Stream stream = new MemoryStream(bytes);
    return stream;
}
 
/// <summary>
/// Stream转换成byte[]
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
public byte[] StreamToBytes(Stream stream)
{
    byte[] bytes = new byte[stream.Length];
    stream.Read(bytes, 0, bytes.Length);
    stream.Seek ( 0 , SeekOrigin.Begin); // set the start position of the current stream is a stream of 
    return bytes;
}

Second, files and Stream

///  <Summary> 
/// Read From File Stream, ideas file = - byte - stream
 ///  </ Summary> 
///  <param name = "path"> </ param> 
///  <Returns > </ Returns> 
public Stream FileToStream ( String path)
{
    FileStream the FileStream = new new the FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Read); // open file 
    byte [] bytes = new new  byte [fileStream.Length]; // read the file byte [] 
    FileStream.Read (bytes, 0 , bytes.Length);
    fileStream.Close();
    Stream Stream = new new the MemoryStream (bytes); // the byte [] is converted into Stream 
    return Stream;
}
 
///  <Summary> 
/// the idea to write the file Stream Flow = - byte file - write byte
 ///  </ Summary> 
///  <param name = "Stream"> </ param> 
// /  <param name = "path"> </ param> 
public  void StreamToFile (Stream Stream, String path)
{
    byte[] bytes = new byte[stream.Length]; // 把Stream转换成byte[]
    stream.Read(bytes, 0, bytes.Length);
    stream.Seek ( 0 , SeekOrigin.Begin); // set the start position of the current stream is a stream of 
    the FileStream FS = new new the FileStream (path, FileMode.Create); // the byte [] file write 
    the BinaryWriter BW = new new the BinaryWriter ( fs);
    bw.Write(bytes);
    bw.Close();
    fs.Close();
}

https://www.cnblogs.com/warioland/archive/2012/03/06/2381355.html

 

Three, BYTE and string

1
System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding();
  byte[] inputBytes =converter.GetBytes(inputString);
  string inputString = converter.GetString(inputBytes);

2   string inputString = System.Convert.ToBase64String(inputBytes);
  byte[] inputBytes = System.Convert.FromBase64String(inputString);
FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);

 

Guess you like

Origin www.cnblogs.com/icaowu/p/11628485.html