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

你可能感兴趣的文章
mt_rand
查看>>
mysql -存储过程
查看>>
mysql /*! 50100 ... */ 条件编译
查看>>
mudbox卸载/完美解决安装失败/如何彻底卸载清除干净mudbox各种残留注册表和文件的方法...
查看>>
mysql 1264_关于mysql 出现 1264 Out of range value for column 错误的解决办法
查看>>
mysql 1593_Linux高可用(HA)之MySQL主从复制中出现1593错误码的低级错误
查看>>
mysql 5.6 修改端口_mysql5.6.24怎么修改端口号
查看>>
MySQL 8.0 恢复孤立文件每表ibd文件
查看>>
MySQL 8.0开始Group by不再排序
查看>>
mysql ansi nulls_SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON 什么意思
查看>>
multi swiper bug solution
查看>>
MySQL Binlog 日志监听与 Spring 集成实战
查看>>
MySQL binlog三种模式
查看>>
multi-angle cosine and sines
查看>>
Mysql Can't connect to MySQL server
查看>>
mysql case when 乱码_Mysql CASE WHEN 用法
查看>>
Multicast1
查看>>
mysql client library_MySQL数据库之zabbix3.x安装出现“configure: error: Not found mysqlclient library”的解决办法...
查看>>
MySQL Cluster 7.0.36 发布
查看>>
Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
查看>>