基于java文本復(fù)制的7種方式總結(jié)
更新時間:2018年01月22日 13:59:37 作者:小林子林子
下面小編就為大家分享一篇基于java文本復(fù)制的7種方式總結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
如下所示:
package copy; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class FileCopy { public static void main(String[] args) throws IOException {
// 第一種: 使用FileReader和FileWrite,一次讀取一個字符 FileReader fr = new FileReader("D:\\a.txt"); FileWriter fw = new FileWriter("D:\\b.txt"); int ch; while((ch = fr.read()) != -1) { fw.write(ch); } fw.close(); fr.close();
// 第二種: 使用FileReader和FileWrite,一次讀取一個字符數(shù)組 FileReader fr = new FileReader("D:\\a.txt"); FileWriter fw = new FileWriter("D:\\b.txt"); char[] chs = new char[1024]; int len; while((len = fr.read(chs)) != -1) { fw.write(chs, 0, len); } fw.close(); fr.close();
// 第三種: 使用FileOutputStream和FileInputStream,一次讀取一個字節(jié) FileInputStream fis = new FileInputStream("D:\\a.txt"); FileOutputStream fos = new FileOutputStream("D:\\b.txt"); int ch; while((ch = fis.read()) != -1) { fos.write(ch); } fos.close(); fis.close();
// 第四種: 使用FileOutputStream和FileInputStream,一次讀取一個字節(jié)數(shù)組 FileInputStream fis = new FileInputStream("D:\\a.txt"); FileOutputStream fos = new FileOutputStream("D:\\b.txt"); int ch; byte[] by = new byte[1024]; while((ch = fis.read(by)) != -1) { fos.write(by, 0, ch); } fos.close(); fis.close();
// 第五種: 使用BufferedReader和BufferedWriter,一次讀取一行 BufferedReader br = new BufferedReader(new FileReader("D:\\a.txt")); BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\b.txt")); String line; while((line = br.readLine()) != null) { bw.write(line); bw.newLine(); bw.flush(); } bw.close(); br.close();
// 第六種: 使用高效緩沖流,BufferedInputStream和BufferedOutputStream,一次讀取一個字節(jié) BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\a.txt")); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\b.txt")); int ch; while((ch = bis.read()) != -1) { bos.write(ch); } bos.close(); bis.close();
// 第七種: 使用高效緩沖流,BufferedInputStream和BufferedOutputStream,一次讀取一個字節(jié)數(shù)組 BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\a.txt")); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\b.txt")); int ch; byte[] by = new byte[1024]; while((ch = bis.read(by)) != -1) { bos.write(by, 0, ch); } bos.close(); bis.close();
} }
以上這篇基于java文本復(fù)制的7種方式總結(jié)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
log4j2 自動刪除過期日志文件的配置及實現(xiàn)原理
這篇文章主要介紹了log4j2 自動刪除過期日志文件配置及實現(xiàn)原理解析,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07Java構(gòu)造方法 super 及自定義異常throw合集詳解用法
異常是程序中的一些錯誤,但不是所有錯誤都是異常,且錯誤有時候是可以避免的,super可以理解為是指向自己超(父)類對象的一個指針,而這個超類指的是離自己最近的一個父類,構(gòu)造器也叫構(gòu)造方法、構(gòu)造函數(shù),是一種特殊類型的方法,負責(zé)類中成員變量(域)的初始化2021-10-10freemarker?jsp?java內(nèi)存方式實現(xiàn)分頁示例
這篇文章主要介紹了freemarker?jsp?java內(nèi)存方式實現(xiàn)分頁示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06詳解Spring Boot Mysql 版本驅(qū)動連接池方案選擇
這篇文章主要介紹了詳解Spring Boot Mysql 版本驅(qū)動連接池方案選擇,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08SpringBoot集成slf4j2日志配置的實現(xiàn)示例
本文主要介紹了SpringBoot集成slf4j2日志配置的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-08-08