博客
关于我
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/

你可能感兴趣的文章
MAC M1大数据0-1成神篇-25 hadoop高可用搭建
查看>>
mac mysql 进程_Mac平台下启动MySQL到完全终止MySQL----终端八步走
查看>>
Mac OS 12.0.1 如何安装柯美287打印机驱动,刷卡打印
查看>>
MangoDB4.0版本的安装与配置
查看>>
Manjaro 24.1 “Xahea” 发布!具有 KDE Plasma 6.1.5、GNOME 46 和最新的内核增强功能
查看>>
mapping文件目录生成修改
查看>>
MapReduce程序依赖的jar包
查看>>
mariadb multi-source replication(mariadb多主复制)
查看>>
MariaDB的简单使用
查看>>
MaterialForm对tab页进行隐藏
查看>>
Member var and Static var.
查看>>
memcached高速缓存学习笔记001---memcached介绍和安装以及基本使用
查看>>
memcached高速缓存学习笔记003---利用JAVA程序操作memcached crud操作
查看>>
Memcached:Node.js 高性能缓存解决方案
查看>>
memcache、redis原理对比
查看>>
memset初始化高维数组为-1/0
查看>>
Metasploit CGI网关接口渗透测试实战
查看>>
Metasploit Web服务器渗透测试实战
查看>>
MFC模态对话框和非模态对话框
查看>>
Moment.js常见用法总结
查看>>