java基础—IO流

IO流总结

1. 输入输出流,用于实现数据源和数据接收者间数据的传输

2. IO流的分类:
	按照方向分:输入流和输出流(以内存为参考进行判断)
		输入流:InputStream、 Reader
		输出流:OutputStream、Writer
		
	按照处理单元分:字节流和字符流
		字节流:InputStream、OutputStream
			System.in:标准输入流
		字符流:Reader、Writer
		
	按照功能分:节点流和处理流(包装流)
		节点流:直接与磁盘,内存,设备相连
			父类:InputStream、OutputStream、Reader、Writer
			文件子类:FileInputStream、FileOutputStream、FileReader、FileWriter
		处理流:是对一个已存在的流的封装,通过所封装的流的功能调用实现数据读写
			缓冲流:
				BufferedReader、BufferedWriter...
		
			数据流:DataInputStream、DataOutputStream
				能将java中的基本数据类型和String类型直接进行读写
				注意:读的顺序必须和写的顺序一致才行
			
			对象流:ObjectInputStream、ObjectOutputStream
				将java中的数据类型直接进行读写
				注意:如果对对象类型进行操作的话,该类必须实现Serializable接口,实现序列化
					序列化的版本号:用于解决新老版本不兼容的问题
					如果不希望将某个属性序列化,可以使用transient关键字进行修饰
			
			转换流:InputStreamReader、OuputStreamWriter
				实现字节流和字符流间的转换
				使用时必须传一个对应的字节流做实际参数
				
			打印流:PrintStream、PrintWriter
				使用时不抛出IOException
				System.out:标准输出流
3.serializable接口(处理对象流ObjectInputStream,ObjectOutputStream)
	在java.io.serializable中,内部没有任何的方法及属性,主要用于表示当前类型
	具有可序列化的能力
	要求:
		1.对象的类型要实现Serializable接口。(如果类的父类已经序列化了,
			子类具有同等能力)
		2.类中如果使用了其他的自定义类型则要求自定义要同样实现Serializable接口
		3.用static所修饰的变量不会被写入到文件中,(有初值的例外)
		4.如果某个变量不希望被存储,则需要使用transient关键字进行修饰
		5.序列化保存的知识变量的值,对于变量的任何修饰符都不能保存
		6.不同版本之间不兼容
示例代码:
整个文件夹的复制
<pre name="code" class="html">package test1;

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

/**
 * 文件夹的复制
 *
 */
public class CopyFileTest {

	public static void main(String[] args) {
		//源文件
		File srcDir=new File("G:\\中级项目\\managerClassSystem1");
		File destDir=new File("G:\\");
		
		
		copyAll(srcDir,destDir);
	}

	//将源文件整个复制到指定文件夹中
	private static void copyAll(File srcDir, File destDir) {
		if(srcDir.exists()){
			//创建目标文件夹
			File file=new File(destDir,srcDir.getName());
			file.mkdirs();
			
			//获取源文件中所有文件信息
			File[] fList=srcDir.listFiles();
			//依次对每一个文件进行判断,如果是文件夹,递归,继续创建文件夹;如果是文件,进行文件复制
			for(File f:fList){
				if(f.isDirectory()){
					copyAll(f,file);
				}else if(f.isFile()){
					copySingleFile(f,file);
				}
			}
		}else{
			System.out.println(srcDir+"不存在,无法进行复制");
		}
	}

	//带缓冲区的文件复制
	private static void copySingleFile(File srcFile, File destFile) {
			BufferedInputStream bis=null;
			BufferedOutputStream bos=null;
			
			try {
				bis=new BufferedInputStream(new FileInputStream(srcFile));
				bos=new BufferedOutputStream(new FileOutputStream(new File(destFile,srcFile.getName())));
				
				byte[] b=new byte[1024];
				int len=0;
				while((len=bis.read())!=-1){
					bos.write(b, 0, len);
					bos.flush();
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}finally{
				if(bos!=null){
					try {
						bos.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if(bis!=null){
					try {
						bis.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
	}
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值