Java作业杨枝11.25/11.26
2018-04-10 04:19
337 查看
package prac; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; /*复制文本文件的5种方法(将当前项目下a.txt的内容复制到当前项目下的b.txt中)*/ public class Prac01 { public static void main(String[] args) throws IOException { String s1="a.txt"; String s2="b.txt"; method1(s1,s2); method2(s1,s2); method3(s1,s2); method4(s1,s2); method5(s1,s2); } //字符缓冲流一次读写一个字符串 private static void method5(String s1,String s2) throws IOException { BufferedReader fr=new BufferedReader(new FileReader(s1)); BufferedWriter fw = new BufferedWriter(new FileWriter(s2)); String len=null; while((len=fr.readLine())!=null){ fw.write(len); fw.newLine(); fw.flush(); } fw.close(); fr.close(); } //字符缓冲流一次读写一个字符数组 private static void method4(String s1,String s2) throws IOException { BufferedReader fr=new BufferedReader(new FileReader(s1)); BufferedWriter fw = new BufferedWriter(new FileWriter(s2)); char[] chs=new char[1024]; int len=0; while((len=fr.read())!=-1){ fw.write(chs,0,len); } fw.close(); fr.close(); } //字符缓冲流一次读写一个字符 private static void method3(String s1,String s2) throws IOException { BufferedReader fr=new BufferedReader(new FileReader(s1)); BufferedWriter fw = new BufferedWriter(new FileWriter(s2)); int by=0; while((by=fr.read())!=-1){ fw.write(by); } fw.close(); fr.close(); } //基本字符流一次读写一个字符数组 private static void method2(String s1,String s2) throws IOException { FileReader fr=new FileReader(s1); FileWriter fw = new FileWriter(s2); char[] chs=new char[1024]; int len=0; while((len=fr.read())!=-1){ fw.write(chs,0,len); } fw.close(); fr.close(); } //基本字符流一次读写一个字符 private static void method1(String s1,String s2) throws IOException { FileReader fr=new FileReader(s1); FileWriter fw = new FileWriter(s2); int by=0; while((by=fr.read())!=-1){ fw.write(by); } fw.close(); fr.close(); } }
package prac; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /*4:复制图片:4种方式 * 将F:\\照片\\1.JPG复制到F:\\照片\\2.jpg * */ public class Prac02 { public static void main(String[] args) throws IOException { String s1="F:\\picture\\1.JPG"; String s2="F:\\picture\\2.jpg"; method1(s1,s2); method2(s1, s2); method3(s1, s2); method4(s1, s2); } //字节缓冲流一次读写一个字节数组 private static void method4(String s1, String s2) throws IOException { BufferedInputStream fis = new BufferedInputStream(new FileInputStream(s1)); BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(s1)); byte[] chs=new byte[1024]; int len=0; while((len=fis.read(chs))!=-1){ fos.write(chs,0,len); } fis.close(); fos.close(); } //字节缓冲流一次读写一个字节 private static void method3(String s1, String s2) throws IOException { BufferedInputStream fis = new BufferedInputStream(new FileInputStream(s1)); BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(s1)); int by=0; while((by=fis.read())!=-1){ fos.write(by); } fis.close(); fos.close(); } //一般字节流一次读写一个字节数组 private static void method2(String s1, String s2) throws IOException { FileInputStream fis = new FileInputStream(s1); FileOutputStream fos=new FileOutputStream(s2); byte[] chs=new byte[1024]; int len=0; while((len=fis.read(chs))!=-1){ fos.write(chs,0,len); } fis.close(); fos.close(); } //一般字节流一次读写一个字节 private static void method1(String s1, String s2) throws IOException { FileInputStream fis = new FileInputStream(s1); FileOutputStream fos=new FileOutputStream(s2); int by=0; while((by=fis.read())!=-1){ fos.write(by); } fis.close(); fos.close(); } }
相关文章推荐
- Java作业杨枝10.14/10.15
- 11.25java作业
- Java第十八天~第十九天/11.25~11.26
- Java作业杨枝12.2/12.3
- 【Java作业】2017.11.25 / 11.26
- Java作业杨枝11.11/11.12
- java杨枝作业11.4/11.5
- Java笔记杨枝11.26
- Java笔记杨枝11.25
- java作业杨枝11.18/11.19
- Java作业杨枝10.28/10.29
- Java作业_Day21_
- JAVA课程作业06
- 11csdn-java周末作业(登陆界面)
- java做的操作系统进程管理模拟(操作系统作业)
- Java作业-查询相等价格图书
- java-java57-第四次作业
- WEEK4 JAVA 作业
- JAVA第二次作业
- java第二次作业