Java作业杨枝11.25/11.26

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();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值