黑马程序员——FileINPutStream,FileOutPutStream介绍

本文介绍如何使用Java字节流进行数据的读写操作,包括单个字节读取、字节数组读取及利用FileInputStream类中的available()方法获取字节数的具体数量并直接建立相应字节数组的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                                          ----------------------     android培训      java培训   期待与您交流!    ----------------------   
                   

package com.io.bytes;

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

public class FileInputStream_FileOutPutStream {
	public static void main(String[] args)throws IOException  {
		//fileOutputStream();//字节写入数据
		//read_method1();//单个字节读取数据
		//read_method2();//字节数组读取数据
		read_method3();//利用FileInPutStream类中的available()方法获取字节数,直接建立相应的字节数组
	}
	/*
	 * 建立缓冲区,即建立字节数组,读取数据,循环读取数据
	 * 利用FileInPutStream类中的available()方法,该方法返回数据具体多少个字节
	 */
	public static void read_method3() throws IOException{
		FileInputStream fis=new FileInputStream(new File("g:\\java\\yyy.txt"));
		byte[] s=new byte[fis.available()];//利用返回的具体字节数直接建立好长度和数据相等的字节数组,这样就不用循环了
		fis.read(s);//这样写简单。但当数据量大过内存时但是这样容易使内存溢出,所以不建议这样,最好用字节数组定义1024的整数倍
		System.out.println(new String(s));
		fis.close();
	}
	/*
	 * 建立缓冲区,即建立字节数组,读取数据,循环读取数据
	 */
	public static void read_method2() throws IOException{
		FileInputStream fis=new FileInputStream(new File("g:\\java\\yyy.txt"));
		int num=0;
		byte[] s=new byte[1024];//注意这里最好是1024的整数倍,有利于读取
		while(-1!=(num=fis.read(s))){
			System.out.println(new String(s,0,num));//注意这里要取的值是到num的值
		}
		fis.close();
	}
	/*
	 * 读取数据,循环读取数据
	 */
	public static void read_method1() throws IOException{
		FileInputStream fis=new FileInputStream(new File("g:\\java\\yyy.txt"));
		int num=0;
		while(-1!=(num=fis.read())){
			System.out.println((char)num);
		}
		fis.close();
	}
	
	/*
	 * 字节流写入数据,
	 * 注意,字节流写入数据不用刷新,会直接写入,但要关闭流
	 */
	public static void fileOutputStream()throws IOException {
		FileOutputStream fos=new FileOutputStream(new File("g:\\java\\yyy.txt"));
		fos.write("abcde".getBytes());
		fos.close();
	}
}
                                          ----------------------     android培训      java培训   期待与您交流!    ----------------------   
                         详细请查看       http://edu.youkuaiyun.com/heima



### Java `FileInputStream` 和 `FileOutputStream` 的作用及用法 #### 1. 基本概念 `FileInputStream` 是一种用于从文件中读取字节数据的输入流,而 `FileOutputStream` 则是一种向文件写入字节数据的输出流。两者都属于字节流类别,适用于处理二进制数据或任何需要逐字节操作的情况[^1]。 #### 2. 使用场景 这两种流非常适合于复制文件的操作,因为它们支持按字节数组的形式批量读写数据,从而提高效率。无论是文本文件还是图片、视频等其他类型的文件都可以通过这种方式进行传输和保存[^2]。 #### 3. 示例代码展示 ##### (1)简单读取文件内容到控制台 下面是一个利用 `FileInputStream` 将本地文件的内容打印至终端的例子: ```java import java.io.FileInputStream; import java.io.IOException; public class FileInputStreamExample { public static void main(String[] args) { try { // 创建文件输入流 FileInputStream in = new FileInputStream("input.txt"); int c; while ((c = in.read()) != -1) { System.out.print((char)c); } // 关闭资源释放内存空间 in.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` ##### (2)实现文件之间的拷贝功能 这里给出一个完整的例子说明如何使用这两个类完成两个不同位置上的同名文件之间内容的一致性同步工作流程: ```java import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class FileCopyExample { public static void copyFileUsingStream(File source, File dest) throws IOException { // 检查源文件是否存在并确认其为实际存在的普通文件而非目录之类的特殊项目类型 if (!source.exists()){ throw new IllegalArgumentException("Source file does not exist!"); } if (!source.isFile()){ throw new IllegalArgumentException("The provided path is not pointing towards an ordinary file."); } // 初始化必要的变量准备开始正式的数据迁移过程 FileInputStream inputStream = null; FileOutputStream outputStream = null; final int BUFFER_SIZE = 1024 * 8; try { inputStream = new FileInputStream(source); outputStream = new FileOutputStream(dest); byte[] buffer = new byte[BUFFER_SIZE]; int length; while ((length = inputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, length); } } finally { // 确保无论发生什么都能够妥善关闭所有打开过的IO通道以防泄露系统资源造成浪费现象的发生 if (outputStream != null){ try{ outputStream.close(); }catch(IOException ex){} } if(inputStream != null){ try{ inputStream.close(); }catch(IOException ex){} } } } public static void main(String[] args)throws Exception{ File src=new File("/path/to/source/file"); File dst=new File("/another/path/destination/file"); long startTime=System.currentTimeMillis(); copyFileUsingStream(src,dst); long endTime=System.currentTimeMillis(); System.out.println("Time taken:"+ (endTime-startTime)+" ms"); } } ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

King·Forward

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值