1,FileInfo类
类库定义对象File(静态方法类)
定义:dim fi as FileInfo
创建:fi = new FileInfo("strpath")
文件存在否:fi.exists
删除文件:fi.delete
返回流文件:fsm = fi.create();fsm = fi.open();fsm = fi.openwrite();fsm = fi.openread();
返回写流:smw = fi.appendtext();smw = fi.createtext();
返回读流:smr = fi.opentext();
2,FileStream类
定义:dim fsm as FileStream
创建:fsm = new FileStream("strpath")
存入基础设备:fsm.flush
使用结束:fsm.close
添加文本:AddText(fs, "This is some text")
读取文本:
Dim b(1024) As Byte
Dim temp As UTF8Encoding = New UTF8Encoding(True)
Do While fs.Read(b, 0, b.Length) > 0
Console.WriteLine(temp.GetString(b))
loop
3,StreamReader类
定义:dim smr as StreamReader
创建:smr = new StreamReader("strpath")
指向头:smr.basestream.seek(0,seekorigin.begin)
后移:smr.peek()
读字符:smr.read(str, 0, 1)
读行:smr.readline
读全文:smr.readToEnd()
使用结束:smr.close()
4,StreamWriter类
定义:dim smw as StreamWriter
创建:smw = StreamWriter("strpath");
写入字符:smw.write("achar")
写入行:smw.writeline("str")
存入磁盘…………
================================================================
using System;
using System.IO;
namespace csdn
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
int sz;
FileStream s=new FileStream(@"劳斯来斯2.jpg",FileMode.OpenOrCreate,FileAccess.Read);
sz=Convert.ToInt32(s.Length);
byte[] k=new byte[sz];
s.Read(k,0,sz);
s.Close();
FileStream x=new FileStream(@"f:\1.jpg",FileMode.Create,FileAccess.Write);
x.Write(k,0,sz);
x.Close();
}
}
}
========================================================
StreamReader reader=StreamReader(@"c:\text.txt");
string strReader=reader.ReadToEnd();
reader.Close();
ii += strReader;
=====================================
使用binarywriter写字符串,它会默认在字符串后面加 "\0",由实际的10个字节变为11个字节。建议直接写如byte数组。
int i = 10245679;
string str = t.ToString( "0000000000" );
byte[] buffer = System.Text.Encoding.Default.GetBytes( str );
FileStream fs = new FileStream( fileName , FileMode.Open , FileAccess.ReadAndWrite );
fs.Seek( 0 , SeekOrigin.End ); //当前fs的position再其文件末尾
fs.Write( buffer , 0 , buffer.Length ); //写入byte数组
fs.Close();
----------------------------------
读取:
FileStream fs = new FileStream( fileName , FileMode.Open , FileAccess.ReadAndWrite );
fs.Seek( -10 , SeekOrigin.End );
byte[] buffer = new byte[ 10 ];
fs.Read( buffer , 0 , 10 );
fs.Close();
string str = Encoding.Defauult.GetString( buffer );
=====================================================
/// <summary>
/// 同步票号
/// </summary>
private void ProWriteTxt()
{
string strLocalFileName=Directory.GetCurrentDirectory() + @"\baseinfo.txt";
string strInfo="";
StreamReader sr=new StreamReader((System.IO.Stream)File.OpenRead(strLocalFileName),System.Text.ASCIIEncoding.UTF8 ) ;
strInfo=sr.ReadLine();
sr.Close();
string[] strSubInfo=strInfo.Split('#');//读过程
strSubInfo[3]=this.lblPh.Text.ToString();//修改过程
strInfo = strSubInfo[0]+"#"+strSubInfo[1]+"#"+strSubInfo[2]+"#"+strSubInfo[3]+"#"+strSubInfo[4]+"#"+strSubInfo[5]+"#"+strSubInfo[6]+"#"+strSubInfo[7];
FileStream fs=new FileStream(strLocalFileName,FileMode.Truncate,FileAccess.Write);
StreamWriter sw=new StreamWriter(fs);
sw.WriteLine(strInfo);
sw.Close();
fs.Close();
}
我是用#分割的。
浙公网安备 33010602011771号