FileInputStream与FileOutputStream 复制文件例子代码
生活随笔
收集整理的這篇文章主要介紹了
FileInputStream与FileOutputStream 复制文件例子代码
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
FileInputStream與FileOutputStream 復(fù)制文件例子代碼 try {File sourceFile = new File("C:\\Users\\prize\\Desktop\\Demo1\\盜墓筆記7.txt");//創(chuàng)建源文件InputStream inputStream = new FileInputStream(sourceFile);//創(chuàng)建輸入文件流BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); //創(chuàng)建文本行緩存輸入流 導(dǎo)入 緩存輸入流File targetFile = new File("F:\\手機(jī)T卡資料\\直接復(fù)制T卡\\EBOOK\\復(fù)制的書.txt"); //創(chuàng)建復(fù)制的目標(biāo)文件if (targetFile.exists()) { //判斷文件是否存在targetFile.delete(); //刪除文件targetFile.createNewFile(); //創(chuàng)建文件}else {targetFile.createNewFile(); }OutputStream out = new FileOutputStream(targetFile); //創(chuàng)建輸出文件流BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out)); //創(chuàng)建文本緩存輸出流 導(dǎo)入 緩存輸出流String aString = null; //給一行的內(nèi)容準(zhǔn)備的Stringint i = 0;while ((aString = reader.readLine()) != null && i<20) { //讀取一行,并且賦值給aString,然后在判斷不是空值//i++; //此處是為了輸出指定數(shù)量行數(shù)寫的 循環(huán)自增,writer.write("\r\n"+aString); //將內(nèi)容添加到輸出流中//System.out.println(aString);}writer.flush(); //刷新輸出流writer.close(); //關(guān)閉流 ,注意流的關(guān)閉順序out.close();reader.close();inputStream.close();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}
posted on 2018-03-26 13:43?觀心靜 閱讀(...) 評(píng)論(...) 編輯 收藏
批量處理文件:
posted on 2018-03-26 13:43?觀心靜 閱讀(...) 評(píng)論(...) 編輯 收藏
轉(zhuǎn)載于:https://www.cnblogs.com/guanxinjing/p/9708641.html
總結(jié)
以上是生活随笔為你收集整理的FileInputStream与FileOutputStream 复制文件例子代码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hibernate一对多关联映射
- 下一篇: 2.boost遍历数组容器