IO流(随机访问流、合并流、序列化流、Properties集合、JDK7的NIO)

IO流(随机访问流、合并流、序列化流、Properties集合、JDK7的NIO),第1张

IO流(随机访问流、合并流、序列化流、Properties集合、JDK7的NIO) 随机访问流 


 

 写数据

 读数

 

不是12的原因:

指定位置读

import java.io.IOException;
import java.io.RandomAccessFile;

public class Demo {
    public static void main(String[] args) throws IOException {
        read();
        //write();
    }

    static void read()throws IOException {
        RandomAccessFile raf = new RandomAccessFile("a.txt", "rw");
        int i = raf.readInt();
        System.out.println(i);//100
        System.out.println(raf.getFilePointer());//4
        char c = raf.readChar();
        System.out.println(c);//a
        System.out.println(raf.getFilePointer());//6
        String s = raf.readUTF();
        System.out.println(s);//中国
        System.out.println(raf.getFilePointer());//14
        raf.seek(4);
        System.out.println(raf.readChar());//a
    }

    static void write()throws IOException {
        RandomAccessFile raf = new RandomAccessFile("a.txt", "rw");
        raf.writeInt(100);
        raf.writeChar('a');
        raf.writeUTF("中国");
    }
}


合并流

两个文件合并

多个文件合并 

 

 

序列化流

 

类必须需要实现Serializable接口才能序列化或者反序列化

 

序列化 

 

 反序列化

write()——修改class—read(),这样会报错

write()——修改class—重新write()——read(),这样就不会报错

 

transient关键字声明不需要序列化的成员变量

 

 

Properties集合

作为Map集合使用 

特殊功能

getProperty的底层原理:

 

 读写数据

 读数据

写数据

 

 

题目1

我有一个文本文件(a. txt) ,我知道数据是键值对形式的,但是不知道内容是什么。
请写一个程序判断是否有"lisi"这样的键存在,如果有就改变其实为"100"

public class Properties集合修改文本文件 {
    public static void main(String[] args) throws IOException {
        Properties prop = new Properties();
        Reader r = new FileReader("a.txt");
        prop.load(r);
        Set set = prop.stringPropertyNames();
        boolean flag = false;
        for (String s : set) {
            if (s.equals("lisi")) {
                flag = true;
                prop.setProperty("lisi","100");
                break;
            }
        }
        Writer w=new FileWriter("a.txt");
        prop.store(w,null);
        w.close();
        System.out.println(flag);
    }
}

题目2

让猜数字小游戏只能玩五次

package classes.IO流;

import java.io.*;
import java.util.Properties;
import static classes.IO流.登录注册IO实现.game.GuessNumber.start;


public class 五次登陆注册游戏 {
    public static void main(String[] args) throws IOException {
        Properties prop = new Properties();
        Reader r = new FileReader("a.txt");
        prop.load(r);
        r.close();
        Writer w = new FileWriter("a.txt");
        int count = Integer.parseInt(prop.getProperty("count"));
        while (true) {
            if (count > 5) {
                System.out.println("试完结束,请付费");
                break;
            } else {
                start();
                count++;
                String s = String.valueOf(count);
               // String s=Integer.toString(count);这两种都可以
                prop.setProperty("count", s);
                prop.store(w, null);
                w.close();
            }
        }
    }
}

NIO包下的IO流(了解)

 

 

 

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5716307.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存