java进阶笔记Day(十一)

标准的输入输出流(了解)
System.in: 表示的输入流,用于读取键盘录入的数据;它是属于InputStream的子类
Scanner内部就是使用的System.in
System.out: 表示的输出,用于往控制台输出数据;它是属于OutputStream的子类
System.out.println(“hello”);
打印流(了解)

PrintStream和PrintWriter都是打印流,只有输出流没有输入流(也就是说,只能写,不能读)

PrintWriter可以往不同的目的地写数据
//目的地是一个文件路径
PrintWriter pw1=new PrintWriter(“day10/a.txt”);
//目的地是一个File对象表示的路径
PrintWriter pw2=new PrintWriter(new File(“day10/a.txt”));
//目的地是一个输出流
PrintWriter pw4=new PrintWriter(new FileWriter(“day10/a.txt”));
//目的地是控制台
PrintWriter pw5=new PrintWriter(System.out);

PrintWriter和PrintStream有两个特有的方法
print: 可以输出任何类型的数据,都会原样输出; 不会换行
println: 可以输出任何类型的数据,都会原样输出;会换行

PrintWriter和PrintStream在创建对象时,可以指定一个boolean类型的参数true,表示自动刷新
//第二个参数true: 表示自动刷新
PrintWriter pw3=new PrintWriter(new FileOutputStream(“day10/a.txt”),true);
对象序列化流(了解)
对象序列化流用于把对象写到文件中或者把文件中的对象读取出来。

public class Student implements Serializable {
private String name;
private int age;
//被transient修饰的变量,不被序列化
private transient String sex;

//序列号
private static final long serialVersionUID = -68497944754667710L;

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

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

}
//序列化:把对象写到文件
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(“day10/a.txt”));
Student stu=new Student(“小明”,18);
//把对象写到文件中
oos.writeObject(stu);
//释放资源
oos.close();

//反序列化:把文件中的对象读取出来
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(“day10/a.txt”));
//把对象写到文件中
Object obj = ois.readObject();
System.out.println(obj);
//释放资源
ois.close();
注意事项

  1. 使用序列化流操作的对象,需要实现一个接口Serializable接口,才能进行序列化和反序列化
  2. 为了保证序列化和反序列化的序列号不变,我们可以写一个固定的序列号
    private static final long serialVersionUID = -68497944707546677L;
  3. 瞬态:被transient修饰的变量,就不能被序列化
    Properties集合(重点)
    Properties是属于Map集合的子类,所有Map集合的方法它都可以用。它还具备一些特有的方法。

public String getProperty(String key)
根据键获取值; 类似于get方法
public Object setProperty(String key, String value)
添加键和值,如果有键重复会覆盖原的值,并且把被覆盖的值返回。类似于put方法
public Set stringPropertyNames()
获取所有的键;类似于keySet方法

public void load(Reader reader)
把文件中的键和值读取到Properties集合中
public void store(Writer writer, String comments)
把Properties集合中的键和值写到文件中
第二个参数String comment:表示注释,如果没有可以写null

Properties pro=new Properties();
//添加键和值
pro.setProperty(“张三”,“武汉”);
pro.setProperty(“张三”, “大武汉”); //“大武汉” 覆盖 “武汉”
pro.setProperty(“王五”,“北京”);
pro.setProperty(“李四”,“上海”);

//获取所有的键
Set keys = pro.stringPropertyNames();
for (String key : keys) {
//根据键获取值
String value = pro.getProperty(key);
System.out.println(key+","+value);
}
统计程序使用次数

/*
思路:
1.写一个配置文件(config.properties),用来存储程序的使用次数
count=3
2.读取配置文件获取键对应的值
3.判断count对应的值是否大于0
大于0: 说明有使用次数,每次使用值递减1,再存回config.properties文件中
等于0: 说明没有使用次数,请充值
*/
//2.读取配置文件获取键对应的值
Properties pro=new Properties();
pro.load(new FileReader(“day10/config.properties”));
String count = pro.getProperty(“count”);

//3.判断count对应的值是否大于0
int intCount = Integer.parseInt(count);
if(intCount>0){
//说明有使用次数,每次使用值递减1,再存回config.properties文件中
System.out.println(“欢迎使用本软件_”);
intCount–;
pro.setProperty(“count”,intCount+"");
pro.store(new FileWriter(“day10/config.properties”),null);
}else{
//小于等于0: 说明没有使用次数,请充值
System.out.println(“使用次数已到,请充值 http://www.itheima.cn”);
}
IO流的异常处理

public class Demo5 {
public static void main(String[] args) {
FileReader fr=null;
FileWriter fw=null;
try {
fr=new FileReader(“day10/a.txt”);
fw=new FileWriter(“day10/b.txt”);
char[] chs=new char[1024];
int len; //记录每次读取的字节个数
while ((len=fr.read(chs))!=-1){
fw.write(chs,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//释放资源的操作放在finaly里面
if(fw!=null){
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fr!=null){
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值