【高级编程】Java IO流(上)字节流 InputStream OutputStream
文章目录
- 文件操作
- 流
- 输入流InputStream
- 输出流OutputStream
文件操作
文件是指相关记录或放在一起的数据的集合。是一种用于存储数据的基本单位,它可以包含各种类型的信息,例如文本、图像、音频或视频。文件在计算机中通常存储在磁盘或其他存储介质上,并且每个文件都有一个唯一的文件名和扩展名(如 .txt
、.jpg
、.mp4
),用于标识文件的类型和用途。
文件的主要特征
文件名和扩展名、文件路径、文件大小、文件属性、文件系统。
在 Java 中,java.io.File
类是进行文件和目录操作的主要类。它提供了一些方法来创建、删除、检查文件和目录的属性以及操作文件内容。
方法名称 | 说明 |
---|---|
boolean exists( ) | 判断文件或目录是否存在 |
boolean isFile( ) | 判断是否是文件 |
boolean isDirectory( ) | 判断是否是目录 |
String getPath( ) | 返回此对象表示的文件的相对路径名 |
String getAbsolutePath( ) | 返回此对象表示的文件的绝对路径名 |
String getName( ) | 返回此对象表示的文件或目录的名称 |
boolean delete( ) | 删除此对象指定的文件或目录 |
boolean createNewFile( ) | 创建名称的空文件,不创建文件夹 |
long length( ) | 返回文件的长度,单位为字节, 如果文件不存在,则返回 0L |
String path = "D:/4072/1/l.txt";
File file = new File(path);// 判断文件或目录是否存在
System.out.println("判断文件或目录是否存在: " + file.exists());
// 判断是否是文件
System.out.println("判断是否是文件: " + file.isFile());
// 判断是否是文件夹
System.out.println("判断是否是文件夹: " + file.isDirectory());
// 获取相对路径
System.out.println("相对路径: " + file.getPath());
// 获取绝对路径
System.out.println("绝对路径: " + file.getAbsolutePath());
// 获取文件或目录名称
System.out.println("获得文件或目录名称: " + file.getName());
// 获取文件字节数
System.out.println("获得文件字节数: " + file.length());
// 删除文件或目录
System.out.println("删除文件或目录: " + file.delete());
在指定文件夹下创建 txt 文件
public static void main(String[] args) {// 定义路径和文件名String path = "D:/test/1/";String fileName = "1.txt";// 创建 File 对象表示文件夹File dir = new File(path);// 如果路径不存在或者不是一个目录,则 mkdirs() 尝试创建多级目录if (!dir.exists() || !dir.isDirectory()) {if (dir.mkdirs()) { System.out.println("进来创建文件夹");} else {System.out.println("无法创建文件夹");return; // 如果无法创建文件夹,退出程序}}// 创建 File 对象表示文件File file = new File(path, fileName);try {// 如果文件不存在,则创建新文件if (!file.exists() || !file.isFile()) {if (file.createNewFile()) {System.out.println("进来创建文件....");} else {System.out.println("文件创建失败");}} else {System.out.println("文件已存在");}} catch (IOException e) {e.printStackTrace();}
}
流
流是一组有序的数据序列,以先进先出方式发送信息的通道
Java流的分类
输入输出流是相对于计算机内存来说的
字节流是8位通用字节流,字符流是16位 Unicode 字符流
输入流InputStream
InputStream
类常用方法
int read( )int read(byte[] b)int read(byte[] b,int off,int len)void close( ) // 关闭流 释放资源int available() // 可以从输入流中读取的字节数目
子类 FileInputStream
常用的构造方法
FileInputStream(File file)FileInputStream(String name)
使用 FileInputStream
读文本文件
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;public class FileReadExample {public static void main(String[] args) {InputStream is = null;try {// 打开文件输入流is = new FileInputStream("D:\\4072\\1\\1.txt");// 获取文件的字节数byte[] b = new byte[is.available()];// 读取内容到字节数组is.read(b);// 将字节数组转换为字符串(使用 GBK 编码)String str = new String(b, "GBK");// 打印字符串System.out.println(str);} catch (IOException e) {// 捕获并打印异常堆栈e.printStackTrace();} finally {// 关闭输入流并释放资源try {if (is != null) is.close();} catch (IOException e) {e.printStackTrace();
}}}}
输出流OutputStream
OutputStream
类常用方法
void write(int c)void write(byte[] buf)void write(byte[] b,int off,int len)void close() // 释放资源void flush() // 强制把缓冲区的数据写到输出流中
子类 FileOutputStream
常用的构造方法
FileOutputStream(File file)FileOutputStream(String name)FileOutputStream(String name,boolean append)
前两种构造方法在向文件写数据时将覆盖文件中原有的内容,append
参数默认为 false
创建 FileOutputStream 实例时,如果相应的文件并不存在,则会自动创建一个空的文件
使用 FileOutputStream
写文本文件
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.IOException;public class FileWriteExample {public static void main(String[] args) {OutputStream os = null;try {// 打开文件输出流,并以追加模式写入os = new FileOutputStream("p:\\4072\\1\\1.txt", true);String msg = "张三";// 将字符串转换为字节数组并调用 write 方法进行写入os.write(msg.getBytes());System.out.println("写入成功......");} catch (IOException e) {// 捕获并打印异常堆栈e.printStackTrace();} finally {// 关闭输出流并释放资源try {if (os != null) os.close();} catch (IOException e) {e.printStackTrace();
}}}}
复制文件内容代码示例:
public static void main(String[] args) {InputStream is = null; // 读OutputStream os = null; // 写try {// 打开文件输入流和输出流is = new FileInputStream("D:\\4072\\1\\1.txt");os = new FileOutputStream("D:\\4072\\1\\2.txt");int b;// 逐字节读取文件内容并写入到目标文件while ((b = is.read()) != -1) {os.write(b);}System.out.println("复制成功!!!!");} catch (IOException e) {// 捕获并打印异常堆栈e.printStackTrace();} finally {// 关闭输入输出流并释放资源try {if (is != null) is.close();if (os != null) os.close();} catch (IOException e) {throw new RuntimeException(e);
}}}