博客
关于我
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 CPU使用率过高的一次处理经历
查看>>
Multisim中555定时器使用技巧
查看>>
MySQL CRUD 数据表基础操作实战
查看>>
multisim变压器反馈式_穿过隔离栅供电:认识隔离式直流/ 直流偏置电源
查看>>
mysql csv import meets charset
查看>>
multivariate_normal TypeError: ufunc ‘add‘ output (typecode ‘O‘) could not be coerced to provided……
查看>>
MySQL DBA 数据库优化策略
查看>>
multi_index_container
查看>>
MySQL DBA 进阶知识详解
查看>>
Mura CMS processAsyncObject SQL注入漏洞复现(CVE-2024-32640)
查看>>
Mysql DBA 高级运维学习之路-DQL语句之select知识讲解
查看>>
mysql deadlock found when trying to get lock暴力解决
查看>>
MuseTalk如何生成高质量视频(使用技巧)
查看>>
mutiplemap 总结
查看>>
MySQL DELETE 表别名问题
查看>>
MySQL Error Handling in Stored Procedures---转载
查看>>
MVC 区域功能
查看>>
MySQL FEDERATED 提示
查看>>
mysql generic安装_MySQL 5.6 Generic Binary安装与配置_MySQL
查看>>
Mysql group by
查看>>