Java和C#輸入輸出流的方法(詳解)
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)前流的位置為流的開始
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 寫入文件
/// </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)前流的位置為流的開始
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[] 轉(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ì)象輸入輸出流讀寫文件的操作實(shí)例
- 淺析Java.IO輸入輸出流 過(guò)濾流 buffer流和data流
- Java輸入輸出流復(fù)制文件所用時(shí)間對(duì)比
- Java實(shí)現(xiàn)帶緩沖的輸入輸出流
相關(guān)文章
Springboot整合GuavaCache緩存過(guò)程解析
這篇文章主要介紹了springboot整合GuavaCache緩存過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
SpringMVC 接收前端傳遞的參數(shù)四種方式小結(jié)
這篇文章主要介紹了SpringMVC 接收前端傳遞的參數(shù)四種方式小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
shiro實(shí)現(xiàn)單點(diǎn)登錄(一個(gè)用戶同一時(shí)刻只能在一個(gè)地方登錄)
這篇文章主要介紹了shiro實(shí)現(xiàn)單點(diǎn)登錄(一個(gè)用戶同一時(shí)刻只能在一個(gè)地方登錄)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧2016-08-08
解決window.location.href之后session丟失的問(wèn)題
今天小編就為大家分享一篇關(guān)于解決window.location.href之后session丟失的問(wèn)題,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
idea搭建mybatis環(huán)境配置全過(guò)程
本文介紹了如何以IDEA搭建MyBatis環(huán)境配置的方法,包括步驟和注意事項(xiàng),通過(guò)本文的介紹,可以輕松地以IDEA搭建MyBatis環(huán)境配置,提高開發(fā)效率2023-10-10
解決springboot整合cxf-jaxrs中json轉(zhuǎn)換的問(wèn)題
這篇文章主要介紹了解決springboot整合cxf-jaxrs中json轉(zhuǎn)換的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
SpringBoot事件機(jī)制相關(guān)知識(shí)點(diǎn)匯總
這篇文章主要介紹了SpringBoot事件機(jī)制相關(guān)知識(shí)點(diǎn)匯總,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09

