java基礎篇-----IO流及File類

在這裏插入圖片描述例1:鍵盤錄入3個學生信息(姓名, 語文成績(chineseScore), 數學成績(mathScore), 英語成績(englishScore)),按照總分從高到低存入文本文件

   public class MyTest {
    public static void main(String[] args) throws IOException {
        TreeSet<Student> set = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                //總分相同,不能說明是同一個人
                int num = s1.getTotalScore() - s2.getTotalScore();
                int num2 = num == 0 ? s1.getName().compareTo(s2.getName()) : num;
        Scanner sc = new Scanner(System.in);
        for (int i = 1; i <= 3; i++) {
            Student student = new Student();
            System.out.println("請輸入第" + i + "個學生的姓名");
            String name = sc.nextLine();
            student.setName(name);
            System.out.println("請輸入第" + i + "個學生的語文成績");
            int chineseScore = Integer.parseInt(sc.nextLine());
            student.setChineseScore(chineseScore);
            System.out.println("請輸入第" + i + "個學生的數學成績");
            int mathScore = Integer.parseInt(sc.nextLine());
            student.setMathScore(mathScore);
            System.out.println("請輸入第" + i + "個學生的英語成績");
            int englishScore = Integer.parseInt(sc.nextLine());
            student.setEnglishScore(englishScore);
            //再把學對象存到集合中
            set.add(student);
        }

        BufferedWriter out = new BufferedWriter(new FileWriter("studentScore.txt",true));
        out.write("序號\t"+"姓名\t"+"語文成績\t"+"數學成績\t"+"英語成績\t"+"總分");
        out.newLine();
        out.flush();
        //遍歷集合把數據存到文本文件中去
        int index=1;
        for (Student student : set) {
            out.write((index++)+"\t"+student.getName()+"\t"+student.getChineseScore()+"\t"+student.getMathScore()+"\t"+student.getEnglishScore()+"\t"+student.getTotalScore());
            out.newLine();
            out.flush();
        }

        out.close();
    }
}
public class Student {
    private String name;
    private int chineseScore;
    private int mathScore;
    private int englishScore;

    public String getName() {
        return name;
    }

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

    public int getChineseScore() {
        return chineseScore;
    }

    public void setChineseScore(int chineseScore) {
        this.chineseScore = chineseScore;
    }

    public int getMathScore() {
        return mathScore;
    }

    public void setMathScore(int mathScore) {
        this.mathScore = mathScore;
    }

    public int getEnglishScore() {
        return englishScore;
    }

    public void setEnglishScore(int englishScore) {
        this.englishScore = englishScore;
    }

    public int getTotalScore(){
        return chineseScore+mathScore+englishScore;
    }

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

例2:複製單級文件夾:
    思路:1.封裝整個原文件夾
               2.創建目標文件夾
               3.遍歷源文件夾,將原文件夾下的所有文件,複製到目標文件夾
複製多級文件夾:
    思路:1.封裝整個原文件夾
               2.創建目標文件夾
               3.遍歷源文件夾,將原文件夾下的所有文件,複製到目標文件夾
public class CopyFileDemo {
    public static void main(String[] args) throws IOException {
        複製單個文件
        File srcFolder = new File("E:\\test");
        //封裝目標文件夾
        File targetFolder = new File("D:\\test");
        //如果目標文件夾不存在,就創建出來
        if(!targetFolder.exists()){
            targetFolder.mkdirs();
        }
        //進行復制操作
        copyFolders(srcFolder,targetFolder);


    }

    private static void copyFolders(File srcFolder, File targetFolder) throws IOException {
        //實現複製操作
        File[] files = srcFolder.listFiles(); //獲取該目錄下,所有的文件和文件夾
        //遍歷複製每個文件
        for (File f : files) {
            //複製文件的操作
            copyFile(f,targetFolder);
        }

    }

    private static void copyFile(File f, File targetFolder) throws IOException {
        FileInputStream in = new FileInputStream(f);
        FileOutputStream out = new FileOutputStream(new File(targetFolder, f.getName()));
        byte[] bytes = new byte[1024 * 8];
        int len=0;
        while ((len=in.read(bytes))!=-1){
            out.write(bytes,0,len);
            out.flush();
        }
        in.close();
        out.close();
    }
}


public class CopyFileDemo2 {
    public static void main(String[] args) throws IOException {
        //複製整個文件:複製多級文件夾
        File srcFolder = new File("E:\\test");
        //封裝目標文件夾
        File targetFolder = new File("D:\\test");
        //如果目標文件夾不存在,就創建出來
        if(!targetFolder.exists()){
            targetFolder.mkdirs();
        }
        //進行復制操作
        copyFolders(srcFolder,targetFolder);


    }

    private static void copyFolders(File srcFolder, File targetFolder) throws IOException {
        //實現複製操作
        File[] files = srcFolder.listFiles(); //獲取該目錄下,所有的文件和文件夾
        //遍歷複製每個文件
        for (File f : files) {
            if(f.isFile()){
                //複製文件的操作
                copyFile(f, targetFolder);
            }else{
                //如果是文件夾,
               // 1. 封裝整個原文件夾
                //2.創建目標文件夾
                //3.遞歸調用
            }

        }

    }

    private static void copyFile(File f, File targetFolder) throws IOException {
        FileInputStream in = new FileInputStream(f);
        FileOutputStream out = new FileOutputStream(new File(targetFolder, f.getName()));
        byte[] bytes = new byte[1024 * 8];
        int len=0;
        while ((len=in.read(bytes))!=-1){
            out.write(bytes,0,len);
            out.flush();
        }
        in.close();
        out.close();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章