0x01:
fileinputstream/fileout焦仲卿和刘兰芝putstream字节流进行文件的复制
privatestaticvoidstreamcopyfile(filesrcfile,filedesfile){try{//使用字节流进行文件复制fileinputstreamfi=newfileinputstream(srcfile);fileoutputstreamfo=newfileoutputstream(desfile);integerby=0;//一次读取一个字节while((by=fi.read())!=-1){fo.write(by);}fi.clo();fo.clo();}catch(exceptione){e.printstacktrace();}}
0x02:
bufferedinputstream/bufferedoutputstream高效字节流进行复制文件
privatestaticvoidbufferedstreamcopyfile(filesrcfile,filedesfile){try{//使用缓冲字节流进行文件复制bufferedinputstreambis=newbufferedinputstream(newfileinputstream(srcfile));bufferedoutputstreambos=newbufferedoutputstream(newfileoutputstream(desfile));byte[]b=newbyte[1024];integerlen=0;//一次读取1024字节衣服染色怎么洗掉的数据while((len=bis.read(b))!=-1){bos.write(b,0,len);}bis.clo();bos.clo();}catch(exceptione){e.printstacktrace();}}
0x03: filereader/filewriter字符流进行文件复制文件
privatestaticvoidreaderwritercopyfile(filesrcfile,filedesfile){try{//使用字符流进什么地什么填动词行文件复制,注意:字符流只能复制只含有汉字的文件filereaderfr=newfilereader(srcfile);filewriterfw=newfilewriter(desfile);integerby=0;while((by=fr.read())!=-1){fw.write(by);}fr.c活动树lo();fw.clo();}catch(exceptione){e.printstacktrace();}}
0x04:
bufferedreader/bufferedwriter高效字符流进行文件复制
privatestaticvoidbufferedreaderwritercopyfile(filesrcfile,filedesfile){try{//使用带缓冲区的高效字符流进行文件复制bufferedreaderbr=newbufferedreader(newfilereader(srcfile));bufferedwriterbw=newbufferedwriter(newfilewriter(desfile));char[]c=newchar[1024];integerlen=0;while((len=br.read(c))!=-1){bw.write(c,0,len);}//方式二/*strings=null;while((s=br.readline())!=null){bw.write(s);bw.newline();}*/br.clo();bw.clo();}catch(exceptione){e.printstacktrace();}}
0x05: nio实现文件拷贝(用transferto的实现 或者transferfrom的实现)
publicstaticvoidniocopyfile(stringsource,stringtarget){try{//1.采用randomaccessfile双向通道完成,rw表示具有读写权限randomaccessfilefromfile=newrandomaccessfile(source,"rw");filechannelfromchannel=fromfile.getchannel();randomaccessfiletofile=newrandomaccessfile(target,"rw");filechanneltochannel=tofile.getchannel();longcount=fromchannel.size();while(count>0){longtransferred=fromchannel.transferto(fromchannel.position(),count,tochannel);count-=transferred;}if(fromfile!=null){fromfile.clo();}if(fromchannel!=null){fromchannel.clo();}}catch(exceptione){e.printstacktrace(); }}
0x06: java.nio.file.files.copy()实现文件拷贝,其中第三个参数决定是否覆盖
publicstaticvoidcopyfile(stringsource,stringtarget){pathsourcepath=paths.get(source);pathdestinationpath=paths.get(targ有关春天的诗词et);try{files.copy(sourcepath,destinationpath,standardcopyoption.replace_existing);}catch(ioexceptione){e.printstacktrace();}}
本文发布于:2023-04-04 06:16:00,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/406a22f75df26f456079087f6e096d25.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:java写文件路径(java程序运行步骤).doc
本文 PDF 下载地址:java写文件路径(java程序运行步骤).pdf
留言与评论(共有 0 条评论) |