Java和C#輸入輸出流的方法(詳解)
更新時(shí)間:2016年10月22日 09:31:50 投稿:jingxian
下面小編就為大家?guī)硪黄狫ava和C#輸入輸出流的方法(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
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è)長度為1024的竹筒
byte[] bbuf = new byte[1024];
//用于保存實(shí)際讀取的字節(jié)數(shù)
int hasRead = 0;
//使用循環(huán)來重復(fù)“取水”的過程
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è)長度為32的"竹筒"
char[] cbuf = new char[32];
//用于保存實(shí)際讀取的字符數(shù)
int hasRead = 0;
//使用循環(huán)來重復(fù)“取水”的過程
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ī)淼腏ava和C#輸入輸出流的方法(詳解)全部內(nèi)容了,希望大家多多支持腳本之家~
相關(guān)文章
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丟失的問題
今天小編就為大家分享一篇關(guān)于解決window.location.href之后session丟失的問題,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12
解決springboot整合cxf-jaxrs中json轉(zhuǎn)換的問題
這篇文章主要介紹了解決springboot整合cxf-jaxrs中json轉(zhuǎn)換的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
SpringBoot事件機(jī)制相關(guān)知識(shí)點(diǎn)匯總
這篇文章主要介紹了SpringBoot事件機(jī)制相關(guān)知識(shí)點(diǎn)匯總,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09

