Java and C input and output stream methods of in detail

  • 2020-05-12 02:34:41
  • OfStack

1. Operation method in Java:


import java.io.*;  
public class FileInputStreamTest  
{  
  public static void main(String[] args) throws IOException  
  {  
    // Create a byte input stream   
    FileInputStream fis = new FileInputStream("FileInputStreamTest.java");  
    // create 1 A length of 1024 The bamboo tube   
    byte[] bbuf = new byte[1024];  
    // To save the number of bytes actually read   
    int hasRead = 0;  
    // Use a cycle to repeat the "water draw" process   
    while((hasRead = fis.read(bbuf))>0)  
    {  
      // Take out the " Bamboo tube " In the ( byte ), Converts a byte array into a string input   
      System.out.println(new String(bbuf,0,hasRead));  
    }  
    fis.close();  
  }  
}  

import java.io.*;  
public class FileReaderTest  
{  
  public static void main(String[] args) throws IOException   
  {  
    FileReader fr = null;  
    try  
    {  
      // Create a character input stream   
      fr = new FileReader("FileReaderTest.java");  
      // create 1 A length of 32 the " Bamboo tube "  
      char[] cbuf = new char[32];  
      // To save the number of characters actually read   
      int hasRead = 0;  
      // Use a cycle to repeat the "water draw" process   
      while((hasRead = fr.read(cbuf))>0)  
      {  
        // Take out the " Bamboo tube " In the ( byte ), Converts a byte array into a string input   
        System.out.println(new String(cbuf,0,hasRead));  
      }  
    }  
    catch (IOException ioe)  
    {  
      ioe.printStackTrace();  
    }  
    finally  
    {  
      // Close the file input stream   
      if(fr != null)  
      {  
        fr.close();  
      }  
    }  
  }  
}   

2. Operation method in C# :


/* - - - - - - - - - - - - - - - - - - - - - - - -  
 * Stream  and  byte[]  Conversion between  
 * - - - - - - - - - - - - - - - - - - - - - - - */  
/// <summary>  
///  will  Stream  into  byte[]  
/// </summary>  
public byte[] StreamToBytes(Stream stream)  
{  
  byte[] bytes = new byte[stream.Length];  
  stream.Read(bytes, 0, bytes.Length);  
  //  Sets the position of the current flow to the beginning of the flow   
  stream.Seek(0, SeekOrigin.Begin);  
  return bytes;  
}  
/// <summary>  
///  will  byte[]  into  Stream  
/// </summary>  
public Stream BytesToStream(byte[] bytes)  
{  
  Stream stream = new MemoryStream(bytes);  
  return stream;  
}  
 
/* - - - - - - - - - - - - - - - - - - - - - - - -  
 * Stream  and   Conversion between files  
 * - - - - - - - - - - - - - - - - - - - - - - - */  
/// <summary>  
///  will  Stream  Written to the file   
/// </summary>  
public void StreamToFile(Stream stream,string fileName)  
{  
  //  the  Stream  Converted to  byte[]  
  byte[] bytes = new byte[stream.Length];  
  stream.Read(bytes, 0, bytes.Length);  
  //  Sets the position of the current flow to the beginning of the flow   
  stream.Seek(0, SeekOrigin.Begin);  
  //  the  byte[]  Written to the file   
  FileStream fs = new FileStream(fileName, FileMode.Create);  
  BinaryWriter bw = new BinaryWriter(fs);  
  bw.Write(bytes);  
  bw.Close();  
  fs.Close();  
}  
/// <summary>  
///  Read from file  Stream  
/// </summary>  
public Stream FileToStream(string fileName)  
{        
  //  Open the file   
  FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);  
  //  File reading  byte[]  
  byte[] bytes = new byte[fileStream.Length];  
  fileStream.Read(bytes, 0, bytes.Length);  
  fileStream.Close();  
  //  the  byte[]  Converted to  Stream  
  Stream stream = new MemoryStream(bytes);  
  return stream;  
}  

Related articles: