基于java文本复制的7种方式总结
更新时间:2018年01月22日 13:59:37 作者:小林子林子
下面小编就为大家分享一篇基于java文本复制的7种方式总结,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
如下所示:
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,一次读取一个字符数组 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,一次读取一个字节 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,一次读取一个字节数组 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,一次读取一个字节 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,一次读取一个字节数组 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文本复制的7种方式总结就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
Springboot项目实现Mysql多数据源切换的完整实例
这篇文章主要给大家介绍了关于Springboot项目实现Mysql多数据源切换的完整实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-11-11springboot中使用@Transactional注解事物不生效的坑
这篇文章主要介绍了springboot中使用@Transactional注解事物不生效的原因,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2021-01-01springcloud feign 接口指定接口服务ip方式
这篇文章主要介绍了springcloud feign 接口指定接口服务ip方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-03-03
最新评论