2021-05-17

课堂代码

import java.io.File;


public class Demo01 {
    public static void main(String[] args) {
        // 展示文件树
        File f = new File("d:/demo01");
        printTree( f , 0);
    }
    public static void printTree(File f , int level ) {
        for(int i = 0 ; i < level ; i ++ ) {
            System.out.print("\t");
        }
        System.out.println(f.getAbsolutePath());
        if ( f.isDirectory() ) {
            level ++;
            File[] strs = f.listFiles();
            for ( int i = 0 ; i < strs.length ; i ++ ) {
                File f0 = strs[i];
                printTree( f0 , level + 1);
            }
        }


    }

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


// 实现文件的赋值
public class Demo02 {
    public static void main(String[] args) throws IOException {
        FileInputStream fi = null;
        FileOutputStream fo = null;
        try {
            // 1 准备输入输出流
             fi = new FileInputStream("D:\\ideaIC-2020.3.2.exe");
             fo = new FileOutputStream("C:\\des\\idea_copy.exe");

            int len =0;
            byte[] b = new byte[1024];
            long startTime = System.currentTimeMillis();
            while (  (len = fi.read(b)) != -1 ) {
                fo.write(b , 0 , len );
            }
            long endTime = System.currentTimeMillis();
            System.out.println(endTime-startTime);
        }catch (IOException e) {
            System.out.println("文件路劲不存在");
        }finally {
            fi.close();
            fo.close();
        }

import java.io.*;
import java.util.Date;
import java.util.Timer;


// 实现文件的赋值
public class Demo03 {
    public static void main(String[] args) throws IOException {
        BufferedInputStream fi = null;
        BufferedOutputStream fo = null;
        try {
            // 1 准备输入输出流
             fi = new BufferedInputStream(new FileInputStream("D:\\ideaIC-2020.3.2.exe"));
             fo = new BufferedOutputStream( new FileOutputStream("C:\\des\\idea_copy.exe"));

            int len =0;
            byte[] b = new byte[1024];
            long startTime = System.currentTimeMillis();
            while (  (len = fi.read(b)) != -1 ) {
                fo.write(b , 0 , len );
            }
            long endTime = System.currentTimeMillis();
            System.out.println(endTime-startTime);
        }catch (IOException e) {
            System.out.println("文件路劲不存在");
        }finally {
            fi.close();
            fo.close();
        }

    }
}

import java.io.*;


public class Demo04 {
    public static void main(String[] args) throws Exception {
        Reader reader = new FileReader("D:\\three.txt");
        Writer writer = new FileWriter("C:\\des\\hah.txt");
        int len = 0 ;
        char [] buffer  = new char[1024];
        long start = System.currentTimeMillis();
        while ( (len = reader.read(buffer) ) != -1) {
            writer.write(buffer , 0 , len);
        }
        long end = System.currentTimeMillis();
        System.out.println( end -start );
        reader.close();
        writer.close();
    }
}

import java.io.*;


public class Demo05 {
    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new FileReader("D:\\three.txt"));
        BufferedWriter writer = new BufferedWriter(new FileWriter("C:\\des\\hah.txt"));
        int len = 0 ;
        char [] buffer  = new char[1024];
        long start = System.currentTimeMillis(); // 获取程序执行之前的时间戳
        while ( (len = reader.read(buffer) ) != -1) {
            writer.write(buffer , 0 , len);
        }
        writer.flush();
        long end = System.currentTimeMillis();
        System.out.println("时间:" + (end -start) );
        reader.close();
        writer.close();

    }
}

import java.io.*;
import java.util.Map;
import java.util.Properties;


public class Demo06 {
    public static void main(String[] args) throws IOException {
        // 1 用输入流指向硬盘文件
        BufferedInputStream bfi = new BufferedInputStream(new FileInputStream("a.properties" )) ;
        // 2 工具类
        Properties pro = new Properties();
        // 3 jdk中的工具类和输入流挂钩
        pro.load(bfi);
        bfi.close();
        // 4 访问其中的值
        for (Map.Entry e : pro.entrySet() ) {
            System.out.println(e);
        }

        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("a.properties", false));
        pro.setProperty("high" , "188" );
        pro.store(bos,"新添加了身高");

        bos.close();


    }
}

import java.io.File;
import java.io.IOException;

public class Main {

    public static void main(String[] args)  {
        File file = new File("D:\\classthree.txt");
        File file1 = new File("d:/three.txt");
        File file2 = new File("d:/ch10/demo01");
        File file3 = new File("d:/ch010");
        try {
            file.createNewFile();
            file1.createNewFile();
            file2.mkdirs();
            /*file2.mkdirs();  // 创建多层文件夹
            file3.mkdir();  // 创建一层文件夹
            file1.delete();  // 删除文件
            file2.delete();  // 删除为空的那一级文件夹
            file3.renameTo(new File("d:/ch0010")); // 修改路径*/
            System.out.println(file.exists());
            System.out.println(file.isFile());
            System.out.println(file2.isDirectory());
            System.out.println(file2.getPath());

        }catch (Exception e ) {
            e.printStackTrace();
        }


    }
}

你可能感兴趣的:(2021-05-17)