首页 > 其他分享 >七IO流

七IO流

时间:2024-10-18 20:49:10浏览次数:1  
标签:IO try File catch new 异常

Java IO流学习笔记

异常处理

异常概述

异常是Java程序在运行过程中出现的错误。异常可以被看作是问题的具体事务,通过Java类的形式进行描述,并封装成对象。

try {
    // 可能产生异常的代码
    int divisionResult = 10 / 0;
} catch (Exception e) {
      e.printStackTrace();
}

异常分类

Java中的异常分为两大类:编译时异常和运行时异常。

异常处理方案

Java提供了两种异常处理方案:try...catch...finallythrows

try {
    // 尝试执行的代码
} catch (ExceptionType name) {
    // 异常发生时的处理代码
} finally {
    // 一定会执行的代码
}

throwsthrow

  • throws用于方法声明,表示该方法可能会抛出的异常类型。
  • throw用于方法体内,用于抛出一个具体的异常实例。
public void myMethod() throws IOException {
    // 方法可能会抛出IOException
}

public void anotherMethod() {
    throw new RuntimeException("发生了运行时异常");
}

File类

File类概述

File类是文件和目录路径名的抽象表示形式。

File file = new File("path/to/your/file.txt");
if (file.exists()) {
    System.out.println("文件存在");
}

成员方法

File类提供了创建、删除、重命名、判断和获取文件属性的方法。

File dir = new File("path/to/your/directory");
if (dir.mkdir()) {
    System.out.println("目录创建成功");
}

递归

递归思想概述

递归是方法定义中调用方法本身的现象,适用于能分解为相似子问题的问题。

public static int factorial(int n) {
    if (n == 0) {
        return 1;
    } else {
        return n * factorial(n - 1);
    }
}

IO流概述

IO流定义

IO流用于处理设备之间的数据传输,如文件的读写。

IO流分类

  • 数据流向:输入流(读入数据)和输出流(写出数据)。
  • 数据类型:字节流和字符流。

字节流

字节流写数据

FileOutputStream用于写入字节数据。

try (FileOutputStream fos = new FileOutputStream("path/to/your/file.txt")) {
    fos.write("Hello, World!".getBytes());
} catch (IOException e) {
    e.printStackTrace();
}

字节流读取数据

FileInputStream用于读取字节数据。

try (FileInputStream fis = new FileInputStream("path/to/your/file.txt")) {
    int content;
    while ((content = fis.read()) != -1) {
        System.out.print((char) content);
    }
} catch (IOException e) {
    e.printStackTrace();
}

转换流

转换流概述

转换流用于在字节流和字符流之间转换。

try (OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("path/to/your/file.txt"))) {
    osw.write("Hello, World!");
} catch (IOException e) {
    e.printStackTrace();
}

字符缓冲流

字符缓冲流概述

字符缓冲流提高了字符数据的读写效率。

try (BufferedWriter bw = new BufferedWriter(new FileWriter("path/to/your/file.txt"))) {
    bw.write("Hello, World!");
} catch (IOException e) {
    e.printStackTrace();
}

字符缓冲流特殊功能

BufferedWriternewLine()BufferedReaderreadLine()提供了特殊功能。

try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("path/to/your/file.txt")))) {
    String line;
    while ((line = br.readLine()) != null) {
        System.out.println(line);
    }
} catch (IOException e) {
    e.printStackTrace();
}

Properties集合

Properties概述

Properties集合用于处理配置文件。

Properties prop = new Properties();
try (InputStream input = new FileInputStream("config.properties")) {
    prop.load(input);
} catch (IOException ex) {
    ex.printStackTrace();
}
String username = prop.getProperty("username");

这份详细的笔记包括了异常处理、File类、递归、IO流的基本概念和操作、字符缓冲流以及Properties集合的使用,并通过简单的代码示例帮助理解。希望这份笔记能帮助你更好地掌握Java IO流。

标签:IO,try,File,catch,new,异常
From: https://www.cnblogs.com/bjynjj/p/18475027

