博客
关于我
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 [Err] 1436 - Thread stack overrun: 129464 bytes used of a 286720 byte stack, and 160000 bytes
查看>>
MySQL _ MySQL常用操作
查看>>
MySQL – 导出数据成csv
查看>>
MySQL —— 在CentOS9下安装MySQL
查看>>
MySQL —— 视图
查看>>
mysql 不区分大小写
查看>>
mysql 两列互转
查看>>
MySQL 中开启二进制日志(Binlog)
查看>>
MySQL 中文问题
查看>>
MySQL 中日志的面试题总结
查看>>
mysql 中的all,5分钟了解MySQL5.7中union all用法的黑科技
查看>>
MySQL 中的外键检查设置:SET FOREIGN_KEY_CHECKS = 1
查看>>
Mysql 中的日期时间字符串查询
查看>>
mysql 中索引的问题
查看>>
MySQL 中锁的面试题总结
查看>>
MySQL 中随机抽样:order by rand limit 的替代方案
查看>>
MySQL 为什么需要两阶段提交?
查看>>
mysql 为某个字段的值加前缀、去掉前缀
查看>>
mysql 主从
查看>>
mysql 主从 lock_mysql 主从同步权限mysql 行锁的实现
查看>>