JavaIO流(上传文件必备)

1. IO流分类

在这里插入图片描述
字节流:(8bit)二进制文件
字符流:(字符)文本文件

2.FileInputStream 应用实例

使用 FileInputStream 读取 hello.txt 文件,并将文件内容显示到控制台

public class FileInputStream_ {
    
	public static void main(String[] args) {
    }
	/** 读取文件
	 * 单个字节的读取,效率比较低 
	 *  -> 使用 read(byte[] b) 
	 */ 
	  @Test 
	  public void readFile01() {
    
	  	String filePath = "e:\\hello.txt";
	  	int readData = 0; FileInputStream fileInputStream = null; 
	  	try {
    
	  		// 创建 FileInputStream 对象,用于读取文件 
	  		// fileInputStream = new FileInputStream(filePath); 
	  		// 从该输入流读取一个字节的数据。 如果没有输入可用,此方法将阻止。 
	  		// 如果返回-1 , 表示读取完毕 
	  		while ((readData = fileInputStream.read()) != -1) {
    
	  				System.out.print((char)readData);//转成 char 显示 			  	               
	  		} 
	  	} catch (IOException e) {
    
	  		e.printStackTrace();
	  	} finally {
    
	  		//关闭文件流,释放资源. 
			try {
    
				fileInputStream.close(); 
			} catch (IOException e) {
    
				e.printStackTrace(); 
			} 
		} 
	  }
	/**
	* 使用 read(byte[] b) 读取文件,提高效率 
	*/ 
	@Test 
	public void readFile02() {
    
		String filePath = "e:\\hello.txt"; 
		//字节数组 
		byte[] buf = new byte[8]; //一次读取 8 个字节. 
		int readLen = 0; FileInputStream fileInputStream = null; 
		try {
    
			// 创建 FileInputStream 对象,用于读取 文件 
			// fileInputStream = new FileInputStream(filePath); 
			//从该输入流读取最多 b.length 字节的数据到字节数组。 此方法将阻塞,直到某些输入可用。 		
			//如果返回-1 , 表示读取完毕 
			//如果读取正常, 返回实际读取的字节数 
			while ((readLen = fileInputStream.read(buf)) != -1) {
   
 				System.out.print(new String(buf, 0, readLen));//显示 
 			} 
 		} catch (IOException e) {
    
 			e.printStackTrace(); 
 		} finally {
    
 			//关闭文件流,释放资源. 
 			try {
    
 				fileInputStream.close(); 
 			} catch (IOException e) {
    
 				e.printStackTrace(); 
 			} 
 	 	} 
  	} 
}

3. FileOutputStream 应用实例

要求: 请使用 FileOutputStream 在 a.txt 文件,中写入 “hello,world”, 如果文件不存在,会创建 文件(注意:前提是目录已经存在.)

public class FileOutputStream01 {
    
	public static void main(String[] args) {
    }
	/**
	* 演示使用 FileOutputStream 将数据写到文件中, 
	* 如果该文件不存在,则创建该文件 
	*/
	@Test
	public void writeFile() {
    
		//创建 FileOutputStream 对象 
		String filePath = "e:\\a.txt"; 
		FileOutputStream fileOutputStream = null; 
		try {
    
			//得到 FileOutputStream 对象 
			//说明 
			//1. new FileOutputStream(filePath) 创建方式,当写入内容是,会覆盖原来的内容 
			//2. new FileOutputStream(filePath, true) 创建方式,当写入内容是,是追加到文件后面 
			fileOutputStream = new FileOutputStream(filePath, true); 
			//写入一个字节 
			//fileOutputStream.write('H');// 
			//写入字符串 
			String str = "hsp,world!"; 
			//str.getBytes() 可以把 字符串-> 字节数组
			//fileOutputStream.write(str.getBytes());  									   																			
			/*
			write(byte[] b, int off, int len) 将 len 字节从位于偏移量 off 的指定字节数组写入此文件输出流 
			*/ 
			fileOutputStream.write(str.getBytes(), 0, 3); 
		} catch (IOException e) {
    
			e.printStackTrace(); 
		} finally {
    
			try {
    
 				fileOutputStream.close(); 
 			} catch (IOException e) {
    
 				e.printStackTrace(); 
 			} 
 		} 
 	}
}

4. FileIutputStream和FileIuOputStream文件拷贝

要求: 编程完成图片/音乐 的拷贝

public class FileCopy {
    
	public static void main(String[] args) {
    
		//完成 文件拷贝,将 e:\\Koala.jpg 拷贝 c:\\ 
		//思路分析
		//1. 创建文件的输入流 , 将文件读入到程序 
		//2. 创建文件的输出流, 将读取到的文件数据,写入到指定的文件. 
		String srcFilePath = "e:\\Koala.jpg"; 
		String destFilePath = "e:\\Koala3.jpg"; 
		FileInputStream fileInputStream = null; 
		FileOutputStream fileOutputStream = null; 
		try {
    
			fileInputStream = new FileInputStream(srcFilePath); 
			fileOutputStream = new FileOutputStream(destFilePath
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值