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