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

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();
				}
			}
		}

	}
}

猜你喜欢

转载自blog.csdn.net/javanofa/article/details/104443069