文件的读取流和书写流
public void read(){
??InputStream in=null; ?? ?
?try {
???//創(chuàng)建文件字節(jié)流
???in=new FileInputStream("jpg/1.jpg");
?
?int i=0;?
?//一次讀取一個(gè)字節(jié),返回對(duì)去的數(shù)據(jù),如果返回-1表示讀取完畢?
??while((i=in.read())!=-1){
?????System.out.println(i);?
??} ??? ?
??byte[] by=new byte[1024];
???int len=0; ?
??//一次讀取1024字節(jié),將讀取的數(shù)據(jù)存入字節(jié)數(shù)組,返回本次讀取的字節(jié)數(shù),返回-1表示讀取完畢 ??
?while((len=in.read(by))!=-1){
????System.out.println(len); ?
??} ???
??} catch (Exception e) {
???// TODO Auto-generated catch block ???
e.printStackTrace();
??}finally{
???try { ??
??in.close(); ?
??} catch (IOException e) { ?
???// TODO Auto-generated catch block ???
?e.printStackTrace(); ?
??} ??} ?}
?public void writer(){
??OutputStream out=null; ?
? ??try { ???
//創(chuàng)建文件寫入流,true表示追加寫入數(shù)據(jù),默認(rèn)為替換寫入 ?
??out=new FileOutputStream("abc.txt",true);??
??//寫入文件 ???out.write("明天可以睡懶覺".getBytes()); ?
?} catch (Exception e) { ??
?// TODO Auto-generated catch block ?
??e.printStackTrace(); ?
?}finally{ ???
try { ????
out.close(); ?
??} catch (IOException e) {
????// TODO Auto-generated catch block ???
?e.printStackTrace(); ??
?} ??} ?} ?
?public void copyfile(){ ?
?InputStream in=null; ??
OutputStream out=null; ??
??try {
???in=new FileInputStream("d:/西西軟件園.txt");
???out=new FileOutputStream("abc.txt"); ?
?? ???byte[] b=new byte[1024]; ??
?int a=0;
???while((a=in.read(b))!=-1){ ??
??out.write(b, 0, a);
???} ??? ???
??} catch (Exception e) {
???// TODO Auto-generated catch block
???e.printStackTrace(); ?
?}finally{ ?
??try { ???
?out.close();
????in.close(); ??
?} catch (IOException e) { ?
???// TODO Auto-generated catch block ????e.printStackTrace(); ??
?} ??} ?}
轉(zhuǎn)載于:https://www.cnblogs.com/ldl454700988/p/6822330.html
總結(jié)
以上是生活随笔為你收集整理的文件的读取流和书写流的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Maven的安装文字版(Windows/
- 下一篇: 移动端h5开发总结不断更新中....