学习进度P351-P400

学习内容:视频P351-P400

  1. File文件的遍历搜索
		public static void method05(File file) {
//        File[] files = file.listFiles(new FileFilter() {
//            @Override
//            public boolean accept(File pathname) {
//                return pathname.isDirectory()||pathname.getName().toLowerCase().endsWith("txt");
//            }
//        });
        File[] files = file.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                File pathname = new File(dir,name);
                return pathname.isDirectory()||pathname.getName().toLowerCase().endsWith("txt");
            }
        });

        for (File file1 : files) {
            if (file1.isDirectory()){
                method05(file1);
            }else
            System.out.println(file1);
        }


    }
  1. 递归的概念和使用
  2. 练习1-n之间的和
	public static int method01(int n) {

        if (n != 1) {
            return n + method01(n - 1);

        } else {
            return 1;
        }
    }
  1. 递归阶层计算
	public static int method02(int n) {
        if (n == 1) {
            return 1;
        } else {

            return n * method02(n - 1);
        }
    }

IO流视频P359-P400

  1. 字符输出流、字符输入流、字节输出流、字节输入流、flush和close的区别、try、catch、finally处理异常、Properties集合的store和load方法、缓冲流的概念、字节缓冲输入流和输出流、字符缓冲输出流和输入流、字符编码集、转换流的使用、对象的序列化、对象的反序列化、transient瞬间关键字
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;

public class IODemo {
    public static void main(String[] args) throws IOException {
        method03();
    }
    public static void method01() throws IOException {
        FileOutputStream fos = new FileOutputStream("D:\\ideaProjects\\Demo_java\\javaTest\\src\\April\\Apr19th\\a.txt");
        byte[] bytes = "张三".getBytes();
        fos.write(bytes);
        System.out.println(Arrays.toString(bytes));
        fos.close();
    }
    // \r\n 为换行,构造方法true为追加模式
    public static void method2() throws IOException {
        FileOutputStream fos = new FileOutputStream("D:\\ideaProjects\\Demo_java\\javaTest\\src\\April\\Apr19th\\c.txt",true);


        fos.write("追加内容".getBytes());
        for (int i = 0; i < 10; i++) {
            fos.write("你好".getBytes());
            fos.write("\r\n".getBytes());
        }
        fos.close();
    }

    //输入流
    public static void method03() throws IOException {
        FileInputStream fis = new FileInputStream("D:\\ideaProjects\\Demo_java\\javaTest\\src\\April\\Apr19th\\c.txt");
        //read一次读取一个,结尾返回-1
        int len = 0;
        while ((len = fis.read() )!= -1){
            System.out.println((char)len);
        }
        //释放资源
        fis.close();
    }
}

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

public class Demo001 {
    public static void main(String[] args) throws IOException {
        method02();
    }

    public static void method01() throws IOException {
        FileInputStream fis = new FileInputStream("D:\\ideaProjects\\Demo_java\\javaTest\\src\\April\\Apr20th\\a.txt");
        FileOutputStream fos = new FileOutputStream("D:\\ideaProjects\\Demo_java\\javaTest\\src\\April\\Apr20th\\b.txt");
        int temp = 0;
        while ((temp = fis.read()) != -1) {
            fos.write(temp);
        }

    }

    public static void method02() throws IOException {
        FileReader fr = new FileReader("D:\\ideaProjects\\Demo_java\\javaTest\\src\\April\\Apr20th\\a.txt");
        char[] cs = new char[1024];
        int len = 0;
        while ((len = fr.read(cs))!= -1){
            System.out.println(new String(cs,0,len));
        }
        fr.close();

    }
}

import javax.imageio.IIOException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

public class Demo02 {
    public static void main(String[] args) {
        method01();
    }

