Java and C # input and output stream method (detailed) _java

Source: Internet
Author: User

How to operate in 1,java:

Import java.io.*;  
public class Fileinputstreamtest  
{public  
  static void Main (string[] args) throws IOException  
  {  
    // Create byte input stream  
    FileInputStream fis = new FileInputStream ("Fileinputstreamtest.java");  
    Create a bamboo byte[with a length of 1024  
    ] bbuf = new byte[1024];  
    The number of bytes to save the actual read  
    int hasread = 0;  
    Use loops to repeat the process of fetching while  
    ((Hasread = Fis.read (bbuf)) >0)  
    {  
      //Remove "bamboo" (bytes), and convert 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 character input stream  
      fr = new FileReader ("Filereadertest.java");  
      Create a length of 32 "Bamboo tube"  
      char[] cbuf = new CHAR[32];  
      The number of characters used to save the actual read  
      int hasread = 0;  
      Use loops to repeat the process of fetching while  
      ((Hasread = Fr.read (cbuf)) >0)  
      {  
        //Remove "bamboo" (bytes), and convert a byte array into a string input  
        System.out.println (New String (Cbuf,0,hasread));  
      }  
    catch (IOException IoE)  
    {  
      ioe.printstacktrace ();  
    }  
    Finally  
    {  
      ///close file input stream  
      if (fr!= null)  
      {  
        fr.close ();  
      }  
}}}   

How to operate in 2,c#:

*--------------------------------------------------------------------- --*////<summary>///convert stream to byte[]///</summary> public byte[] Streamtobytes (Stream Strea m) {byte[] bytes = new Byte[stream.  
  Length]; Stream. Read (bytes, 0, bytes.  
  Length); Sets the position of the current stream as the start stream of the stream.  
  Seek (0, seekorigin.begin);  
return bytes;  
  ///<summary>///convert byte[] to Stream///</summary> public Stream bytestostream (byte[] bytes) {  
  Stream stream = new MemoryStream (bytes);  
return stream;  }/*-------------------------------------------------------------------  --*////<summary>///writes stream to file///</summary> public void Streamtofile (stream stream,string FileName) {//convert Stream to byte[] byte[] bytes = new Byte[stream.  
  Length]; Stream. Read (bytes, 0, bytes.  
  Length); Sets the current stream's position as the start of the stream strEam.  
  Seek (0, seekorigin.begin);  
  Write byte[] to file FileStream fs = new FileStream (FileName, FileMode.Create);  
  BinaryWriter bw = new BinaryWriter (FS); Bw.  
  Write (bytes); Bw.  
  Close (); Fs.  
Close ();        
  ///<summary>///reads stream///</summary> public stream filetostream from file (string fileName) {  
  Open file FileStream FileStream = new FileStream (FileName, FileMode.Open, FileAccess.Read, FileShare.Read);  
  Read the file byte[] byte[] bytes = new Byte[filestream.length]; FileStream.Read (bytes, 0, bytes.  
  Length);  
  Filestream.close ();  
  Convert byte[] into stream stream stream = new MemoryStream (bytes);  
return stream;   }

The above is a small series for everyone to bring Java and C # input and Output flow method (detailed) All content, I hope that we support cloud-Habitat Community ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.