hpe实训课(IO-字节流)

字节流: InputStream 、 OutputStream

1.从硬盘上读取一个文件内容加载到程序 需要使用输入流(FileInputStream)

用法

public void testFileInputStream1() throws 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);
		}
		
		
		// 4.关闭相应的流
		fis.close();
	}


	// 使用try-catch处理异常  可以保证流的关闭一定执行
	@Test
	public void testFileInputStream2() {
		FileInputStream fis = null;
		try {
			File file = new File("Hello.txt");
			fis = new FileInputStream(file);
			int b;
			while ((b=fis.read()) != -1) {
				System.out.println((char)b);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
2.读取到的数据写入到数组 (len
public void testFileInputStream3() {
		FileInputStream fis = null;
		try {
			File file = new File("hello.txt");
			fis = new FileInputStream(file);
			// 读取到的数据写入到数组
			byte[] b = new byte[5];
			// 每次读入byte数组中的字节长度(真实长度)
			int len;
			while ((len = fis.read(b)) != -1) {
//				// 第一种方式
//				for (int i = 0; i < len; i++) {
//					System.out.print((char)b[i]);
//				}
				
				
				// 第二种方式
				// 读取一个数组,从索引0开始读,读取的长度是len
				String str = new String(b, 0, len);
				System.out.println(str);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
3.FileOutputStream(将程序(内存)中的数据持久化到磁盘)
public void testFileOutputStream() {
		FileOutputStream fos = null;
		try {
			// 1.创建一个File对象,指定写入的文件位置
			// 如果指定的文件存在则将原有的文件覆盖,如果不存在,则创建一个新的文件
			File file = new File("hello2.txt");
			if (!file.exists()) {
				file.createNewFile();
			}
			// 2.创建一个FileOutputStream对象,将file对象作为形参传递给构造器
			fos = new FileOutputStream(file);
			// 3.写入的数据
			String str = "I love you forever and yibainian";
			// 需要将字符串转换成byte类型的数组, write(byte[] b)
			fos.write(str.getBytes());
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				// 4.关闭输出流
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
4. 实现文件的复制(从硬盘读取一个文件,并写入到另外一个位置)
public void copyFile(String src,String dest) {
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			// 1.提供读入,写出的文件
			File file1 = new File(src);
			File file2 = new File(dest);
			// 2.提供对应的输入流和输出流对象
			fis = new FileInputStream(file1);
			fos = new FileOutputStream(file2);
			// 3.实现文件的复制(先读数据再写数据)
			byte[] b = new byte[24];
			int len;
			// 读取输入流中的数据
			while ((len = fis.read(b)) != -1) {
				// 写数据(从头开始写 写的长度是len)
				fos.write(b, 0, len);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				// 4.关闭相应的输入流、输出流
				// 最早打开的 最晚关闭
				if (fos != null) {
					fos.close();
				}
				if (fis != null) {
					fis.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
发布了30 篇原创文章 · 获赞 0 · 访问量 1940

猜你喜欢

转载自blog.csdn.net/qq_38499019/article/details/104645230
今日推荐