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)文章
Java Gradle項(xiàng)目中的資源正確獲取方式
這篇文章主要介紹了Java Gradle項(xiàng)目中的資源正確獲取方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
java?使用BeanFactory實(shí)現(xiàn)service與dao層解耦合詳解
這篇文章主要介紹了java?使用BeanFactory實(shí)現(xiàn)service與dao層解耦合詳解,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
SpringBoot集成Swagger構(gòu)建api文檔的操作
這篇文章主要介紹了SpringBoot集成Swagger構(gòu)建api文檔的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12
springboot?集成easy-captcha實(shí)現(xiàn)圖像驗(yàn)證碼顯示和登錄
本文主要介紹了springboot?集成easy-captcha實(shí)現(xiàn)圖像驗(yàn)證碼顯示和登錄,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
詳解Java8的forEach(...)如何提供index值
這篇文章主要介紹了詳解Java8的forEach(...)如何提供index值,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03

