File练习

1:复制文本文件:有5种方式
1.基本字节流:一次遍历一个字节
public class first {
public static void main(String[] args) throws IOException {
  // 文件字节输入流
FileInputStream fs=new FileInputStream("third.txt");
  // 文件字节输出流
  FileOutputStream fo=new FileOutputStream("four.txt");
  int by=0;
  while((by=fs.read())!=-1){
  fo.write(by);
  fo.flush();
  }
  fs.close();
  fo.close();
}
}
2.基本字节流:一次遍历一个字节数组
public class first2 {
public static void main(String[] args) throws IOException {
FileInputStream fs= new FileInputStream("first.txt");
   FileOutputStream so = new FileOutputStream("second.txt");
   //一次便利一个字节数组
   int by=0;
  byte[]b=new byte[1024];
   while((by=fs.read())!=-1){
    so.write(b, 0, by);
   }
   fs.close();
   so.close();
}
}
3.字符缓冲流一次读写一个字符
public class first3 {
public static void main(String[] args) throws IOException {
  BufferedReader br = new BufferedReader(new FileReader("first.txt"));  
       BufferedWriter bw = new BufferedWriter(new FileWriter("second.txt"));  
       int ch = 0;  
       while ((ch = br.read()) != -1) {  
           bw.write(ch);  
       }  
       bw.close();  
      br.close();  
   }  
}


4. 字符缓冲流一次读写一个字符数组
public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("first.txt"));  
        BufferedWriter bw = new BufferedWriter(new FileWriter("second.txt"));  
  
        char[] chs = new char[1024];  
        int len = 0;  
        while ((len = br.read(chs)) != -1) {  
            bw.write(chs, 0, len);  
        }  
        bw.close();  
        br.close();  
}
}
5.字符缓冲流一次读一个字符串
public class first5 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("first.txt"));  
     BufferedWriter bw = new BufferedWriter(new FileWriter("second.txt"));  
     String line = null;  
     while ((line = br.readLine()) != null) {  
         bw.write(line);  
         bw.newLine();  
         bw.flush();  
     } 
     bw.close();  
     br.close();  
}
}


2.复制图片:4种方式
1.高效字节流一个读写一个字节数组
public class second {
public static void main(String[] args) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("3.png"));
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("4.png"));  
     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();  
}
}


2. 高效字节流一次读写一个字节
public class second2 {
public static void main(String[] args) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("3.png"));
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("4.png"));  
    int by = 0;
while ((by=bis.read())!=-1){
bos.write(by);
}
bis.close();
bos.close();
}
}


3. 基本字节流一次读写一个字节数组
public class second3 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("3.png");
FileOutputStream fos = new FileOutputStream("4.png");
byte [] bys = new byte [1024];
int by = 0;
while((by=fis.read(bys))!=-1){
fos.write(bys,0,by);
fos.flush();

}
fis.close();
fos.close();
}
}

4. 基本字节流一次读写一个字节
public class second4 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("4.png");
FileOutputStream fos = new FileOutputStream("3.png");
int by = 0;
    while ((by=fis.read())!=-1){
    fos.write(by);
    }
    fis.close();
    fos.close();
}
}


3.已知s.txt文件中有这样的一个字符串:“hcexfgijkamdnoqrzstuvwybpl”
请编写程序读取数据内容,把数据排序后写入ss.txt中。
public class third {
public static void main(String[] args) throws IOException {
BufferedReader bu = new BufferedReader(new FileReader("s.txt"));
String line = bu.readLine();
bu.close();
char[] chs = line.toCharArray();
Arrays.sort(chs);//排序
String s = new String(chs);
//将字符串写入新文件
BufferedWriter newbu = new BufferedWriter(new FileWriter("ss.txt"));
newbu.write(s);
newbu.newLine();
newbu.flush();
newbu.close();
}
}
6:键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低存入文本文件
学生类:


public class student {
private String name ;//姓名
private int chinese ;//语文
private int math ;//数学
private int english;//英语
public student() {
super();
}
public student(String name, int chinese, int math, int english) {
super();
this.name = name;
this.chinese = chinese;
this.math = math;
this.english = english;
}
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 getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
@Override
public String toString() {
return "student [name=" + name + ", chinese=" + chinese + ", math="
+ math + ", english=" + english + "]";
}
public int getSum(){
return this.chinese + this.math+this.english ;
}

}
测试类:
public class fourScanner {
public static class StudentDemo {
  public static void main(String[] args) throws IOException {  
       // 创建集合对象  
       TreeSet<student> ts = new TreeSet<student> (new Comparator<student>() {  
        public int compare(student s1, student s2) {  
         int num = s2.getSum() - s1.getSum();  
             int num2 = num == 0 ? s1.getChinese() - s2.getChinese() : num;  
         int num3 = num2 == 0 ? s1.getMath() - s2.getMath() : num2;  
         int num4 = num3 == 0 ? s1.getEnglish() - s2.getEnglish() : num3;  
             int num5 = num4 == 0 ? s1.getName().compareTo(s2.getName()) : num4;  
         return num5;  
          }  
       });  
        for (int x = 1; x <= 5; x++) {
        Scanner sc = new Scanner(System.in);
            System.out.println("请录入第" + x + "个的学习信息");
        System.out.println("姓名:");
        String name = sc.nextLine();
            System.out.println("语文成绩:");
            int chinese = sc.nextInt();
            System.out.println("数学成绩:");
            int math = sc.nextInt();
            System.out.println("英语成绩:");
            int english = sc.nextInt();
            student s = new student();
            s.setName(name);
            s.setChinese(chinese);
            s.setMath(math);
            s.setEnglish(english);
            ts.add(s);
        }
    // 遍历集合,把数据写到文本文件
    BufferedWriter bw = new BufferedWriter(new FileWriter("ranking.txt"));
        bw.write("姓名,语文成绩,数学成绩,英语成绩");
        bw.newLine();
        bw.flush();
      for (student s : ts) {
          StringBuffer sb = new StringBuffer();
          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();
    }
}

}

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值