Java和C#輸入輸出流的方法(詳解)
更新時(shí)間:2016年10月22日 09:31:50 投稿:jingxian
下面小編就為大家?guī)?lái)一篇Java和C#輸入輸出流的方法(詳解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
1,Java中操作方法:
import java.io.*; public class FileInputStreamTest { public static void main(String[] args) throws IOException { //創(chuàng)建字節(jié)輸入流 FileInputStream fis = new FileInputStream("FileInputStreamTest.java"); //創(chuàng)建一個(gè)長(zhǎng)度為1024的竹筒 byte[] bbuf = new byte[1024]; //用于保存實(shí)際讀取的字節(jié)數(shù) int hasRead = 0; //使用循環(huán)來(lái)重復(fù)“取水”的過(guò)程 while((hasRead = fis.read(bbuf))>0) { //取出"竹筒"中(字節(jié)),將字節(jié)數(shù)組轉(zhuǎn)成字符串輸入 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 { //創(chuàng)建字符輸入流 fr = new FileReader("FileReaderTest.java"); //創(chuàng)建一個(gè)長(zhǎng)度為32的"竹筒" char[] cbuf = new char[32]; //用于保存實(shí)際讀取的字符數(shù) int hasRead = 0; //使用循環(huán)來(lái)重復(fù)“取水”的過(guò)程 while((hasRead = fr.read(cbuf))>0) { //取出"竹筒"中(字節(jié)),將字節(jié)數(shù)組轉(zhuǎn)成字符串輸入 System.out.println(new String(cbuf,0,hasRead)); } } catch (IOException ioe) { ioe.printStackTrace(); } finally { //關(guān)閉文件輸入流 if(fr != null) { fr.close(); } } } }
2,C#中操作方法:
/* - - - - - - - - - - - - - - - - - - - - - - - - * Stream 和 byte[] 之間的轉(zhuǎn)換 * - - - - - - - - - - - - - - - - - - - - - - - */ /// <summary> /// 將 Stream 轉(zhuǎn)成 byte[] /// </summary> public byte[] StreamToBytes(Stream stream) { byte[] bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); // 設(shè)置當(dāng)前流的位置為流的開(kāi)始 stream.Seek(0, SeekOrigin.Begin); return bytes; } /// <summary> /// 將 byte[] 轉(zhuǎn)成 Stream /// </summary> public Stream BytesToStream(byte[] bytes) { Stream stream = new MemoryStream(bytes); return stream; } /* - - - - - - - - - - - - - - - - - - - - - - - - * Stream 和 文件之間的轉(zhuǎn)換 * - - - - - - - - - - - - - - - - - - - - - - - */ /// <summary> /// 將 Stream 寫(xiě)入文件 /// </summary> public void StreamToFile(Stream stream,string fileName) { // 把 Stream 轉(zhuǎn)換成 byte[] byte[] bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); // 設(shè)置當(dāng)前流的位置為流的開(kāi)始 stream.Seek(0, SeekOrigin.Begin); // 把 byte[] 寫(xiě)入文件 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) { // 打開(kāi)文件 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[] 轉(zhuǎn)換成 Stream Stream stream = new MemoryStream(bytes); return stream; }
以上就是小編為大家?guī)?lái)的Java和C#輸入輸出流的方法(詳解)全部?jī)?nèi)容了,希望大家多多支持腳本之家~
您可能感興趣的文章:
- Java IO之字節(jié)輸入輸出流詳解
- 圖文詳解Java中的字節(jié)輸入與輸出流
- Java中的字節(jié),字符輸出流與字節(jié)和字符輸入流的簡(jiǎn)單理解
- JAVA輸出流與輸入流代碼實(shí)例
- Java字節(jié)流 從文件輸入輸出到文件過(guò)程解析
- Java輸入輸出流實(shí)例詳解
- Java輸入/輸出流體系詳解
- java 對(duì)象輸入輸出流讀寫(xiě)文件的操作實(shí)例
- 淺析Java.IO輸入輸出流 過(guò)濾流 buffer流和data流
- Java輸入輸出流復(fù)制文件所用時(shí)間對(duì)比
- Java實(shí)現(xiàn)帶緩沖的輸入輸出流
相關(guān)文章
Java Gradle項(xiàng)目中的資源正確獲取方式
這篇文章主要介紹了Java Gradle項(xiàng)目中的資源正確獲取方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11java?使用BeanFactory實(shí)現(xiàn)service與dao層解耦合詳解
這篇文章主要介紹了java?使用BeanFactory實(shí)現(xiàn)service與dao層解耦合詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12SpringBoot集成Swagger構(gòu)建api文檔的操作
這篇文章主要介紹了SpringBoot集成Swagger構(gòu)建api文檔的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12springboot?集成easy-captcha實(shí)現(xiàn)圖像驗(yàn)證碼顯示和登錄
本文主要介紹了springboot?集成easy-captcha實(shí)現(xiàn)圖像驗(yàn)證碼顯示和登錄,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04詳解Java8的forEach(...)如何提供index值
這篇文章主要介紹了詳解Java8的forEach(...)如何提供index值,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03