鐩稿鍐呭瓨鐨勬枃浠舵搷浣滃瓧鑺傛祦

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鐩稿鍐呭瓨鐨勬枃浠舵搷浣滃瓧鑺傛祦相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/nal' title='nal'>nal   time   鏃犳硶   close   鎸囧畾   涓嶅悓   鎿嶄綔鏂囦欢   bre   code   

瀛楄妭娴佺殑姒傚康

鍦ㄨ绠楁満涓紝鏃犺鏂囨湰銆佸浘鐗囥€侀煶棰戣繕鏄棰戯紝鎵€鏈夋枃浠堕兘鏄互浜岃繘鍒?瀛楄妭)褰㈠紡瀛樺湪鐨勩€侷O娴佷腑閽堝瀛楄妭鐨勮緭鍏ヨ緭鍑烘彁渚涗簡涓€绯诲垪鐨勬祦锛岀粺绉颁负瀛楄妭娴併€傚瓧鑺傛祦鏄▼搴忎腑鏈€甯哥敤鐨勬祦锛屾牴鎹暟鎹殑浼犺緭鏂瑰悜鍙皢鍏跺垎涓哄瓧鑺傝緭鍏ユ祦鍜屽瓧鑺傝緭鍑烘祦銆傚湪JDK涓紝鎻愪緵浜嗕袱涓娊璞$被InputStream鍜孫utputStream锛屽畠浠槸瀛楄妭娴佺殑椤剁骇鐖剁被锛屾墍鏈夌殑瀛楄妭杈撳叆娴侀兘缁ф壙鑷狪nputStream锛屾墍鏈夌殑瀛楄妭杈撳嚭娴侀兘缁ф壙鑷狾utputStream銆?/p>

InputStream琚湅鎴愪竴涓緭鍏ョ閬擄紝OutputStream琚湅鎴愪竴涓緭鍑虹閬擄紝鏁版嵁閫氳繃InputStream浠庢簮璁惧杈撳叆鍒扮▼搴忥紝閫氳繃OutputStream浠庣▼搴忚緭鍑哄埌鐩爣璁惧锛屼粠鑰屽疄鐜版暟鎹殑浼犺緭銆侷O娴佷腑鐨勮緭鍏ヨ緭鍑洪兘鏄浉瀵逛簬绋嬪簭鑰岃█鐨?/p>

InputStream鐨勫父鐢ㄦ柟娉?/p>

public static read()
    浠庢瀛楄妭娴佷腑璇诲彇涓€涓暟鎹瓧鑺?

public static read(byte[] b)
    浠庢杈撳叆娴佷腑灏嗘渶澶歜.length涓瓧鑺傜殑鏁版嵁璇诲叆涓€涓猙yte鏁扮粍涓?

public static read(byte[] b,int off,int len)
    鎸囧畾byte鏁扮粍涓粠鍋忕Щ閲弌ff寮€濮嬬殑len涓瓧鑺傝鍑烘鏂囦欢娴?   

os.close()
    鍏抽棴姝ゆ枃浠惰緭鍑烘祦骞堕噴鏀句笌姝ゆ祦鏈夊叧鐨勬墍鏈夌郴缁熻祫婧?/span>

OutputStream鐨勫父鐢ㄦ柟娉?/p>

public void write(int b)
    灏嗘寚瀹氬瓧鑺傚啓鍏ユ鏂囦欢杈撳嚭娴?

public void write(byte[] b)
    灏哹.length涓瓧鑺備粠鎸囧畾byte鏁扮粍鍐欏叆鍒版枃浠惰緭鍑烘祦涓幓

public void write(byte[] b,int off,int len)
    鎸囧畾byte鏁扮粍涓粠鍋忕Щ閲弌ff寮€濮嬬殑len涓瓧鑺傚啓鍏ユ鏂囦欢杈撳嚭娴?       

flush() 
    鍒锋柊缂撳瓨鍖哄苟寮哄埗鍐欏嚭鎵€鏈夌紦鍐茬殑杈撳嚭瀛楄妭    

