2017.11.26

1.复制文本文件:有5种方式

package file;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
 * 复制文本文件,字符流的五种方式
 * @author naughtymonkey
 *
 */
public class FileCopyDemo {
        public static void main(String[] args) throws IOException {
            String sorc="a.txt";
            String cop="b.txt";
            //method(sorc, cop);
            //method2(sorc,cop);
            //method3(sorc,cop);
            //method4(sorc,cop);
            method5(sorc,cop);
        }
        //字符缓冲流一次读写一个字符串
        private static void method5(String sorc, String cop) throws IOException {
            // TODO Auto-generated method stub
            BufferedReader br=new BufferedReader(new FileReader(sorc));
            BufferedWriter bw=new BufferedWriter(new FileWriter(cop));
            String line=null;
            while((line=br.readLine())!=null){
                bw.write(line);
                bw.newLine();
                bw.flush();
            }
            bw.close();
            br.close();

        }
        //字符缓冲流一次 读写一个字符数组
        private static void method4(String sorc, String cop) throws IOException {
            // TODO Auto-generated method stub
            BufferedReader br=new BufferedReader(new FileReader(sorc));
            BufferedWriter bw=new BufferedWriter(new FileWriter(cop));
            char[] ch=new char[1024];
            int len=0;
            while((len=br.read())!=-1){
                bw.write(len);
            }
            bw.close();
            br.close();
        }

        //缓冲字符流一次读写一个字符
        private static void method3(String sorc, String cop) throws IOException {
            // TODO Auto-generated method stub
            BufferedReader br=new BufferedReader(new FileReader(sorc));
            BufferedWriter bw=new BufferedWriter(new FileWriter(cop));
            int len=0;
            while((len=br.read())!=-1){
                bw.write(len);
            }
            bw.close();
            br.close();

        }

        //基本字符流一次读写一个字符数组
private static void method2(String sorc, String cop) throws IOException {
            FileReader fr=new FileReader(sorc);
            FileWriter fw=new FileWriter(cop);
            char[] ch=new char[1024];
            int len=0;
            while((len=fr.read(ch))!=-1){
                fw.write(ch, 0, len);
            }
            fw.close();
            fr.close();
        }
        //基本字符流一次读写一个字符
        private static void method(String sorc,String cop) throws IOException {
            FileReader fr=new FileReader(sorc);
            FileWriter fw=new FileWriter(cop);
            int ch=0;;
            while((ch=fr.read())!=-1){
                fw.write(ch);
            }
            fw.close();
            fr.close();
        }

}

2.复制图片:4种方式

package file;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileStreamDemo {
    // 图片拷贝的四种方式
    public static void main(String[] args) throws IOException {
        File srcFile = new File("c:\\mm.jpg");
        File copFile = new File("d:\\mm.jpg");
        // method(srcFile,copFile);
        // method2(srcFile,copFile);
        // method3(srcFile,copFile);
        method4(srcFile, copFile);
    }

    private static void method4(File srcFile, File copFile) throws IOException {
        // TODO Auto-generated method stub
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(copFile));
        byte[] by = new byte[1024];
        int len = 0;
        while ((len = bis.read(by)) != -1) {
            bos.write(by, 0, len);
        }
        bos.close();
        bis.close();
    }

    // 字节缓冲刘一次读写一个字节
    private static void method3(File srcFile, File copFile) throws IOException {
        // TODO Auto-generated method stub
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(copFile));
        int len = 0;
        while ((len = bis.read()) != -1) {
            bos.write(len);
        }
        bos.close();
        bis.close();
    }

    // 字节流一次读写一个字节数组
    private static void method2(File srcFile, File copFile) throws IOException {
        // TODO Auto-generated method stub
        FileInputStream fis = new FileInputStream(srcFile);
        FileOutputStream fos = new FileOutputStream(copFile);
        byte[] by = new byte[1024];
        int len = 0;
        while ((len = fis.read(by)) != -1) {
            fos.write(by, 0, len);
        }
        fos.close();
        fis.close();

    }

    // 字节流一次读写一个字节
    private static void method(File srcFile, File copFile) throws IOException {
        // TODO Auto-generated method stub
        FileInputStream fis = new FileInputStream(srcFile);
        FileOutputStream fos = new FileOutputStream(copFile);
        int len = 0;
        while ((len = fis.read()) != -1) {
            fos.write(len);
        }
        fos.close();
        fis.close();
    }
}

3.已知s.txt文件中有这样的一个字符串:“hcexfgijkamdnoqrzstuvwybpl”

 请编写程序读取数据内容,把数据排序后写入ss.txt中。
package file;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
/**
 * 需求:已知s.txt文件中有一个这样的字符串:hcexfgijkamdnoqrzstuvwybpl
 * @author naughtymonkey
 *
 */
public class FileCopyTest {
    public static void main(String[] args) throws IOException {
        BufferedReader br=new BufferedReader(new FileReader("ss.txt"));
        String s =br.readLine();
        char[] cha = s.toCharArray();
        Arrays.sort(cha);
        String str = new String(cha);
        BufferedWriter bw=new BufferedWriter(new FileWriter("ss.txt"));
        bw.write(str);
        bw.newLine();
        bw.flush();
        bw.close();
        br.close();
    }

}

4.键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低存入文本文件

package file;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.TreeSet;

public class FileCopy {
    public static void main(String[] args) throws IOException {
        BufferedWriter bw=new BufferedWriter(new FileWriter("student.txt"));
        TreeSet<Student> ts=new TreeSet<Student>();
        for(int x=1;x<=3;x++){
            Scanner scan =new Scanner(System.in);
            System.out.println("输入第"+x+"为学生的姓名");
            String name=scan.nextLine();
            System.out.println("输入语文成绩");
            int chinese=scan.nextInt();
            System.out.println("输入数学成绩");
            int math=scan.nextInt();
            System.out.println("输入英语成绩");
            int english =scan.nextInt();

            Student s=new Student(name,chinese,english,math);
            ts.add(s);

        }

            for(Student s:ts){
                StringBuilder sb=new StringBuilder();
    sb.append(s.getName()).append(",").append(s.getChinese()).append(",").append(s.getMath()).append(",").append(s.getEnglish());
                bw.write(sb.toString());
                bw.newLine();
                bw.flush();
            }
            bw.close();
    }


}
class Student implements Comparable<Student>{
    private String name;
    private int chinese;
    private int english;
    private int math;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getChinese() {
        return chinese;
    }
    public void setChinese(int chinese) {
        this.chinese = chinese;
    }
    public int getEnglish() {
        return english;
    }
    public void setEnglish(int english) {
        this.english = english;
    }
    public int getMath() {
        return math;
    }
    public void setMath(int math) {
        this.math = math;
    }
    public Student() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Student(String name, int chinese, int english, int math) {
        super();
        this.name = name;
        this.chinese = chinese;
        this.english = english;
        this.math = math;
    }
    private int sum(){
        return this.chinese+this.english+this.math;
    }
    @Override
    public int compareTo(Student s) {
        int num=this.sum()-s.sum();
        int num1=num==0?this.getName().compareToIgnoreCase(s.name):num;
        return num1;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值