Java I/O之文件流

文件流的工作步骤:

    ①  生成文件流对象(对文件读操作时应该为FileInputStream类,而文件写应该为FileOutputStream类);

    ②  调用FileInputStream或FileOutputStream类中的功能函数如read()、write(int b)等)读写文件内容;

    ③  关闭文件(close())。 

1.2 读取标准文件流

package org.io;

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

/**
 * 文件流
 * @author 我的账号
 * 
 */

public class FileStream {
	public static void main(String[] args) {
		String pathname = "";
		String desName = "";
		File soucfile = new File(pathname);
		File destFile = new File(desName);
		FileInputStream fis = null;
		FileOutputStream fos = null;
		
		int fileRead = 0;
		
		try {
			fis = new FileInputStream(soucfile);
			byte[] buff = new byte[fis.available()];	//通过avalible获取流的最大字节数
			fos = new FileOutputStream(destFile);
			if((fileRead = fis.read(buff)) == -1){
				fos.write(buff);
			}
			
		} catch (FileNotFoundException e) {

			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}finally{
			try {
				fos.close();
				fis.close();
			} catch (IOException e) {
				
				e.printStackTrace();
			}
			
		}
	}
}
1.2 读写任意大文件应用

因为byte数组最大存储值不超过64M,所以当一个文件大于60M 的时候,需要分开几个流操作

package org.io;

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

/**
 * 
 * @author 我的账号
 * 
 */
public class FileStream2 {
	//把特定位置的流内容读入数组,已经读入byte[]数组的内容,会在流文件中删除。
	public static void main(String[] args) {
		//创建两个文件
		File inFile = new File("");
		File outFile = new File("");
		
		//最大的流为60M,当文件的容量大于60M的时候便分开流
		final int MAX_BYTE = 60000000;
		long streamTotal = 0l;			//接收流的容量
		int streamNum = 0;				//流需要分开的数量
		int leave = 0;					//文件剩下的字符数
		
		byte[] inOutb;  				//byte数组接受文件的数据
		
		//创建读入文件和写入文件
		FileInputStream fis = null;
		FileOutputStream fos = null;
		
		try {
			fis = new FileInputStream(inFile);
			fos = new FileOutputStream(outFile);
			
			
			//通过avalible方法取得流的最大字符数
			streamTotal = fis.available();
			
			//取得流文件需要分开的数量
			streamNum = (int)Math.floor(streamTotal/MAX_BYTE);
			
			//分开文件之后,剩余的数量
			leave  = (int)streamTotal%MAX_BYTE;
			
			//文件的容量大于60M时进入循环
			if(streamNum >0){
				for(int i = 0;i< streamNum;++i){
					inOutb = new byte[MAX_BYTE];
					
					fis.read(inOutb, 0, MAX_BYTE);
					fos.write(inOutb);
				}
			}
			inOutb = new byte[leave];
				
			fis.read(inOutb, 0, MAX_BYTE);
			fos.write(inOutb);
			
		} catch (FileNotFoundException e) {
			
			e.printStackTrace();
		} catch (Exception ex){
			ex.printStackTrace();
		}finally{
			//关闭文件流
			try {
				fis.close();
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值