1. Operation methods in 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 tube with length 1024 byte[] bbuf = new byte[1024]; //Use a loop to repeat the "water withdrawal" process while((hasRead = fis.read(bbuf))>0) { //Take out the "bamboo tube" (bytes), convert the byte array into a string and enter 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 a "bamboo tube" with length 32 char[] cbuf = new char[32]; //Use a loop to repeat the "water withdrawal" process while((hasRead = fr.read(cbuf))>0) { //Take out the "bamboo tube" (bytes), convert the byte array into a string and enter 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#:
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ /// <summary> /// Convert Stream to byte[] /// </summary> public byte[] StreamToBytes(Stream stream) { byte[] bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); // Set the current position of the stream to the beginning 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; } /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - * Conversion between Stream and file* - - - - - - - - - - - - - - - - - - - - - - - - - * / // // <summary> /// Write 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); // Set the current stream position to the beginning of the stream stream.Seek(0, SeekOrigin.Begin); // Write byte[] to the file FileStream fs = new FileStream(fileName, FileMode.Create); BinaryWriter bw = new BinaryWriter(fs); bw.Write(bytes); bw.Close(); fs.Close(); } /// <summary> /// Read Stream from the file /// </summary> public Stream FileToStream(string fileName) { // Open the 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[] to Stream Stream stream = new MemoryStream(bytes); return stream; }The above is the full content of the Java and C# input and output stream methods (detailed explanation) brought to you by the editor. I hope everyone will support Wulin.com more~