javaSE(17)(打印流、转换流、对象流、配置文件和递归)

打印流:
package zz.itheima.printstreamandobjectstream;

import java.io.PrintStream;
import java.io.PrintWriter;

public class TestPrintStream {

    public static void main(String[] args) throws Exception{
        //打印流
        // 演示打印流PrintStream(字节打印流)和PrintWriter(字符打印流),官方推荐使用字符打印流
        String s = "hello";
        boolean b = true;
        int age = 20;
        double price = 19.8;
        //把上面四个变量的值写到一个文本文件中
        //用字节流
        //write(int c) write(byte[] b)
        //用字符流
        //write(int c) write(String s)

        //用打印流PrintStream
        /*public PrintStream(File file) public PrintStream(String fileName) public void flush() public void close() public void print(Xxx x) pulic void println(Xxx x)*/
    /* PrintStream ps = new PrintStream("demo.txt"); ps.println(s); ps.println(b); ps.println(age); ps.println(price); ps.close();*/

        //使用字符输出流
        PrintWriter pw = new PrintWriter("demo.txt");
        pw.println(s);
        pw.println(b);
        pw.println(age);
        pw.println(price);
        pw.close();
    }

}
对象流:
package zz.itheima.printstreamandobjectstream;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class TestObjectStream {

    public static void main(String[] args)throws Exception {
        //对象流
        /*ObjectOutputStream 对象序列化:把对象以流的形式写入文件 public ObjectOutputStream(OutputStream out) public void close() public void flush() public final void writeObject(Object obj)*/
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("book.txt"));
        oos.writeObject(new Book("aaa", 10));
        oos.close();

        /*ObjectInputStream 对象进行反序列化:以流的形式从文件中读取对象 public ObjectInputStream(InputStream in) public void close() public final Object readObject()*/
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("book.txt"));
        Book b = (Book)ois.readObject();
        ois.close();
        System.out.println(b.getName());
    }

}
递归:
package zz.itheima.printstreamandobjectstream;

public class TestRecursion {

    //递归
    //递归就是方法自身调用自身,会产生类似循环的效果
    //盗梦空间...(梦中梦)
    //必须有条件出口,就是能退出方法的条件
    //在控制台换行输出10次"java",分别使用for和递归实现
    public static void test1(){
        for (int i = 0; i < 10; i++) {
            System.out.println("java");
        }
    }
    public static void test2(int n){
        System.out.println("java");
        if (n==0) {
            return;
        }
        test2(--n);

    }
    public static void main(String[] args) {
        test2(10);
    }

}
输出指定目录下的所有文件:
package zz.itheima.printstreamandobjectstream;

import java.io.File;

public class TestRecursionDir {

    // 输出指定目录下的所有文件(包括子文件夹)
    public static void recursionDir(String path){
        File f = new File(path);
        String[] names = f.list();
        for (int i = 0; i < names.length; i++) {
            if (names[i].endsWith(".java")) {
                System.out.println(names[i]);
                File ziFile = new File(path+"\\"+names[i]);
                if (ziFile.isDirectory()) {
                    recursionDir(path+"\\"+names[i]);

                }
            }
        }


    }
    public static void main(String[] args) {
        recursionDir("E:\\work");
    }

}
向文本文件中写入对象:
package zz.itheima.printstreamandobjectstream;

import java.io.FileWriter;
import java.io.PrintWriter;

public class TestWriteObject {

    public static void main(String[] args) throws Exception{
        // 向文本文件中写入对象
        //FileWriter
        //write(int c) write(String s) write(char[] c)
        FileWriter fw=new FileWriter("demo.txt");
        fw.write(new Book("asdf",100).toString());
        fw.close();

        //PrintWriter
        PrintWriter pw=new PrintWriter("book.txt");
        pw.print(new Book("asdf",100));
        pw.close();
    }

}
属性配置文件:
package zz.itheima.printstreamandobjectstream;

import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Properties;
import java.util.Set;

public class TestProperties {

    public static void main(String[] args)throws Exception {
        //属性配置文件,存储的是键值对
    // 演示java.util.Properties

        /*Properties p=new Properties(); //存储方式跟HashMap一样 p.put("a", "aa"); p.put("b", "aa"); p.put("c", "aa"); System.out.println(p); HashMap hm=new HashMap(); hm.put("a", "aa"); hm.put("b", "aa"); hm.put("c", "aa"); System.out.println(hm);*/

        /* public Object setProperty(String key,String value) public void list(PrintWriter out) public void store(Writer writer, String comments) public void load(Reader reader) public String getProperty(String key) public Set<String> stringPropertyNames()*/
        Properties ps = new Properties();
        ps.setProperty("阿发", "aa");
        ps.setProperty("b", "aa");
        ps.setProperty("c", "aa");

        PrintWriter pw = new PrintWriter("demo.txt");
        ps.list(pw);//把存储的键值对存储到文本文件中
        pw.close();

        //读取
        ps.load(new FileReader("demo.txt"));//从文本文件中读取(加载)键值对
        System.out.println(ps);
        Set keys = ps.keySet();
        for (Object key : keys) {
            String value = (String) ps.get(key);
            System.out.println(key+"="+value);

        }




    }

}
bean类:
package zz.itheima.printstreamandobjectstream;

import java.io.Serializable;

public class Book implements Serializable{
private static final long serialVersionUID = -3137157247111227006L;

    private String name;
    private int price;

    public Book() {
        super();
    }

    public Book(String name, int price) {
        super();
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

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

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

/* @Override public String toString() { return "Book [name=" + name + ", price=" + price + "]"; }*/
}
例子一:
package zz.itheima.printstreamandobjectstream;

import java.io.PrintWriter;
import java.util.Scanner;

public class Demo1 {

    public static void main(String[] args)throws Exception  {
        // 让用户在控制台录入学生的姓名、性别、年龄等信息,并使用PrintWriter写入到一个文本文件中
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入姓名:");
        String name=sc.next();
        System.out.println("请输入性别:");
        String sex=sc.next();
        System.out.println("请输入年龄:");
        int age=sc.nextInt();

        PrintWriter pw=new PrintWriter("demo.txt");
        pw.println(name);
        pw.println(sex);
        pw.println(age);
        pw.close();
    }

}
例子二:
package zz.itheima.printstreamandobjectstream;

import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Properties;

public class Demo2 {

    public static void main(String[] args) throws Exception{
        // 使用Properties类结合IO流把身份证号和姓名这个键值对写到一个属性配置文件中,然后再读出来
        Properties ps = new Properties();
        ps.put("123", "zhangsan");
        ps.put("456", "lisi");
        ps.put("1789", "wangwu");
        PrintWriter pw = new PrintWriter("demo.txt");
        ps.list(pw);
        pw.close();

        ps.load(new FileReader("demo.txt"));//加载(读取文本文件中的键值对)
        System.out.println(ps);
    }

}
例子三:
package zz.itheima.printstreamandobjectstream;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class Demo3 {

    public static void main(String[] args)throws Exception {
        // 创建一个Book类(书名,价格),创建几个对象,然后实现序列化和反序列化
        ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("book.txt"));
        oos.writeObject(new Book("aaa", 1000));
        oos.writeObject(new Book("bbb", 1000));
        oos.close();

        ObjectInputStream ois =new ObjectInputStream(new FileInputStream("book.txt"));

        while (true) {
            try {
                Book b=(Book)ois.readObject();
                System.out.println(b.getName());
            } catch (Exception e) {
                break;
            }

        }

        ois.close();
    }

}

你可能感兴趣的:(递归,打印流-转换流-对象)