Java的I/O流——文件输入/输出流

本文详细介绍了Java中使用FileInputStream/FileOutputStream进行文件读写的步骤,包括文件对象的创建、信息的写入与读取,以及资源的关闭。同时,文章还讲解了如何使用FileReader/FileWriter处理字符流,避免汉字读取时出现乱码。

FileInputStream与FileOutputStream类

FileInputStream与FileOutputStream类都是用来操作磁盘文件。FileInputStream(文件字节输入流),FileOutputStream(文件字节输出流)。
FileInputStream与FileOutputStream类可实现文件的读取与写入功能。

首先创建文件对象

public class Study2 {

	public static void main(String[] args) {
		
		//创建文件对象
		File f = new File("word.txt");

	}
}

创建FileOutputStream对象来实现信息写入到文件中

public class Study2 {

	public static void main(String[] args) {

		// 创建文件对象
		File f = new File("word.txt");

		// 将信息写入到文件中
		FileOutputStream out = null;
		try {
			out = new FileOutputStream(f);// 创建FileOutputStream对象
			byte b[] = "Holle Word!".getBytes();// 创建byte型数组
			out.write(b);// 将数组中的信息写入到文件中
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {// 判断out是否为空值,如果不等于空值,则需要将流关闭
				try {
					out.close();// 将流关闭
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}
}

项目目录下会出现word.txt文件
在这里插入图片描述
打卡文件,会输出byte数组中的那一句话
在这里插入图片描述
创建FileInputStream对象来实现将文件中的信息读取出来

// 将文件信息读取输出
		FileInputStream in = null;
		try {
			in = new FileInputStream(f);// 创建FileInputStream对象
			byte b1[] = new byte[1024];//创建byte数组
			int len = in.read(b1);//从文件中读取信息
			System.out.println("文件中的信息是:" + new String(b1,0,len));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (in != null) {// 判断in是否为空值,如果不等于空值,则需要将流关闭
				try {
					in.close();// 将流关闭
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

在这里插入图片描述
完整代码:

public class Study2 {

	public static void main(String[] args) {

		// 创建文件对象
		File f = new File("word.txt");

		// 将信息写入到文件中
		FileOutputStream out = null;
		try {
			out = new FileOutputStream(f);// 创建FileOutputStream对象
			byte b[] = "Holle Word!".getBytes();// 创建byte型数组
			out.write(b);// 将数组中的信息写入到文件中
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {// 判断out是否为空值,如果不等于空值,则需要将流关闭
				try {
					out.close();// 将流关闭
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		// 将文件信息读取输出
		FileInputStream in = null;
		try {
			in = new FileInputStream(f);// 创建FileInputStream对象
			byte b1[] = new byte[1024];// 创建byte数组
			int len = in.read(b1);// 从文件中读取信息
			System.out.println("文件中的信息是:" + new String(b1, 0, len));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (in != null) {// 判断in是否为空值,如果不等于空值,则需要将流关闭
				try {
					in.close();// 将流关闭
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}
}

FileReader与FileWriter类

因为FileInputStream与FileOutputStream这两个类只提供了对字节或字节数组的读取方法,汉字在字节中占两个字节,如果读取不好,可能出现乱码的现象,因此就有了FileReader(文件字符输入流)与FileWriter(文件字符输出流)字符流。
两者之间大同小异,方法相同。
代码如下:

public class Study2 {

	public static void main(String[] args) {

		// 创建文件对象
		File f = new File("word.txt");

		// 将信息写入到文件中
		FileWriter out = null;
		try {
			out = new FileWriter(f);// 创建FileOutputStream对象
			String str = "Holle Word!";// 创建byte型数组
			out.write(str);// 将数组中的信息写入到文件中
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {// 判断out是否为空值,如果不等于空值,则需要将流关闭
				try {
					out.close();// 将流关闭
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		// 将文件信息读取输出
		FileReader in = null;
		try {
			in = new FileReader(f);// 创建FileInputStream对象
			char c[] = new char[1024];// 创建byte数组
			int len = in.read(c);// 从文件中读取信息
			System.out.println("文件中的信息是:" + new String(c, 0, len));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (in != null) {// 判断in是否为空值,如果不等于空值,则需要将流关闭
				try {
					in.close();// 将流关闭
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值