Java IO流->节点流->字节流:FileInputStream与FileOutputStream

时间:2022-03-01 21:00:35

示例代码:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.junit.Test;

/**
 * 1.流的分类:
 * 按照数据流向的不同:输入流	输出流
 * 按照处理数据的单位不同:字节流 (不能处理中文,一个中文由两个字节组成)	字符流(处理文本文件)
 * 按照角色的不同:节点流(直接作用于文件)	处理流
 * 2.IO体系:
 * 抽象基类			节点流
 * InputStream		FileInputStream
 * OutputStream		FileOutputStream
 * Reader			FileReader
 * Writer			FileWriter
 *
 */
public class TestFileIOStream {
	/**
	 * FileInputStream类
	 * 说明:1.test1()和test2()是一个字节一个字节的读取文件内容,test3()是一次性多个字节读入
	 * 		2.test2()比test1()更加合理
	 */
	//从硬盘存在的一个文件中,读取其内容到程序中。使用FileInputStream
	//读取的文件一定要存在,否则抛FileNotFoundException
	@Test
	public void test1() throws FileNotFoundException , IOException {//直接抛异常的方式较不合理
		//1.创建一个File类的对象
		File file = new File("hello.txt");
		//2.创建一个FileInputStream类的对象
		FileInputStream fis = new FileInputStream(file);
		//3.调用FileInputStream的方法,实现file文件的读取
		/*
		 * read():读取文件的下一个字节,当执行到文件结尾时,返回-1
		 */
		//方法一:
		/*int b = fis.read();
		while(b != -1) {
			System.out.print((char)b);
			b = fis.read();
		}*/
		//方法二:
		int b;
		while((b = fis.read()) != -1) {
			System.out.print((char) b);
		}
		/*
		 * hello.txt文本内容:hello everybody.中国
		 * 输出结果:hello everybody.???ú
		 * 分析:字节流不能处理中文,一个中文由两个字节组成,字节流将中文拆成两半
		 */
		//4.关闭相应的流
		if(fis != null) {
			fis.close();
		}
	}
	
	@Test
	public void test2() {//使用try-catch方式处理如下异常更合理,不管有没有出异常都要关流,所以用finally最合适
		//2.创建一个FileInputStream类的对象
		FileInputStream fis = null;
		try {//选中要try-catch的内容,然后:右击鼠标->Surround With->Try/Catch Block
			//1.创建一个File类的对象
			File file = new File("hello.txt");
			fis = new FileInputStream(file);
			//3.调用FileInputStream的方法,实现file文件的读取
			/*
			 * read():读取文件的下一个字节,当执行到文件结尾时,返回-1
			 */
			//方法二:
			int b;
			while((b = fis.read()) != -1) {
				System.out.print((char) b);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			//4.关闭相应的流
			if(fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	@Test
	public void test3() {
		FileInputStream fis = null;
		try {
			File file = new File("hello.txt");
			fis = new FileInputStream(file);
			byte[] b = new byte[5];//读取到的数据要写入的数组
			int len;//每次读入到byte[]中字节的个数
			while((len = fis.read(b)) != -1) {
				//方法一:
				/*for (int i = 0; i < len; i++) {
					System.out.print((char) b[i]);
				}*/
				//方法二:
				String str = new String(b, 0, len);//如果中文没有刚好被拆开就可以显示,如果被拆了就会乱码
				System.out.println("$:"+str);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	/**
	 * FileOutputStream类
	 */
	@Test
	public void test4() {
		//1.创建File对象
		//输出的物理文件可以不存在,当执行过程中,若不存在,会自动创建,若存在,会覆盖。
		File file = new File("test4_hello.txt");
		//2.创建FileOutputStream对象
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(file);
			//3.写入操作
			fos.write(new String("I love you forever.").getBytes());//String类型->byte[]类型
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			//4.关闭输出流
			if(fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	/**
	 * 从硬盘读取一个文件,并写入另一位置。(相当于文件的复制)
	 */
	@Test
	public void test5() {
		//1.提供读出、写入的文件
		File file1 = new File("hello.txt");//可以是其他格式的(.jpg)等等
		File file2 = new File("world.txt");
		//2.提供相应的流
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream(file1);
			fos = new FileOutputStream(file2);
			//3.实现文件的复制
			byte[] b = new byte[20];
			int len;
			while((len = fis.read(b)) != -1) {
				//fos.write(b);//错误的写法,相当于错误写法:fos.write(b,0,b.length)
				fos.write(b, 0, len);
			}
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	@Test
	public void test6() {
		String src = "hello.txt";
		String dest = "new2.txt";
		copyFile(src, dest);
	}
	
	public static void copyFile(String src , String dest) {
		//1.提供读出、写入的文件
		File file1 = new File(src);//可以是其他格式的(.jpg)等等
		File file2 = new File(dest);
		//2.提供相应的流
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream(file1);
			fos = new FileOutputStream(file2);
			//3.实现文件的复制
			byte[] b = new byte[20];//当遇到大文件时,应该适当提高byte[]的长度
			int len;
			while((len = fis.read(b)) != -1) {
				//fos.write(b);//错误的写法,相当于错误写法:fos.write(b,0,b.length)
				fos.write(b, 0, len);
			}
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}