    public static void method01(){
        Properties prop = new Properties();
        prop.setProperty("张三","18岁");
        prop.setProperty("李四","20岁");
        prop.setProperty("王五","45岁");
        Set<String> set = prop.stringPropertyNames();
        Iterator<String> iterator = set.iterator();
        while (iterator.hasNext()){
            String key = iterator.next();
            String value = prop.getProperty(key);
            System.out.println(key+" "+value);
        }

    }
    //处理io流异常try catch
    public static void method03(){
        FileWriter fw = null;
        try {

            fw = new FileWriter("D:\\ideaProjects\\Demo_java\\javaTest\\src\\April\\Apr20th\\a.txt",true);
            char[] cs = {'z','b','x'};
            int len = 0;
            for (int i = 0; i < 10; i++) {
                fw.write(cs,0,2);
                fw.write("\r\n");
            }
        }catch (IOException e){
            System.out.println(e);
        }finally {
            try {
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

import java.io.Serializable;

public class Person implements Serializable {
    private String name;
    //transient 修饰的对象不能被序列化 结果为默认值
    private transient int age;

    public Person() {
    }
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }


}

import java.io.*;
import java.util.HashMap;
import java.util.Properties;
import java.util.Set;

public class Demo01 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        method11();
    }

    public static void methodLoad01() throws IOException {
        Properties prop = new Properties();
        prop.load(new FileReader("javaTest\\src\\April\\Apr21th\\a.txt"));
        Set<String> set = prop.stringPropertyNames();
        for (String key : set) {
            String value = prop.getProperty(key);
            System.out.println(key + "" + value);

        }
    }

    public static void method02() throws IOException {
        FileOutputStream fos = new FileOutputStream("javaTest\\src\\April\\Apr21th\\a.txt", true);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        byte[] b = {12, 32, 65, 97, 48};
        bos.write(b);
        bos.write("张三".getBytes());
        bos.flush();
        bos.close();
    }

    public static void method03() throws IOException {
        FileInputStream fis = new FileInputStream("javaTest\\src\\April\\Apr21th\\a.txt");
        BufferedInputStream bis = new BufferedInputStream(fis);
        byte[] by = new byte[1024];
        long s = System.currentTimeMillis();
        int len = 0;
        while ((len = bis.read(by)) != -1) {
            System.out.println(new String(by, 0, len));
        }
        long e = System.currentTimeMillis();
        bis.close();
        System.out.println(e - s + "毫秒");

    }

    public static void method04() throws IOException {
        FileWriter w = new FileWriter("javaTest\\src\\April\\Apr21th\\a.txt", true);
        BufferedWriter bw = new BufferedWriter(w, 1024);
        bw.write("abcde", 0, 5);
        //输入换行,不同系统都可以通用\r\n   /r  /n
        bw.newLine();
        bw.flush();
        bw.close();

    }

    public static void method05() throws IOException {
        FileReader fr = new FileReader("javaTest\\src\\April\\Apr21th\\a.txt");
        BufferedReader br = new BufferedReader(fr, 1024);
        //自动读取一行,遇到换行符终止 、\r\n /r /n   此处为不等于null
        while (br.readLine() != null) {
            System.out.println(br.readLine());
        }
        br.close();

    }
    public static void method06() throws IOException {
        HashMap<String,String> map =new HashMap<>();
        FileReader fr = new FileReader("javaTest\\src\\April\\Apr21th\\read.txt");
        FileWriter fw = new FileWriter("javaTest\\src\\April\\Apr21th\\a.txt",true);
        BufferedReader br =new BufferedReader(fr);
        BufferedWriter bw =new BufferedWriter(fw);
        String line ;
        while ((line = br.readLine())!=null){
            String[] arr = line.split("\\.");
            map.put(arr[0],arr[1]);
        }
        String line2;
        for (String key:map.keySet()){
            String value = map.get(key);
            System.out.println(key+"."+value);
            line2 = key+"."+value;
            bw.write(line2);
            bw.newLine();
        }
        //关闭资源
        bw.close();
        br.close();
    }
    public static void method07() throws IOException {
        FileOutputStream fw = new FileOutputStream("javaTest\\src\\April\\Apr21th\\a.txt",true);
        OutputStreamWriter osw = new OutputStreamWriter(fw,"GBK");
        osw.write("张三");
        osw.flush();
        osw.close();
    }
    public static void method08() throws IOException {
        FileInputStream fis = new FileInputStream("javaTest\\src\\April\\Apr21th\\read.txt");
        InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
        int len =0;
        while ((len = isr.read())!= -1){
            System.out.println((char) len);
        }
        isr.close();
    }
    public static void method09() throws IOException {
        FileInputStream fis = new FileInputStream("javaTest\\src\\April\\Apr21th\\a1.txt");
        InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
        FileOutputStream fos = new FileOutputStream("javaTest\\src\\April\\Apr21th\\b1.txt");
        OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");
        int len =0;
        while ((len = isr.read())!= -1){
            System.out.println((char) len);
            osw.write(len);
        }
        osw.close();
        fos.close();

    }
    //序列化将对象类型写入文件中,用到ObjectOutputStream,书写的类需要实现Serializable接口,次接口仅为标记
    public static void method10() throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("javaTest\\src\\April\\Apr21th\\a1.txt"));
        oos.writeObject(new Person("张三",18));
        oos.close();
    }
    //反序列化readObject()类有两个异常IOException, ClassNotFoundException,需要都抛出
    public static void method11() throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("javaTest\\src\\April\\Apr21th\\a1.txt"));
        Object o = ois.readObject();
        ois.close();
        //打印读取到的对象资源
        System.out.println(o);
        Person p = (Person) o;
        System.out.println(p.getName()+" "+p.getAge());


    }
    //static修饰的变量不能被序列化 transient也一样,只不过仅有不能被序列化
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值