os.close()
    鍏抽棴姝ゆ枃浠惰緭鍏ユ祦骞堕噴鏀句笌姝ゆ祦鏈夊叧鐨勬墍鏈夌郴缁熻祫婧?/span>

InputStream鍜孫utputStream杩欎袱涓被铏界劧鎻愪緵浜嗕竴绯诲垪鐨勮鍐欐暟鎹湁鍏崇殑鏂规硶锛屼絾鏄繖涓や釜绫绘槸鎶借薄绫伙紝涓嶈兘琚疄渚嬪寲锛屽洜姝わ紝閽堝涓嶅悓鐨勫姛鑳斤紝InputStream鍜孫utputStream鎻愪緵浜嗕袱涓笉鍚岀殑瀛愮被锛岃繖浜涘瓙绫诲舰鎴愪簡涓€涓綋绯荤粨鏋?/p>

瀛楄妭娴佽鍐欐枃浠?/h2>

鐢变簬璁$畻鏈轰腑鐨勬暟鎹熀鏈兘淇濆瓨鍦ㄧ‖鐩樼殑鏂囦欢涓紝鍥犳鎿嶄綔鏂囦欢涓殑鏁版嵁鏃朵竴绉嶅緢甯歌鐨勬搷浣溿€傚湪鎿嶄綔鏂囦欢鏃讹紝鏈€甯歌鐨勫氨鏄粠鏂囦欢涓鍙栨暟鎹苟灏嗘暟鎹啓鍏ユ枃浠讹紝鍗虫枃浠剁殑璇诲啓銆傞拡瀵规枃浠剁殑璇诲啓锛孞DK涓撻棬鎻愪緵浜嗕袱涓被锛屽垎鍒槸FileInputStream鍜孎ileOutputStream銆?/p>

FileInputStream鏄疘nputStream鐨勫瓙绫伙紝瀹冩槸鎿嶄綔鏂囦欢鐨勫瓧鑺傝緭鍏ユ祦锛屼笓闂ㄧ敤浜庤鍙栨枃浠朵腑鐨勬暟鎹€傜敱浜庝粠鏂囦欢涓鍙栨暟鎹椂閲嶅鎬х殑鎿嶄綔锛屽洜姝ら渶瑕侀€氳繃寰幆璇彞鏉ュ疄鐜版暟鎹殑鎸佺画璇诲彇銆?/p>

鎺ヤ笅鏉ラ€氳繃涓€涓渚嬫潵瀹炵幇瀛楄妭娴佸鏂囦欢鏁版嵁鐨勮鍙栵細

public class Example01 {
    public static void main(String[] args) throws Exception {
        //鍒涢晫涓€涓枃浠跺瓧鑺傝緭鍏ユ祦瀵硅薄
        FileInputStream in = new FileInputStream("test.txt");
        int b=0;            //瀹氫箟涓€涓猧nt绫诲瀷鐨勫彉閲廱锛岃浣忔瘡娆¤鍙栫殑涓€涓瓧鑺?/span>
        while(true) {
            b=in.read();    //鍙橀噺b璁颁綇璇诲彇鐨勪竴涓瓧鑺?/span>
            if(b==-1) {        //濡傛灉璇诲彇鐨勫瓧鑺備负-1,璺冲嚭while寰幆
                break;
            }
            System.out.println(b);    //鍚﹀垯灏哹杈撳嚭
        }
    }
}

涓嶧ileInputStream瀵瑰簲鐨勬槸FileOutputStream銆侳ileOutputStream鏄疧utputStream鐨勫瓙绫伙紝浠栨槸鎿嶄綔鏂囦欢鐨勫瓧鑺傝緭鍑烘祦锛屼笓闂ㄦ妸杈撳叆鍐欏叆鏂囦欢銆?/p>

鎺ヤ笅鏉ラ€氳繃涓€涓渚嬫潵婕旂ず濡備綍灏嗘暟鎹啓鍏ユ枃浠讹細

