當前位置:首頁 » 文件管理 » 關閉文件流可以使用文件流

關閉文件流可以使用文件流

發布時間: 2024-05-09 07:51:04

❶ C# 濡備綍鍦ㄧ嚎紼嬩腑鍏抽棴鏂囦歡嫻侊紵

1.濡傛灉綰跨▼姝e湪浣跨敤,搴旇ュ厛鎸傝搗,鍦ㄥ叧闂,
2.鍏抽棴鏂囦歡嫻佸拰鍒鐨勪竴鏍,閮芥槸close
3.榪欎釜鏈夊氱嶅彲鑳,鍙鑳戒綘鐨勭▼搴忔病鍏,鍙鑳界嚎紼嬪湪榪愯,鍙鑳戒綘鐢ㄧ殑鏄鍐呭瓨緙撳啿寰鏂囦歡閲屽啓,娌℃湁Flush(); 涓涓

❷ java涓鏂囦歡鎵撳紑鍜屽叧闂鐨勯棶棰

JVM閫鍑烘椂浼氶噴鏀炬墍鏈夋枃浠剁殑鍙ユ焺錛岃繖涓鏂囦歡褰撶劧鏄琚鑷鍔ㄥ叧闂浜嗭紱濡傛灉浣犲湪鍐欏叆鏂囦歡鍚庯紝娌℃湁鍙婃椂璋冪敤flush()錛屽啓鍏ョ殑鍐呭逛笉浼氬埛鍏ョ佺洏錛屽氨涓㈠け浜.

❸ c璇璦涓,鎶婃枃浠剁湅浣滀竴緋誨垪鐨勪粈涔堟祦

璇ヨ$畻鏈虹紪紼嬩腑錛屾妸鏂囦歡鐪嬩綔涓緋誨垪鐨勫瓧鑺傛祦銆
C璇璦浣跨敤鏂囦歡嫻佺殑姒傚康鏉ュ勭悊鏂囦歡銆傛枃浠舵祦鏄涓縐嶆娊璞$殑姒傚康錛岃〃紺轟粠鏂囦歡璇誨彇鏁版嵁鎴栧悜鏂囦歡鍐欏叆鏁版嵁鐨勮繃紼嬨傞氳繃浣跨敤鏂囦歡嫻侊紝C璇璦紼嬪簭鍙浠ユ柟渚垮湴璇誨彇鍜屽啓鍏ユ枃浠躲
C璇璦涓鎻愪緵stdio.h澶存枃浠朵腑鐨勫嚱鏁幫紝鐢ㄤ簬鍒涘緩銆佹墦寮銆佽誨啓鍜屽叧闂鏂囦歡銆傝繖浜涘嚱鏁板彲浠ヤ笌鏂囦歡嫻佷竴璧蜂嬌鐢錛屼互鎵ц屽悇縐嶆枃浠舵搷浣溿備緥濡傦紝fopen錛堬級鍑芥暟鐢ㄤ簬鎵撳紑涓涓鏂囦歡騫惰繑鍥炰竴涓鏂囦歡嫻侊紝fread錛堬級鍑芥暟鐢ㄤ簬浠庢枃浠朵腑璇誨彇鏁版嵁錛宖write錛堬級鍑芥暟鐢ㄤ簬鍚戞枃浠朵腑鍐欏叆鏁版嵁錛宖close錛堬級鍑芥暟鐢ㄤ簬鍏抽棴鏂囦歡嫻併
鍥犳わ紝鍦–璇璦涓錛屾枃浠惰鐪嬩綔鏄涓緋誨垪瀛楄妭鐨勬祦錛岄氳繃浣跨敤鏂囦歡嫻佸拰鐩稿叧鍑芥暟錛屽彲浠ユ柟渚垮湴璇誨彇鍜屽啓鍏ユ枃浠躲

❹ c++涓濡備綍浣跨敤鏂囦歡嫻

