特殊操作流

目录

前言

一:标准输入输出流

二:打印流

​编辑

字节打印流 

 字符打印流

 案例:复制Java文件(打印流改进版)

三:对象序列化流 

 对象序列化流

 对象反序列化流

 问题分析:

四:Properties 

 Properties作为map集合使用

Properties作为集合的特有用法 

 Properties和IO流结合的方法

 案例:游戏次数

总结


前言

特殊操作流:标准输入输出流,打印流,对象序列化流以及Properties。


一:标准输入输出流

 程序演示:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class SystemInDemo {
    public static void main(String[] args) throws IOException {
/*        InputStream is = System.in;
//        int by;
//        while ((by = is.read()) != -1) {
//            System.out.print((char) by);
//        }
        InputStreamReader isr = new InputStreamReader(is);
        //使用字符流实现一次读取一行
        BufferedReader br = new BufferedReader(isr);*/
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("请输入一个字符串");
        String line = br.readLine();
        System.out.println("你输入的字符串是"+line);

        System.out.println("请输入一个整数");
        int i = Integer.parseInt(br.readLine());
        System.out.println("你输入的整数是"+i);
        
    }
}
import java.io.PrintStream;

public class SystemOutDemo {
    public static void main(String[] args) {
        PrintStream ps = System.out;
//        ps.print("hello");
//        ps.print(100);
        ps.println("hello");
        ps.println(100);
    }
}

二:打印流

字节打印流 

import java.io.IOException;
import java.io.PrintStream;

public class PrintStreamDemo {
    public static void main(String[] args) throws IOException {
        PrintStream ps = new PrintStream("myOtherStream\\ps.txt");
        //写数据
        //字节输出流的方法
//        ps.write(97);
        //使用特有方法
        ps.println(97);
        ps.println(98);

        ps.close();
    }
}

 字符打印流

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

public class PrintWriterDemo {
    public static void main(String[] args) throws IOException {
//        PrintWriter pw = new PrintWriter("myOtherStream\\ps.txt");
//        pw.write("hello");
//        pw.write("\n");
//        pw.write("world");
//        pw.flush();
//        pw.println("hello");
//        pw.flush();
//        pw.println("world");
//        pw.flush();
        PrintWriter pw = new PrintWriter(new FileWriter("myOtherStream\\ps.txt"),true);
        pw.println("hello");
        pw.close();

    }
}

 案例:复制Java文件(打印流改进版)

import java.io.*;

//需求:把模块目录下的PrintStreamDemo.java复制到模块目录下的Copy.java "myOtherStream\\src\\com\\itheima02\\PrintStreamDemo.java" "myOtherStream\\Copy.java"
public class Demo {
    public static void main(String[] args) throws IOException {

/*        BufferedReader br = new BufferedReader(new FileReader("myOtherStream\\src\\com\\itheima02\\PrintStreamDemo.java"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("myOtherStream\\Copy.java"));

        String line;
        while((line = br.readLine())!= null) {
            bw.write(line);
            bw.newLine();
            bw.flush();
        }

        br.close();
        bw.close()*/;
        BufferedReader br = new BufferedReader(new FileReader("myOtherStream\\src\\com\\itheima02\\PrintStreamDemo.java"));
        PrintWriter pw = new PrintWriter(new FileWriter("myOtherStream\\Copy.java"),true);

        String line;
        while((line = br.readLine())!= null) {
            pw.println(line);
        }
        pw.close();
        br.close();
    }
}

 三:对象序列化流 

 对象序列化流

import java.io.Serializable;

public class Student implements Serializable {
    private  String name;
    private int age;

    public Student() {
    }

    public Student(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.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class ObjectOutPutStreamDemo {
    public static void main(String[] args) throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("myOtherStream\\oos.txt"));
        Student s1 = new Student("路飞",23);
        oos.writeObject(s1);
        oos.close();
    }
}

 对象反序列化流

import java.io.*;

public class ObjectOutPutStreamDemo {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("myOtherStream\\oos.txt"));
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("myOtherStream\\oos.txt"));
        Student s1 = new Student("路飞",23);
        oos.writeObject(s1);
        Object obj = ois.readObject();
        Student s = (Student) obj;
        System.out.println(s.getName()+","+s.getAge());


        oos.close();
        ois.close();
    }
}

 问题分析:

 


四:Properties 

 Properties作为map集合使用

import java.util.Properties;
import java.util.Set;

public class PropertiesDemo01 {
    public static void main(String[] args) {
        Properties prop = new Properties();
        //存储元素
        prop.put("001","A");
        prop.put("002","B");
        prop.put("003","C");

        Set<Object> keySet = prop.keySet();
        for (Object key:keySet) {
            Object value = prop.get(key);
            System.out.println(key+","+value);
        }
    }
}

Properties作为集合的特有用法 

package com.itheima04;

import java.util.Properties;
import java.util.Set;

public class PropertiesDemo02 {
    public static void main(String[] args) {
        Properties prop = new Properties();

        prop.setProperty("001","A");
        prop.setProperty("002","A");
        prop.setProperty("003","A");

        System.out.println(prop.getProperty("001"));

        Set<String> names = prop.stringPropertyNames();
        System.out.println(names);


        System.out.println(prop);
    }
}

 Properties和IO流结合的方法

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

public class PropertiesDemo03 {
    public static void main(String[] args) throws IOException {
        //把集合中的数据保存到文件
//        myStore();

        //把文件中的数据加载到集合
        myLoad();
    }

    private static void myLoad() throws IOException {
        Properties prop = new Properties();
        FileReader fr = new FileReader("myOtherStream\\fw.txt");
        prop.load(fr);
        fr.close();
        System.out.println(prop);
    }

    private static void myStore() throws IOException {
        Properties prop = new Properties();
        prop.setProperty("001","A");
        prop.setProperty("002","B");
        prop.setProperty("003","C");

        FileWriter fw = new FileWriter("myOtherStream\\fw.txt");
        prop.store(fw,null);
        fw.close();
    }
}

 案例:游戏次数

需求:实现猜数字小游戏只能试玩三次,如果还想玩,提示:游戏试玩已结束,想玩请充值(www.itcast.cn)。

初始化 game.txt

count=0

Game类

import java.util.Random;
import java.util.Scanner;

public class Game {
    public Game() {
    }
    public static void start() {
        Random r = new Random();
        int number = r.nextInt(100) + 1;
        while (true) {
            Scanner sc = new Scanner(System.in);
            System.out.println("输入你要猜的数字");
            int guessNumber = sc.nextInt();

            if (guessNumber>number) {
                System.out.println("你猜的数字"+guessNumber+"大了");
            }else if (guessNumber<number) {
                System.out.println("你猜的数字"+guessNumber+"小了");
            }else {
                System.out.println("恭喜你猜中了!");
                break;
            }
        }
    }
}

 主程序

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

//游戏次数
public class Demo {
    public static void main(String[] args) throws IOException {
        Properties prop = new Properties();

        FileReader fr = new FileReader("myOtherStream\\game.txt");

        prop.load(fr);
        fr.close();
        String count = prop.getProperty("count");
        int number = Integer.parseInt(count);

        if (number>=3) {
            System.out.println("游戏试玩已结束,想玩请充值(www.itcast.cn)");
        } else {
            Game.start();
            number++;
            String s = String.valueOf(number);
            prop.setProperty("count",s);
            FileWriter fw = new FileWriter("myOtherStream\\game.txt");
            prop.store(fw,null);
            fw.close();

        }
    }

}

总结

       总结特殊操作流,包括:标准输入输出流,打印流,对象序列化流以及Properties。并通过编写案例熟悉应用。

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值