public class Example02 {
    public static void main(String[] args) throws IOException {
        FileOutputStream out = new FileOutputStream("example.txt");
        String str="娉㈤煶椋炴満";
        byte[] b = str.getBytes();
        for (int i = 0; i < b.length; i++) {
            out.write(b[i]);
        }
        out.close();
    }
}

闇€瑕佹敞鎰忕殑鏄紝濡傛灉鏄€氳繃FileOutputStream鍚戜竴涓凡缁忓瓨鍦ㄧ殑鏂囦欢涓啓鍏ユ暟鎹紝閭d箞璇ユ枃浠朵腑鐨勬暟鎹鍏堜細琚儏鍐碉紝鍐嶅啓鍏ユ暟鎹€傝嫢甯屾湜鍦ㄥ凡瀛樺湪鐨勬枃浠跺唴瀹逛箣鍚庤拷鍔犳柊鍐呭锛屽垯鍙娇鐢‵ileOutputStrem鐨勬瀯閫犲嚱鏁癋ileOutputStream(String file, boolean append)鏉ュ垱寤烘枃浠惰緭鍑烘祦瀵硅薄锛屽苟鎶奱ppend鍙傛暟鐨勫€艰缃负true銆?/p>

鎺ヤ笅鏉ラ€氳繃涓€涓渚嬫潵婕旂ず濡備綍灏嗘暟鎹拷鍔犲埌鏂囦欢鏈熬

public class Example01 {
    public static void main(String[] args) throws Exception {
        //鍒涢晫涓€涓枃浠跺瓧鑺傝緭鍏ユ祦瀵硅薄
        FileInputStream in = new FileInputStream("test.txt");
        int b=0;            //瀹氫箟涓€涓猧nt绫诲瀷鐨勫彉閲廱锛岃浣忔瘡娆¤鍙栫殑涓€涓瓧鑺?/span>
        while(true) {
            b=in.read();    //鍙橀噺b璁颁綇璇诲彇鐨勪竴涓瓧鑺?/span>
            if(b==-1) {        //濡傛灉璇诲彇鐨勫瓧鑺備负-1,璺冲嚭while寰幆
                break;
            }
            System.out.println(b);    //鍚﹀垯灏哹杈撳嚭
        }
    }
}

鐢变簬IO娴佸湪杩涜鏁版嵁璇诲啓鎿嶄綔鏃朵細鍑虹幇寮傚父锛屼负浜嗕唬鐮佺殑绠€娲侊紝鍦ㄤ笂闈㈢殑绋嬪簭涓娇鐢ㄤ簡throws鍏抽敭瀛楀皢寮傚父鎶涘嚭銆傜劧鑰屼竴閬囧埌IO寮傚父锛孖O娴佺殑close()鏂规硶灏嗘棤娉曞緱鍒版墽琛岋紝娴佸璞℃墍鍗犵敤鐨勭郴缁熻祫婧愬皢寰椾笉鍒伴噴鏀撅紝鍥犳锛屼负浜嗕繚璇両O娴佺殑close()鏂规硶蹇呴』鎵ц锛岄€氬父灏嗗叧闂祦鐨勬搷浣滃啓鍦╢inally浠g爜鍧椾腑

