博客
关于我
I/O--(下)非流结构&内存流&读档&登录校验
阅读量:556 次
发布时间:2019-03-08

本文共 3534 字,大约阅读时间需要 11 分钟。

Java IO流操作详解

登录注册流程

1.1 登录注册校验

DAO接口操作集

  • 接口定义:定义注册和登录的操作接口。
    interface UserDao {  public abstract boolean isLogin(String username, String password);  public abstract void regist(User user);}
  • 实现类:实现类通过静态初始化块确保文件的存在。
    class UserDaoImpl implements UserDao {  private static File file = new File("user.txt");  static {    try {      file.createNewFile();    } catch (IOException e) {      System.out.println("创建文件失败.");      // e.printStackTrace();    }  }  // 其他实现代码...}

登录流程

  • 读取文件:使用BufferedReader读取文件内容。
    BufferedReader br = new BufferedReader(new FileReader(file));String line = null;while ((line = br.readLine()) != null) {  String[] datas = line.split("=");  if (datas[0].equals(username) && datas[1].equals(password)) {    flag = true;    break;  }}
  • 异常处理:使用try-catch-finally确保资源释放。
    try {  // 读取逻辑} catch (FileNotFoundException e) {  System.out.println("用户登录找不到信息所在的文件.");} catch (IOException e) {  System.out.println("用户登录失败.");} finally {  // 资源释放逻辑}

注册流程

  • 写入文件:使用BufferedWriter写入用户信息。
    BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));bw.write(user.getUsername() + "=" + user.getPassword());bw.newLine();bw.flush();
  • 异常处理:确保写入操作的可靠性。
    try {  // 写入逻辑} catch (IOException e) {  System.out.println("用户注册失败.");} finally {  // 资源释放逻辑}

数据操作流

1.2 数据输出流

  • DataOutputStream:用于写入基本数据类型。
    DataOutputStream dos = new DataOutputStream(new FileOutputStream("dos.txt"));dos.writeByte(10);dos.writeShort(100);dos.writeInt(1000);
  • 读取数据:使用DataInputStream读取相应类型的数据。
    DataInputStream dis = new DataInputStream(new FileInputStream("dos.txt"));byte b = dis.readByte();short s = dis.readShort();

1.3 内存操作流

  • ByteArrayInputStream/ByteArrayOutputStream:用于临时存储。
    ByteArrayOutputStream baos = new ByteArrayOutputStream();baos.write("hello".getBytes());byte[] bys = baos.toByteArray();ByteArrayInputStream bais = new ByteArrayInputStream(bys);
  • CharArrayReader/CharArrayWriter:用于处理字符串。
    CharArrayReader car = new CharArrayReader("abc".toCharArray());char c = car.read();

打印流

1.4 打印流特点

  • 只操作目的地:不直接处理数据源。
  • 支持自动刷新:通过BufferedWriter优化输出。
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));bw.write("hello");bw.newLine();

随机访问流

1.5 RandomAccessFile

  • 读写混合模式:支持随机读取和写入。
    RandomAccessFile raf = new RandomAccessFile("raf.txt", "rw");raf.writeInt(100);raf.writeChar('a');
  • 文件操作:支持随机访问和修改。
    raf.seek(4);String str = raf.readUTF();

合并流

1.6 SequenceInputStream

  • 多个输入流合并输出:用于文件合并。
    SequenceInputStream sis = new SequenceInputStream(new FileInputStream("a.txt"), new FileInputStream("b.txt"));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("c.txt"));byte[] buffer = new byte[1024];while ((len = sis.read(buffer)) != -1) {  bos.write(buffer, 0, len);}

序列化流

1.7 ObjectOutputStream

  • 对象序列化:将对象转换为流数据。
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("oos.txt"));oos.writeObject(new Person("Tom", 3));
  • 反序列化:还原流数据为对象。
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("oos.txt"));Person p = ois.readObject();

Properties

1.8 Properties类

  • 键值对存储:支持存储和加载配置信息。
    Properties prop = new Properties();prop.setProperty("Jack", "5");prop.setProperty("Tom", "2");
  • 文件操作:通过store和load方法进行保存和加载。
    prop.store(new FileWriter("Store.txt"), "Tips:");Properties loadedProp = new Properties();loadedProp.load(new FileReader("Store.txt"));

NIO

1.9 NIO核心类

  • Path和Files类:用于文件路径和操作。
    Path path = Paths.get("file.txt");Files.copy(path, new FileOutputStream("copy.txt"));
  • 字符集处理:支持多种字符编码。
    Files.write(Paths.get("Array.txt"), "hello world", Charset.forName("GBK"));

通过以上内容,可以清晰地了解Java IO流的操作流程和相关技术。每个部分都包含了具体的代码示例和详细解释,帮助开发者更好地理解和应用这些功能。

转载地址:http://xewiz.baihongyu.com/

你可能感兴趣的文章
MySQL 优化:Explain 执行计划详解
查看>>
Mysql 会导致锁表的语法
查看>>
mysql 使用sql文件恢复数据库
查看>>
mysql 修改默认字符集为utf8
查看>>
Mysql 共享锁
查看>>
MySQL 内核深度优化
查看>>
mysql 内连接、自然连接、外连接的区别
查看>>
mysql 写入慢优化
查看>>
mysql 分组统计SQL语句
查看>>
Mysql 分页
查看>>
Mysql 分页语句 Limit原理
查看>>
MySql 创建函数 Error Code : 1418
查看>>
MySQL 创建新用户及授予权限的完整流程
查看>>
mysql 创建表,不能包含关键字values 以及 表id自增问题
查看>>
mysql 删除日志文件详解
查看>>
mysql 判断表字段是否存在,然后修改
查看>>
MySQL 到底能不能放到 Docker 里跑?
查看>>
mysql 前缀索引 命令_11 | Mysql怎么给字符串字段加索引?
查看>>
MySQL 加锁处理分析
查看>>
mysql 协议的退出命令包及解析
查看>>