[2017.11.26]作业14
来源:互联网 发布:mv视频制作软件 编辑:程序博客网 时间:2024/06/05 08:54
3:复制文本文件:有5种方式
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class Demo { public static void main(String[] args) throws IOException { String srcString = "c:\\a.txt"; String destString = "d:\\b.txt"; method1(srcString, destString); method2(srcString, destString); method3(srcString, destString); method4(srcString, destString); method5(srcString, destString); } // 字符缓冲流一次读写一个字符串 private static void method5(String srcString, String destString) throws IOException { BufferedReader br = new BufferedReader(new FileReader(srcString)); BufferedWriter bw = new BufferedWriter(new FileWriter(destString)); String line = null; while ((line = br.readLine()) != null) { bw.write(line); bw.newLine(); bw.flush(); } bw.close(); br.close(); } // 字符缓冲流一次读写一个字符数组 private static void method4(String srcString, String destString) throws IOException { BufferedReader br = new BufferedReader(new FileReader(srcString)); BufferedWriter bw = new BufferedWriter(new FileWriter(destString)); char[] chs = new char[1024]; int len = 0; while ((len = br.read(chs)) != -1) { bw.write(chs, 0, len); } bw.close(); br.close(); } // 字符缓冲流一次读写一个字符 private static void method3(String srcString, String destString) throws IOException { BufferedReader br = new BufferedReader(new FileReader(srcString)); BufferedWriter bw = new BufferedWriter(new FileWriter(destString)); int ch = 0; while ((ch = br.read()) != -1) { bw.write(ch); } bw.close(); br.close(); } // 基本字符流一次读写一个字符数组 private static void method2(String srcString, String destString) throws IOException { FileReader fr = new FileReader(srcString); FileWriter fw = new FileWriter(destString); char[] chs = new char[1024]; int len = 0; while ((len = fr.read(chs)) != -1) { fw.write(chs, 0, len); } fw.close(); fr.close(); } // 基本字符流一次读写一个字符 private static void method1(String srcString, String destString) throws IOException { FileReader fr = new FileReader(srcString); FileWriter fw = new FileWriter(destString); int ch = 0; while ((ch = fr.read()) != -1) { fw.write(ch); } fw.close(); fr.close(); } }
4:复制图片:4种方式
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class Demo2 { public static void main(String[] args) throws IOException { File srcFile = new File("c:\\a.jpg"); File destFile = new File("d:\\b.jpg"); method1(srcFile, destFile); method2(srcFile, destFile); method3(srcFile, destFile); method4(srcFile, destFile); } //高效字节流一次读写一个字节数组 public static void method4(File srcFile, File destFile) throws IOException { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile)); BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(destFile)); byte[] bys = new byte[1024]; int len = 0; while ((len = bis.read(bys)) != -1) { bos.write(bys, 0, len); bos.flush(); } bos.close(); bis.close(); } //高效字节流一次读写一个字节 public static void method3(File srcFile, File destFile) throws IOException { BufferedInputStream bis = new BufferedInputStream(new FileInputStream( srcFile)); BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(destFile)); int len = 0; while ((len = bis.read()) != -1) { bos.write(len); } bos.close(); bis.close(); } //基本字节流一次读写一个字节数组 public static void method2(File srcFile, File destFile) throws IOException { FileInputStream fis = new FileInputStream(srcFile); FileOutputStream fos = new FileOutputStream(destFile); byte[] bys = new byte[1024]; int len = 0; while ((len = fis.read(bys)) != -1) { fos.write(bys, 0, len); fos.flush(); } fos.close(); fis.close(); } //基本字节流一次读写一个字节 public static void method1(File srcFile, File destFile) throws IOException { FileInputStream fis = new FileInputStream(srcFile); FileOutputStream fos = new FileOutputStream(destFile); int len = 0; while ((len = fis.read()) != -1) { fos.write(len); } fos.close(); fis.close(); } }
阅读全文
0 0
- [2017.11.26]作业14
- 2017.11.14作业
- 2017.11.14作业
- [2017.11.01]作业06
- 2017.11.2作业
- [2017.11.04]作业08
- 2017.11.3作业
- 2017.11.6作业
- 2017.11.7作业
- 2017.11.10作业
- 2017.11.13作业
- 2017.11.15作业
- [2017.11.16]作业10
- [2017.11.18]作业12
- 2017.11.21大作业
- 2017.11.25作业
- 2017.11.27作业
- //11/26 作业(5)
- PageHelper connot be cast to org.apache.ibatis.plugin.Interceptor
- 【CentOS】——安装jdk(用yum安装jdk)
- PHP目前比较常见的五大运行模式
- vue.js 开发环境搭建最简单攻略
- 上传图片
- [2017.11.26]作业14
- POJ3169 Layout (差分约束)
- Android ViewPager+Fragment切换显示不出的问题
- jQuery之trigger()/triggerHandler()
- 启明星第五周学习总结
- Android 时间戳字符串转Date类型
- arm shellcode 编写详析2
- 链式前向星+b/dfs
- Servlet介绍之高级篇