filestream 详细介绍参考
using (filestream fs = new filestream("file.filestream", filemode.create, fileaccess.write)) { for (int i = 0; i < cycles; i++) { for (int j = 0; j < length; j++) { dis[j] = i * length + j; } buffer.blockcopy(dis, 0, bydata, i * readcount, readcount); } fs.write(bydata, 0, bydata.length); }
using (filestream fs = new filestream("file.filestream", filemode.open, fileaccess.read)) { for (int i = 0; i < cycles; i++) { fs.seek(i * readcount, seekorigin.begin); fs.read(bydata, 0, readcount); dis = new double[length]; buffer.blockcopy(bydata, i * readcount, dis, 0, readcount); } }
using (binarywriter bw = new binarywriter(file.open("file.binary", filemode.create))) { byte[] data = new byte[cycles * readcount]; for (int i = 0; i < cycles; i++) { for (int j = 0; j < length; j++) { dis[j] = i * length + j; } buffer.blockcopy(dis, 0, data, i * readcount, readcount); } bw.write(data); }
using (binaryreader wr = new binaryreader(file.open("file.binary", filemode.open))) { for (int i = 0; i < cycles; i++) { var readdata = wr.readbytes(readcount); dis = new double[length]; buffer.blockcopy(readdata, 0, dis, 0, readcount); } }
using (streamwriter sw = new streamwriter("file.stream", false, encoding.getencoding("utf-16"))) { stringbuilder sb = new stringbuilder(); for (int i = 0; i < cycles; i++) { for (int j = 0; j < length; j++) { dis[j] = i * length + j; sb.appendformat("{0},", dis[j]); } sb.appendformat("\n"); } sw.writeline(sb); }
using (streamreader sd = new streamreader("file.stream", encoding.getencoding("utf-16"))) { for (int i = 0; i < cycles; i++) { string[] ch = sd.readline().split(new char[] { ',' }, system.stringsplitoptions.removeemptyentries); for (int j = 0; j < length; j++) { double.tryparse(ch[j], out dis[j]); } } }
class program { static void main() { filereadandwrite.binarywritermethod(); filereadandwrite.binaryreadermethod(); filereadandwrite.filestreamwritermethod(); filereadandwrite.filestreamreadmethod(); filereadandwrite.streamwritermethod(); filereadandwrite.streamreadermethod(); console.readkey(true); } }
class filereadandwrite { private const int length = 1024; private const int cycles = 64; private int readcount; private byte[] bydata; private double[] dis; public filereadandwrite() { readcount = length * sizeof(double); dis = new double[length]; bydata = new byte[cycles * length * sizeof(double)]; } #region binarywriter\binaryreader public void binarywritermethod() { using (binarywriter bw = new binarywriter(file.open("file.binary", filemode.create))) { byte[] data = new byte[cycles * readcount]; for (int i = 0; i < cycles; i++) { for (int j = 0; j < length; j++) { dis[j] = i * length + j; } buffer.blockcopy(dis, 0, data, i * readcount, readcount); } bw.write(data); } } public void binaryreadermethod() { using (binaryreader wr = new binaryreader(file.open("file.binary", filemode.open))) { for (int i = 0; i < cycles; i++) { var readdata = wr.readbytes(readcount); buffer.blockcopy(readdata, 0, dis, 0, readcount); } } } #endregion #region filestream read\write public void filestreamwritermethod() { using (filestream fs = new filestream("file.filestream", filemode.create,fileaccess.write)) { for (int i = 0; i < cycles; i++) { for (int j = 0; j < length; j++) { dis[j] = i * length + j; } buffer.blockcopy(dis, 0, bydata, i * readcount, readcount); } fs.write(bydata, 0, bydata.length); } } public void filestreamreadmethod() { using (filestream fs = new filestream("file.filestream", filemode.open, fileaccess.read)) { for (int i = 0; i < cycles; i++) { fs.seek(i * readcount, seekorigin.begin); fs.read(bydata, 0, readcount); buffer.blockcopy(bydata, i * readcount, dis, 0, readcount); } } } #endregion #region streamwriter\streamreader public void streamwritermethod() { using (streamwriter sw = new streamwriter("file.stream", false, encoding.getencoding("utf-16"))) { stringbuilder sb = new stringbuilder(); for (int i = 0; i < cycles; i++) { for (int j = 0; j < length; j++) { dis[j] = i * length + j; sb.appendformat("{0},", dis[j]); } sb.appendformat("\n"); } sw.writeline(sb); } } public void streamreadermethod() { using (streamreader sd = new streamreader("file.stream", encoding.getencoding("utf-16"))) { for (int i = 0; i < cycles; i++) { string[] ch = sd.readline().split(new char[] { ',' }, system.stringsplitoptions.removeemptyentries); for (int j = 0; j < length; j++) { double.tryparse(ch[j], out dis[j]); } } } } #endregion }
如对本文有疑问, 点击进行留言回复!!
C# 对PDF文档加密、解密(基于Spire.Cloud.SDK for .NET)
Unity通用泛型单例设计模式(普通型和继承自MonoBehaviour)
网友评论