Java 및 C# 출력 스트림 가져오기 방법(상세)

1, Java의 작동 방법:

import java.io.*;  
public class FileInputStreamTest  
{  
  public static void main(String[] args) throws IOException  
  {  
    //   
    FileInputStream fis = new FileInputStream("FileInputStreamTest.java");  
    // 1024   
    byte[] bbuf = new byte[1024];  
    //   
    int hasRead = 0;  
    // “ ”   
    while((hasRead = fis.read(bbuf))>0)  
    {  
      // " " ( ),   
      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  
    {  
      //   
      fr = new FileReader("FileReaderTest.java");  
      // 32 " "  
      char[] cbuf = new char[32];  
      //   
      int hasRead = 0;  
      // “ ”   
      while((hasRead = fr.read(cbuf))>0)  
      {  
        // " " ( ),   
        System.out.println(new String(cbuf,0,hasRead));  
      }  
    }  
    catch (IOException ioe)  
    {  
      ioe.printStackTrace();  
    }  
    finally  
    {  
      //   
      if(fr != null)  
      {  
        fr.close();  
      }  
    }  
  }  
}   
2, C#의 작업 방법:

/* - - - - - - - - - - - - - - - - - - - - - - - -  
 * Stream   byte[]   
 * - - - - - - - - - - - - - - - - - - - - - - - */  
/// <summary>  
///   Stream   byte[]  
/// </summary>  
public byte[] StreamToBytes(Stream stream)  
{  
  byte[] bytes = new byte[stream.Length];  
  stream.Read(bytes, 0, bytes.Length);  
  //    
  stream.Seek(0, SeekOrigin.Begin);  
  return bytes;  
}  
/// <summary>  
///   byte[]   Stream  
/// </summary>  
public Stream BytesToStream(byte[] bytes)  
{  
  Stream stream = new MemoryStream(bytes);  
  return stream;  
}  
 
/* - - - - - - - - - - - - - - - - - - - - - - - -  
 * Stream     
 * - - - - - - - - - - - - - - - - - - - - - - - */  
/// <summary>  
///   Stream    
/// </summary>  
public void StreamToFile(Stream stream,string fileName)  
{  
  //   Stream   byte[]  
  byte[] bytes = new byte[stream.Length];  
  stream.Read(bytes, 0, bytes.Length);  
  //    
  stream.Seek(0, SeekOrigin.Begin);  
  //   byte[]    
  FileStream fs = new FileStream(fileName, FileMode.Create);  
  BinaryWriter bw = new BinaryWriter(fs);  
  bw.Write(bytes);  
  bw.Close();  
  fs.Close();  
}  
/// <summary>  
///   Stream  
/// </summary>  
public Stream FileToStream(string fileName)  
{        
  //    
  FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);  
  //   byte[]  
  byte[] bytes = new byte[fileStream.Length];  
  fileStream.Read(bytes, 0, bytes.Length);  
  fileStream.Close();  
  //   byte[]   Stream  
  Stream stream = new MemoryStream(bytes);  
  return stream;  
}  
지금까지 여러분에게 가져온 Java와 C#입력 출력 흐름의 방법(상해)의 모든 내용입니다. 많은 응원 부탁드립니다~

좋은 웹페이지 즐겨찾기