finally{
    try{
        if(in!=null){
            in.close;
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

鏂囦欢鐨勬嫹璐?/h2>

鍦ㄥ簲鐢ㄧ▼搴忎腑锛孖O娴侀€氬父閮芥槸鎴愬鍑虹幇鐨勶紝鍗宠緭鍏ユ祦鍜岃緭鍑烘祦涓€璧蜂娇鐢ㄣ€備緥濡傛枃浠剁殑鎷疯礉灏遍渶瑕侀€氳繃杈撳叆娴佹潵璇诲彇鏂囦欢涓殑鏁版嵁锛岄€氳繃鏂囦欢杈撳嚭娴佸啓鍏ユ枃浠躲€?/p>

鎺ヤ笅鏉ラ€氳繃涓€涓渚嬫潵婕旂ず濡備綍杩涜鏂囦欢鍐呭鐨勬嫹璐濓細

public class Example01 {
    public static void main(String[] args) throws Exception {
        //鍒涘缓涓€涓瓧鑺傝緭鍏ユ祦锛岀敤浜庤鍙栧綋鍓嶇洰褰曚笅sourse鏂囦欢澶逛腑鐨刴ap3鏂囦欢
        FileInputStream in = new FileInputStream("sourse\鍛ㄦ繁 - 澶ч奔.mp3");
        
        //鍒涘缓涓€涓瓧鑺傝緭鍑烘祦锛岀敤浜庢妸璇诲彇鐨勫瓧鑺傚啓鍏ュ埌target鐩綍涓嬬殑鏂囦欢涓?/span>
        FileOutputStream out = new FileOutputStream("target\鍛ㄦ繁 - 澶ч奔.mp3");

        int len;    //瀹氫箟涓€涓暣鍨嬬被鍨嬬殑鍙橀噺len锛岃浣忔瘡娆¤鍙栫殑涓€涓瓧鑺?/span>
        long starttime = System.currentTimeMillis();         //鑾峰彇鎷疯礉鏂囦欢鍓嶇殑绯荤粺鏃堕棿
        while((len=in.read())!=-1) {
            out.write(len);      //灏嗚鍒扮殑瀛楄妭鍐欏叆鏂囦欢
        }
        long endtime = System.currentTimeMillis();
        System.out.println("鎷疯礉鏃堕棿: "+(endtime-starttime)+"姣");
        in.close();
        out.close();
    }
}

瀛楄妭娴佺殑缂撳啿鍖?/h2>

鍦ㄦ嫹璐濇枃浠舵椂锛屽彲浠ヤ竴娆℃€ц鍙栧涓瓧鑺傜殑鏁版嵁锛屽苟淇濆瓨鍦ㄥ瓧鑺傛暟缁勪腑锛岀劧鍚庡皢瀛楄妭鑰屾暟缁勪腑鐨勬暟鎹竴娆℃€у啓鍏ユ枃浠躲€?/p>

鎺ヤ笅鏉ラ€氳繃淇敼鏂囦欢鏉ュ涔犲浣曚娇鐢ㄧ紦鍐插尯鎷疯礉鏂囦欢

public class Example02 {
    public static void main(String[] args) throws Exception {
        //鍒涘缓涓€涓枃浠惰緭鍏ユ祦锛岀敤浜庤鍙栧綋鍓嶇洰褰曚笅sourse鐩綍涓嬬殑mp3鏂囦欢
        FileInputStream in = new FileInputStream("sourse\鍛ㄦ繁 - 澶ч奔.mp3");
        
        //鍒涘缓涓€涓枃浠惰緭鍑烘祦锛岀敤浜庡皢璇诲彇鐨勬暟鎹啓鍏ュ綋鍓嶇洰褰曚笅鐨則arget鏂囦欢涓?/span>
        FileOutputStream out = new FileOutputStream("target\鍛ㄦ繁 - 澶ч奔.mp3");
        
        //浠ヤ笅鏄敤缂撳啿鍖鸿鍐欐枃浠?/span>
        byte[] buff = new byte[1024];    //瀹氫箟涓€涓瓧鑺傛暟缁勶紝浣滀负缂撳啿鍖?/span>
        
        int len;
        long startTime = System.currentTimeMillis();
        while((len=in.read(buff))!=-1) {    //鍒ゆ柇鏄惁璇诲埌鏂囦欢鏈熬
            out.write(buff, 0, len);    //浠庣涓€涓瓧鑺傚紑濮嬶紝鎯虫枃浠跺啓鍏en涓瓧绗?/span>
        }
        long endTime = System.currentTimeMillis();
        System.out.println("鎷疯礉鏂囦欢鎵€鐢ㄧ殑鏃堕棿:"+(endTime-startTime));
        in.close();
        out.close();
    }
}

鍦ㄦ嫹璐濊繃绋嬩腑锛屼娇鐢╳hile寰幆閫愭笎瀹炵幇瀛楄妭鏂囦欢鐨勬嫹璐濓紝姣忓惊鐜竴娆★紝灏变粠鏂囦欢璇诲彇鑻ュ共瀛楄妭濉厖瀛楄妭鏁扮粍锛屽苟閫氳繃鍙橀噺len璁颁綇璇诲叆鏁扮粍鐨勫瓧鑺傛暟锛岀劧鍚庝粠鏁扮粍鐨勭涓€涓瓧鑺傚紑濮嬶紝灏唋en涓瓧鑺備竴娆″啓鍏ユ枃浠躲€傚惊鐜線澶嶏紝褰搇en鍊间负-1鏃讹紝璇存槑宸茬粡璇诲埌浜嗘枃浠剁殑鏈熬锛屽惊鐜細缁撴潫锛屾暣涓嫹璐濊繃绋嬩篃灏辩粨鏉熶簡銆?br />绋嬪簭涓殑缂撳啿鍖哄氨鏄竴鍧楀唴瀛橈紝璇ュ唴瀛樹富瑕佺敤浜庡瓨鏀炬殏鏃惰緭鍏ヨ緭鍑虹殑鏁版嵁锛岀敱浜庝娇鐢ㄧ紦鍐插尯鍑忓皯浜嗗鏂囦欢鐨勬搷浣滄鏁帮紝鎵€鏈夊彲浠ユ彁楂樿鍐欐暟鎹殑鏁堢巼銆?/p>

鍦↖O鍖呬腑鎻愪緵涓や釜甯︾紦鍐茬殑瀛楄妭娴侊紝鍒嗗埆鏄疊ufferedInputStream鍜孊ufferedOutputStream锛屽畠浠殑鏋勯€犳柟娉曚腑鍒嗗埆鎺ユ敹InputStream鍜孫utputStream绫诲瀷鐨勫弬鏁颁綔涓哄璞★紝鍦ㄨ鍐欐暟鎹椂鎻愪緵缂撳啿鍔熻兘銆?/p>

鎺ヤ笅鏉ラ€氳繃涓€涓渚嬫潵瀛︿範BufferedInputStream鍜孊ufferedOutputStream杩欎袱涓祦鐨勭敤娉曪細

public class Example03 {
    public static void main(String[] args) throws Exception {
        //鍒涘缓涓€涓甫缂撳啿鍖虹殑杈撳叆娴?/span>
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("src.txt"));
    
        //鍒涘缓涓€涓甫缂撳啿鍖虹殑杈撳嚭娴?/span>
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("des.txt"));
        int len;
        while((len=bis.read())!=-1){
            bos.write(len);
        }
        bis.close();
        bos.close();
    }
}

褰撹皟鐢ㄤ簡read()鎴栬€厀rite()鏂规硶璇诲啓鏁版嵁鏃讹紝棣栧厛灏嗚鍐欑殑鏁版嵁瀛樺叆瀹氫箟濂戒簡鐨勫瓧鑺傛暟缁勶紝鐒跺悗灏嗗瓧鑺傛暟缁勪竴娆℃€у啓鍏ュ埌鏂囦欢涓€備粠鑰屾彁楂樹簡鏁版嵁鐨勮鍐欐晥鐜?/p>

 

以上是关于鐩稿鍐呭瓨鐨勬枃浠舵搷浣滃瓧鑺傛祦的主要内容,如果未能解决你的问题,请参考以下文章

Java 瀛楃鐮併€佸瓧绗︽祦銆佸瓧鑺傛祦

WINDOS涓嬭嚜鍔ㄥ浠芥枃浠跺強鍒犻櫎涔嬪墠鐨勬枃浠?bat

hive 濡備綍鑾峰彇杈撳叆鏁版嵁鐨勬枃浠跺悕

MATLAB璇诲彇鏂囦欢鈥斺€斾粠闈炲父瑙勬枃鏈枃浠朵腑璇诲彇鏁版嵁

鍑芥暟鑺傛祦

鎴戠殑妯″瀷鏈夊蹇紵鈥斺€旀繁搴﹀涔犵綉缁滄ā鍨嬬殑杩愮畻澶嶆潅搴︺€佺┖闂村崰鐢ㄥ拰鍐呭瓨璁块棶鎯呭喌璁$畻