C++鏂囦歡嫻
鍦–++涓錛屾湁涓涓猻tream榪欎釜綾伙紝鎵鏈夌殑I/O閮戒互榪欎釜鈥滄祦鈥濈被涓哄熀紜鐨勶紝鍖呮嫭鎴戜滑瑕佽よ瘑鐨勬枃浠禝/O錛宻tream榪欎釜綾繪湁涓や釜閲嶈佺殑榪愮畻絎︼細
1銆佹彃鍏ュ櫒(<<)
鍚戞祦杈撳嚭鏁版嵁銆傛瘮濡傝寸郴緇熸湁涓涓榛樿ょ殑鏍囧噯杈撳嚭嫻(cout)錛屼竴鑸鎯呭喌涓嬪氨鏄鎸囩殑鏄劇ず鍣錛屾墍浠ワ紝cout<<"Write
Stdout"<<'n';灝辮〃紺烘妸瀛楃︿覆"Write
Stdout"鍜屾崲琛屽瓧絎('n')杈撳嚭鍒版爣鍑嗚緭鍑烘祦銆
2銆佹瀽鍙栧櫒(>>)
浠庢祦涓杈撳叆鏁版嵁銆傛瘮濡傝寸郴緇熸湁涓涓榛樿ょ殑鏍囧噯杈撳叆嫻(cin)錛屼竴鑸鎯呭喌涓嬪氨鏄鎸囩殑閿鐩橈紝鎵浠ワ紝cin>>x;灝辮〃紺轟粠鏍囧噯杈撳叆嫻佷腑璇誨彇涓涓鎸囧畾綾誨瀷(鍗沖彉閲弜鐨勭被鍨)鐨勬暟鎹銆
鍦–++涓錛屽規枃浠剁殑鎿嶄綔鏄閫氳繃stream鐨勫瓙綾籪stream(file
stream)鏉ュ疄鐜扮殑錛屾墍浠ワ紝瑕佺敤榪欑嶆柟寮忔搷浣滄枃浠訛紝灝卞繀欏誨姞鍏ュご鏂囦歡fstream.h銆備笅闈㈠氨鎶婃ょ被鐨勬枃浠舵搷浣滆繃紼嬩竴涓閬撴潵銆
涓銆佹墦寮鏂囦歡
鍦╢stream綾諱腑錛屾湁涓涓鎴愬憳鍑芥暟open()錛屽氨鏄鐢ㄦ潵鎵撳紑鏂囦歡鐨勶紝鍏跺師鍨嬫槸錛
void
open(const
char*
filename,int
mode,int
access);
鍙傛暟錛
filename錛
瑕佹墦寮鐨勬枃浠跺悕
mode錛
瑕佹墦寮鏂囦歡鐨勬柟寮
access錛
鎵撳紑鏂囦歡鐨勫睘鎬
鎵撳紑鏂囦歡鐨勬柟寮忓湪綾籭os(鏄鎵鏈夋祦寮廔/O綾葷殑鍩虹被)涓瀹氫箟錛屽父鐢ㄧ殑鍊煎備笅錛
ios::app錛
浠ヨ拷鍔犵殑鏂瑰紡鎵撳紑鏂囦歡
ios::ate錛
鏂囦歡鎵撳紑鍚庡畾浣嶅埌鏂囦歡灝撅紝ios:app灝卞寘鍚鏈夋ゅ睘鎬
ios::binary錛
浠ヤ簩榪涘埗鏂瑰紡鎵撳紑鏂囦歡錛岀己鐪佺殑鏂瑰紡鏄鏂囨湰鏂瑰紡銆備袱縐嶆柟寮忕殑鍖哄埆瑙佸墠鏂
ios::in錛
鏂囦歡浠ヨ緭鍏ユ柟寮忔墦寮
ios::out錛
鏂囦歡浠ヨ緭鍑烘柟寮忔墦寮
ios::nocreate錛
涓嶅緩絝嬫枃浠訛紝鎵浠ユ枃浠朵笉瀛樺湪鏃舵墦寮澶辮觸
ios::noreplace錛氫笉瑕嗙洊鏂囦歡錛屾墍浠ユ墦寮鏂囦歡鏃跺傛灉鏂囦歡瀛樺湪澶辮觸
ios::trunc錛
濡傛灉鏂囦歡瀛樺湪錛屾妸鏂囦歡闀垮害璁句負0
鍙浠ョ敤鈥滄垨鈥濇妸浠ヤ笂灞炴ц繛鎺ヨ搗鏉ワ紝濡俰os::out|ios::binary
鎵撳紑鏂囦歡鐨勫睘鎬у彇鍊兼槸錛
0錛氭櫘閫氭枃浠訛紝鎵撳紑璁塊棶
1錛氬彧璇繪枃浠
2錛氶殣鍚鏂囦歡
4錛氱郴緇熸枃浠
鍙浠ョ敤鈥滄垨鈥濇垨鑰呪+鈥濇妸浠ヤ笂灞炴ц繛鎺ヨ搗鏉
錛屽3鎴1|2灝辨槸浠ュ彧璇誨拰闅愬惈灞炴ф墦寮鏂囦歡銆
渚嬪傦細浠ヤ簩榪涘埗杈撳叆鏂瑰紡鎵撳紑鏂囦歡c:config.sys
fstream
file1;
file1.open("c:config.sys",ios::binary|ios::in,0);
濡傛灉open鍑芥暟鍙鏈夋枃浠跺悕涓涓鍙傛暟錛屽垯鏄浠ヨ/鍐欐櫘閫氭枃浠舵墦寮錛屽嵆錛
file1.open("c:config.sys");<=>file1.open("c:config.sys",ios::in|ios::out,0);
鍙﹀栵紝fstream榪樻湁鍜宱pen()涓鏍風殑鏋勯犲嚱鏁幫紝瀵逛簬涓婁緥錛屽湪瀹氫箟鐨勬椂鍊欏氨鍙浠ユ墦寮鏂囦歡浜嗭細
fstream
file1("c:config.sys");
鐗瑰埆鎻愬嚭鐨勬槸錛宖stream鏈変袱涓瀛愮被錛歩fstream(input
file
stream)鍜宱fstream(outpu
file
stream)錛宨fstream榛樿や互杈撳叆鏂瑰紡鎵撳紑鏂囦歡錛岃宱fstream榛樿や互杈撳嚭鏂瑰紡鎵撳紑鏂囦歡銆
ifstream
file2("c:pdos.def");//浠ヨ緭鍏ユ柟寮忔墦寮鏂囦歡
ofstream
file3("c:x.123");//浠ヨ緭鍑烘柟寮忔墦寮鏂囦歡
鎵浠ワ紝鍦ㄥ疄闄呭簲鐢ㄤ腑錛屾牴鎹闇瑕佺殑涓嶅悓錛岄夋嫨涓嶅悓鐨勭被鏉ュ畾涔夛細濡傛灉鎯充互杈撳叆鏂瑰紡鎵撳紑錛屽氨鐢╥fstream鏉ュ畾涔夛紱濡傛灉鎯充互杈撳嚭鏂瑰紡鎵撳紑錛屽氨鐢╫fstream鏉ュ畾涔夛紱濡傛灉鎯充互杈撳叆/杈撳嚭鏂瑰紡鏉ユ墦寮錛屽氨鐢╢stream鏉ュ畾涔夈
浜屻佸叧闂鏂囦歡
鎵撳紑鐨勬枃浠朵嬌鐢ㄥ畬鎴愬悗涓瀹氳佸叧闂錛宖stream鎻愪緵浜嗘垚鍛樺嚱鏁癱lose()鏉ュ畬鎴愭ゆ搷浣滐紝濡傦細file1.close();灝辨妸file1鐩歌繛鐨勬枃浠跺叧闂銆
涓夈佽誨啓鏂囦歡
璇誨啓鏂囦歡鍒嗕負鏂囨湰鏂囦歡鍜屼簩榪涘埗鏂囦歡鐨勮誨彇錛屽逛簬鏂囨湰鏂囦歡鐨勮誨彇姣旇緝綆鍗曪紝鐢ㄦ彃鍏ュ櫒鍜屾瀽鍙栧櫒灝卞彲浠ヤ簡錛涜屽逛簬浜岃繘鍒剁殑璇誨彇灝辮佸嶆潅浜涳紝涓嬭佸氨璇︾粏鐨勪粙緇嶈繖涓ょ嶆柟寮
1銆佹枃鏈鏂囦歡鐨勮誨啓
鏂囨湰鏂囦歡鐨勮誨啓寰堢畝鍗曪細鐢ㄦ彃鍏ュ櫒(<<)鍚戞枃浠惰緭鍑猴紱鐢ㄦ瀽鍙栧櫒(>>)浠庢枃浠惰緭鍏ャ傚亣璁緁ile1鏄浠ヨ緭鍏ユ柟寮忔墦寮錛宖ile2浠ヨ緭鍑烘墦寮銆傜ず渚嬪備笅錛
file2<<"I
Love
You";//鍚戞枃浠跺啓鍏ュ瓧絎︿覆"I
Love
You"
int
i;
file1>>i;//浠庢枃浠惰緭鍏ヤ竴涓鏁存暟鍊箋
榪欑嶆柟寮忚繕鏈変竴縐嶇畝鍗曠殑鏍煎紡鍖栬兘鍔涳紝姣斿傚彲浠ユ寚瀹氳緭鍑轟負16榪涘埗絳夌瓑錛屽叿浣撶殑鏍煎紡鏈変互涓嬩竴浜
鎿嶇旱絎
鍔熻兘
杈撳叆/杈撳嚭
dec
鏍煎紡鍖栦負鍗佽繘鍒舵暟鍊兼暟鎹
杈撳叆鍜岃緭鍑
endl
杈撳嚭涓涓鎹㈣岀﹀苟鍒鋒柊姝ゆ祦
杈撳嚭
ends
杈撳嚭涓涓絀哄瓧絎
杈撳嚭
hex
鏍煎紡鍖栦負鍗佸叚榪涘埗鏁板兼暟鎹
杈撳叆鍜岃緭鍑
oct
鏍煎紡鍖栦負鍏榪涘埗鏁板兼暟鎹
杈撳叆鍜岃緭鍑
setpxecision(int
p)
璁劇疆嫻鐐規暟鐨勭簿搴︿綅鏁
杈撳嚭
姣斿傝佹妸123褰撲綔鍗佸叚榪涘埗杈撳嚭錛歠ile1<<hex<<123;瑕佹妸3.1415926浠5浣嶇簿搴﹁緭鍑猴細file1<<setpxecision(5)<<3.1415926銆
2銆佷簩榪涘埗鏂囦歡鐨勮誨啓
鈶爌ut()
put()鍑芥暟鍚戞祦鍐欏叆涓涓瀛楃︼紝鍏跺師鍨嬫槸ofstream
&put(char
ch)錛屼嬌鐢ㄤ篃姣旇緝綆鍗曪紝濡俧ile1.put('c');灝辨槸鍚戞祦鍐欎竴涓瀛楃'c'銆
鈶get()
get()鍑芥暟姣旇緝鐏墊椿錛屾湁3縐嶅父鐢ㄧ殑閲嶈澆褰㈠紡錛
涓縐嶅氨鏄鍜宲ut()瀵瑰簲鐨勫艦寮忥細ifstream
&get(char
&ch);鍔熻兘鏄浠庢祦涓璇誨彇涓涓瀛楃︼紝緇撴灉淇濆瓨鍦ㄥ紩鐢╟h涓錛屽傛灉鍒版枃浠跺熬錛岃繑鍥炵┖瀛楃︺傚俧ile2.get(x);琛ㄧず浠庢枃浠朵腑璇誨彇涓涓瀛楃︼紝騫舵妸璇誨彇鐨勫瓧絎︿繚瀛樺湪x涓銆
鍙︿竴縐嶉噸杞藉艦寮忕殑鍘熷瀷鏄錛
int
get();榪欑嶅艦寮忔槸浠庢祦涓榪斿洖涓涓瀛楃︼紝濡傛灉鍒拌揪鏂囦歡灝撅紝榪斿洖EOF錛屽倄=file2.get();鍜屼笂渚嬪姛鑳芥槸涓鏍風殑銆
榪樻湁涓縐嶅艦寮忕殑鍘熷瀷鏄錛歩fstream
&get(char
*buf,int
num,char
delim='n')錛涜繖縐嶅艦寮忔妸瀛楃﹁誨叆鐢
buf
鎸囧悜鐨勬暟緇勶紝鐩村埌璇誨叆浜
num
涓瀛楃︽垨閬囧埌浜嗙敱
delim
鎸囧畾鐨勫瓧絎︼紝濡傛灉娌′嬌鐢
delim
榪欎釜鍙傛暟錛屽皢浣跨敤緙虹渷鍊兼崲琛岀'n'銆備緥濡傦細
file2.get(str1,127,'A');//浠庢枃浠朵腑璇誨彇瀛楃﹀埌瀛楃︿覆str1錛屽綋閬囧埌瀛楃'A'鎴栬誨彇浜127涓瀛楃︽椂緇堟銆
鈶㈣誨啓鏁版嵁鍧
瑕佽誨啓浜岃繘鍒舵暟鎹鍧楋紝浣跨敤鎴愬憳鍑芥暟read()鍜寃rite()鎴愬憳鍑芥暟錛屽畠浠鍘熷瀷濡備笅錛
read(unsigned
char
*buf,int
num);
write(const
unsigned
char
*buf,int
num);
read()浠庢枃浠朵腑璇誨彇
num
涓瀛楃﹀埌
buf
鎸囧悜鐨勭紦瀛樹腑錛屽傛灉鍦ㄨ繕鏈璇誨叆
num
涓瀛楃︽椂灝卞埌浜嗘枃浠跺熬錛屽彲浠ョ敤鎴愬憳鍑芥暟
int
gcount();鏉ュ彇寰楀疄闄呰誨彇鐨勫瓧絎︽暟錛涜
write()
浠巄uf
鎸囧悜鐨勭紦瀛樺啓
num
涓瀛楃﹀埌鏂囦歡涓錛屽煎緱娉ㄦ剰鐨勬槸緙撳瓨鐨勭被鍨嬫槸
unsigned
char
*錛屾湁鏃跺彲鑳介渶瑕佺被鍨嬭漿鎹銆
渚嬶細
unsigned
char
str1[]="I
Love
You";
int
n[5];
ifstream
in("xxx.xxx");
ofstream
out("yyy.yyy");
out.write(str1,strlen(str1));//鎶婂瓧絎︿覆str1鍏ㄩ儴鍐欏埌yyy.yyy涓
in.read((unsigned
char*)n,sizeof(n));//浠巟xx.xxx涓璇誨彇鎸囧畾涓鏁存暟錛屾敞鎰忕被鍨嬭漿鎹
in.close();out.close();
鍥涖佹嫻婨OF
鎴愬憳鍑芥暟eof()鐢ㄦ潵媯嫻嬫槸鍚﹀埌杈炬枃浠跺熬錛屽傛灉鍒拌揪鏂囦歡灝捐繑鍥為潪0鍊礆紝鍚﹀垯榪斿洖0銆傚師鍨嬫槸int
eof();
渚嬶細
if(in.eof())ShowMessage("宸茬粡鍒拌揪鏂囦歡灝撅紒");
浜斻佹枃浠跺畾浣
鍜孋鐨勬枃浠舵搷浣滄柟寮忎笉鍚岀殑鏄錛孋++
I/O緋葷粺綆$悊涓や釜涓庝竴涓鏂囦歡鐩歌仈緋葷殑鎸囬拡銆備竴涓鏄璇繪寚閽堬紝瀹冭存槑杈撳叆鎿嶄綔鍦ㄦ枃浠朵腑鐨勪綅緗錛涘彟涓涓鏄鍐欐寚閽堬紝瀹冧笅嬈″啓鎿嶄綔鐨勪綅緗銆傛瘡嬈℃墽琛岃緭鍏ユ垨杈撳嚭鏃訛紝鐩稿簲鐨勬寚閽堣嚜鍔ㄥ彉鍖栥傛墍浠ワ紝C++鐨勬枃浠跺畾浣嶅垎涓鴻諱綅緗鍜屽啓浣嶇疆鐨勫畾浣嶏紝瀵瑰簲鐨勬垚鍛樺嚱鏁版槸
seekg()鍜
seekp()錛宻eekg()鏄璁劇疆璇諱綅緗錛宻eekp鏄璁劇疆鍐欎綅緗銆傚畠浠鏈閫氱敤鐨勫艦寮忓備笅錛
istream
&seekg(streamoff
offset,seek_dir
origin);
ostream
&seekp(streamoff
offset,seek_dir
origin);
streamoff瀹氫箟浜
iostream.h
涓錛屽畾涔夋湁鍋忕Щ閲
offset
鎵鑳藉彇寰楃殑鏈澶у礆紝seek_dir
琛ㄧず縐誨姩鐨勫熀鍑嗕綅緗錛屾槸涓涓鏈変互涓嬪肩殑鏋氫婦錛
ios::beg錛
鏂囦歡寮澶
ios::cur錛
鏂囦歡褰撳墠浣嶇疆
ios::end錛
鏂囦歡緇撳熬
榪欎袱涓鍑芥暟涓鑸鐢ㄤ簬浜岃繘鍒舵枃浠訛紝鍥犱負鏂囨湰鏂囦歡浼氬洜涓虹郴緇熷瑰瓧絎︾殑瑙i噴鑰屽彲鑳戒笌棰勬兂鐨勫間笉鍚屻
渚嬶細
file1.seekg(1234,ios::cur);//鎶婃枃浠剁殑璇繪寚閽堜粠褰撳墠浣嶇疆鍚戝悗縐1234涓瀛楄妭
file2.seekp(1234,ios::beg);//鎶婃枃浠剁殑鍐欐寚閽堜粠鏂囦歡寮澶村悜鍚庣Щ1234涓瀛楄妭
濡傛灉vc緙栫▼鐨勮瘽鏈濂戒嬌鐢–File綾葷瓑鏇村姞鏂逛究浜庢枃浠舵搷浣</CA>