相关文章

  • # Tomcat NIO 配置实操指南
    TomcatNIO配置实操指南ApacheTomcat是一个广泛使用的开源JavaServlet容器,支持多种I/O模型来处理HTTP请求。NIO(Non-blockingI/O)是Tomcat提供的三种主要I/O之一(另外两个是Blocking和APR/native)。本文将详细介绍Tomcat中NIO的概念、优势以及如何进行配......
  • IO流 - File()文件的相关知识
    File文件File【文件】:是计算机中所有文件的抽象表示,将来File对象目标可能存在,也可能不存在。构造方法:publicFile(Stringpathname)publicFile(Stringparent,Stringchild)publicFile(Fileparent,Stringchild)路径:......
  • FlashAttention逐代解析与公式推导
    StandardAttention标准Attention计算可以简化为:\[O=softmax(QK^T)V\tag{1}\]此处忽略了AttentionMask和维度归一化因子\(1/\sqrt{d}\)。公式(1)的标准计算方式是分解成三步:\[S=QK^T\tag{2}\]\[P=softmax(S)\tag{3}\]\[O=PV\tag{4}\]但这样做的问题在于,假设\(......
  • Educational Codeforces Round 166 (Rated for Div. 2) - VP记录
    比赛链接A.VerifyPassword挨个判断即可,秒了。#include<cstdio>#include<cstring>usingnamespacestd;constintN=25;intT,n;charstr[N];boolis_digit(charch){returnch>='0'&&ch<='9';}boolis_lowercase(charch){re......
  • java_day15_Collections、递归、Exception、File
    一、CollectionsCollections:是java针对集合操作专门提供的一个工具类静态方法:publicstatic<T>voidsort(List<T>list)publicstatic<T>intbinarySearch(List<?>list,Tkey)publicstatic<T>Tmax(Collection<?>coll)public......
  • MinIO
    1.概述一个开源的用于存储文件的分布式文件存储系统2.官网http://docs.minio.org.cn/docs/3.相关概念bucket–类比于文件系统的目录Object–类比文件系统的文件Keys–类比文件名4.搭建dockerrun-p9000:9000--nameminio-d--restart=always-e"MINIO_A......
  • 【AI绘画】Stable Diffusion实战ControlNET插件(让小姐姐摆出你要的pose!)
    大家好我是安琪!SD插件ControlNET的诞生,无法自定义姿势成为过去,自定义姿势;根据线稿、骨骼、其他图片生成全新的图,AI绘图自主可控;包括边缘检测,深度信息估算;姿态,手势检测;分割等等场景:个人pose图,模特换装;装修出图;设计草图快速复原;颜色快速更换等等此扩展用于AUTOMATIC1111的......
  • RHEL 环境下 Subversion 服务器部署与配置
    RHEL环境下Subversion服务器部署与配置1.更新系统首先确保系统软件包是最新的:sudoyumupdate-y2.安装Apache和Subversion2.1安装ApacheHTTP服务器和Subversion使用以下命令安装Apache、Subversion及其与Apache集成的模块:sudoyuminstall-yhttpdsubversion......
  • 【题解】[Codechef] Beautiful Permutation
    传送门以此纪念我场切的dp。这种计数的类型一看就很dp的样子。考场上一开始设的dp状态是\(dp_{i,j,k_1,k_2,0/1}\)表示将前\(i\)个数分为\(j\)段,放了\(k_1\)个偶数,\(k_2\)个奇数,当前段为偶数段或奇数段的方案数。考虑如何转移,记\(cnt_0\)表示序列中可填入的偶数......
  • 【shiro】11.shiro过滤器鉴权setFilterChainDefinitionMap
    之前学习shiro的时候,设置了登录页面和主页面(需要登录才能范围的页面。)1//配置系统公共资源2Map<String,String>map=newHashMap<>();3//authc请求这个资源需要认证和授权4map.put("/index","authc");5//默认认证界面路径6shiroFilterFactoryBean.setLoginUrl(l......