JAVA作业五:文件部分

6 篇文章 0 订阅

1,向指定的txt文件中写入键盘输入的内容,然后再重新读取该文件中的内容,显示到控制台上。
代码:

package 作业5;

import java.io.*;
import java.util.Scanner;

public class input_output_1 {
    public static void main(String[] args) {
        FileOutputStream fos;
        FileInputStream fis;
        InputStreamReader isr;
        Scanner sc = new Scanner(System.in);
        try{
            fos = new FileOutputStream("E:\\java--idea--1\\a.txt");  //在e盘创建txt文件,用于写入内容
            System.out.print("please inter your String:");
            String s = sc.next();
            byte[] b = s.getBytes();
            fos.write(b);  //将字符串s的内容输入到txt文件中
            System.out.print("The string was written successfully,see it in a.txt.");
            fos.close();  //关闭文件输出流

            fis = new FileInputStream("E:\\java--idea--1\\a.txt");
            isr = new InputStreamReader(fis,"utf-8");  //字符集utf-8,避免中文出现乱码情况
            int data = 0;
            System.out.println("The output is as follows:");
            while((data = isr.read()) != -1){
                System.out.print((char)data);  //读取txt文件的内容并将其输出到控制台
            }
            fis.close();  //关闭文件输入流
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }catch(IOException e1){
            e1.printStackTrace();  //抛出异常
        }
    }
}

运行截图:
文件内容
在这里插入图片描述
控制台内容
在这里插入图片描述

2,键盘录入5个学生信息(姓名,成绩),按照 成绩从高到低存入文本文件。
代码:

package 作业5;

public class Student_20 {
    private String name;
    private int score;
    //无参构造方法
    public Student_20(){
        super();
    }
    //有参构造方法
    public Student_20(String name, int score){
        this.name = name;
        this.score = score;
    }

    public String getName() {
        return name;
    } //得到学生姓名

    public int getScore() {
        return score;
    } //得到学生分数

    public void setName(String name) {
        this.name = name;
    }

    public void setScore(int score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", score=" + score +
                '}';
    }
}

package 作业5;

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

public class Student_test_21 {
    public static void main(String[] args) throws IOException{
        //创建TreeSet集合
        TreeSet<Student_20> set = new TreeSet<>(new Comparator<Student_20>() {
            @Override
            public int compare(Student_20 s1, Student_20 s2) {
                int cmp1 =   s2.getScore() - s1.getScore();
                int cmp2 = cmp1 == 0 ? s2.getName().compareTo(s1.getName()) : cmp1;
                return cmp2;
            }
        });
        // 输入学生信息
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入总学生个数:");
        int Student_number = sc.nextInt();
        for(int i=0;i<Student_number;i++){
            System.out.println("请输入第"+(i+1)+"个学生的信息:");
            System.out.print("name:");
            String name = sc.next();
            System.out.print("score:");
            int score = sc.nextInt();
            //创建学生对象并输入信息
            Student_20 s = new Student_20();
            s.setName(name);
            s.setScore(score);
            set.add(s);  //将学生信息添加到集合中
        }
        System.out.println("信息录入完成!");
            BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\java--idea--1\\student_test.txt"));
            bw.write("由高到低排序后:");
            bw.write("\n");
            for(Student_20 s:set){
                StringBuilder Sb = new StringBuilder("name----"+s.getName()+",score----"+s.getScore());
                bw.write(Sb.toString());
                bw.newLine();
                bw.flush();
            }
            bw.close();
    }
}

运行截图:
在这里插入图片描述
排序后的文本文件
在这里插入图片描述
3,复制指定目录的指定类型(如.java)的文件到另一个目录中。
代码:

package 作业5;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class copy_3 {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.print("从哪里复制?");
        String from = scanner.next();//String inFile = "C:\\Windows\\SysWOW64";
        System.out.print("复制到哪里?");
        String to = scanner.next();//String outFile = "C:\\Users\\Administrator\\Desktop\\64dll";
        System.out.print("复制哪种文件?输入后缀名:");
        String type = scanner.next();

        FileInputStream fis = null;
        FileOutputStream fos = null;
            File destination = new File(to);
            if (!destination.exists()) {
                destination.mkdirs();
            }

            File source = new File(from);

            if (source.exists()) {
                File[] listFiles = source.listFiles();
                if (listFiles != null && listFiles.length > 0) {
                    for (File file : listFiles) {
                        String fileName = file.getName();
                        String[] str = fileName.split("\\.");
                        if (str[str.length - 1].equals(type)) {
                            fis = new FileInputStream(file);
                            fos = new FileOutputStream(new File(destination, fileName));
                            byte[] buf = new byte[1024];
                            int bytesRead;
                            while ((bytesRead = fis.read(buf)) > 0) {
                                fos.write(buf, 0, bytesRead);
                            }
                        }
                    }
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

运行截图:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4,已知s.txt文件中有这样的一个字符串:“hcexfgijkamdnoqrzstuvwybpl”,请编写程序读取数据内容,把数据排序后写入ss.txt中。
代码:

package 作业5;

import java.io.*;
import java.util.Arrays;

public class read_4 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("E:\\java--idea--1\\s.txt"));
        String s = br.readLine();
        br.close();
        char[] ch = s.toCharArray();
        Arrays.sort(ch);
        String result = new String(ch);
        BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\java--idea--1\\ss.txt"));
        bw.write(result);
        bw.close();
    }
}

运行截图:
在这里插入图片描述

  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值