Day_21 IO流

1.IO

1.1流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作。

I : input 输入

O : output 输出

输入 : 就是把数据输入到内存中

输出 : 就是把内存中的数据写出到外面

1.2分类

按处理数据类型的不同,分为字节流和字符流

按数据流向的不同,分为输入流和输出流。(入和出是相对于内存来讲的) 

 按功能不同,分为节点流和处理流

节点流:直接操作数据源

处理流:对其他流进行处理

1.3四大抽象类

InputStream : 字节输入

OutputStream : 字节输出

Reader : 字符输入

Writer : 字符输出

1.4InputStream

 

1.4.1 read使用方式

read : 读取文件中的数据,一次读取一个字节,返回值是读取的字节的值(返回int类型),如果读取到文件末尾(读完了)返回-1

/**
 * FileInputStream:是字节输入流
 * 就是读取文件中的数据
 * 
 * 操作系统层面,一切皆文件
 * @author 18370
 *
 */
public class FileInputStream_01 {
public static void main(String[] args)throws Exception{
	//要读取文件,必须要找到他
	//如何找?两种方式:绝对路径和  相对路径
	//绝对路径:能够找到这个文件的全部路径
	//相对路径:相对于当前文件所在位置去找其他文件,./表示当前目录,../表示上级目录,../../表示上上级目录,../../../
	
	//FileInputStream fis=new FileInputStream("D:/test/a.txt");
	//eclipse中./找到的是当前项目
	FileInputStream fis=new FileInputStream("./sar/com/FileInputStream_01.java");
	//read:读取文件中的数据,一次读取一个字节,返回值是读取的字节的值(返回int类型),如果读取到文件末尾了(读完了)返回-1
	int i1=fis.read();
	System.out.println((char)i1);
}
}
/**
 * 循环读取
 * @author 18370
 *
 */
public class Io_02_FileInputStream_02 {
public static void main(String[] args) {
	//自动关闭
	try(FileInputStream fis=new FileInputStream("./src/com/IO_01_FileInputStream_01");){
		//声明temp为0
		int temp=0;
		//while死循环,对象fis调用read方法读取的值存在temp中,读到最后一个返回-1
		while((temp=fis.read())!=-1){
			//不换行打印字符型temp值
			System.out.print((char)temp);
		}
	}catch(Exception e){
		//打印错误的追踪栈帧,比较常用,适合程序员排错
		//解决方案
		e.printStackTrace();
	}
}
}

1.4.2read重载使用方式

/**
 * read方法的重载,可以传递一个字节数组,为了提高读取效率
 * 
 * 默认一次读取一个字节,如果传递了字节数组,则把数组一次性读满,再回来,然后下次去再读满,一直到读完
 * 
 * read(byte[]):一次读取一个数据,返回当前读取到的个数,如果到达文件末尾,返回-1
 * 

public static void main(String[] args){
	try(FileInputStream fis=new FileInputStream("./src/com/IO_01_FileInputStream_01.java");){
		//available:可以获取的字节数
		System.out.println(fis.available());
		//byte[]bytes=new byte[fis.available()];
		//声明一个bytes数组可以放1025个字节
		byte[]bytes=new byte[1025];
		//调用read方法将数组bytes里边的字节读取
		int count=fis.read(bytes);
		//new String(bytes)把自己数组里边所有的数据转换为字符串,但是可能有冗余数据
		//所以推荐使用new String(byte,0,count)只把本次读取的数据转换为字符串
		System.out.println(new String(bytes,0,count));
		//读取count里边的数据
		System.out.println(count);
		count=fis.read(bytes);
		
	
	}catch(Exception e){
		e.printStackTrace();
	}
}
/**
 * 循环读取
 * @author 18370
 *
 */
public class IO_04_FileInputStream_04 {
public static void main(String[] args) {
	try(FileInputStream fis=new FileInputStream("./src/com/IO_01_FileInputStream_01.java");){
		//声明这个数组可以放1025个字节
		byte[]bytes=new byte[1025];
		//声明count为0
		int count=0;
		//while循环,如果read方法读取bytes中的最后一个数返回-1
		while((count =fis.read(bytes))!=-1){
			//读取一个数并且直接转换成字符串
			System.out.println(new String(bytes,0,count));
		}
	}
	catch(Exception e){
	e.printStackTrace();
}}}

1.5Reader

 /**
 * FileReader字符输入流,字节输入流是一次读取一个字节,而字符输入流一次读取一个字符
 * 并且java中汉字采用的是unicode编码,而unicode编码占用16位,就是一个字符
 * 
 * 所以使用字符输入流可以解决汉字乱码问题
 * 
 * 字符流一般只用于纯文本文件,比如压缩包,图片,视频等,还是需要使用字节流处理
 * 
 * read():读取一个字符,返回该字符的int值,到达文件末尾返回-1
 * read(char[]):读取一个char数组,返回当前读取的个数,到达末尾返回-1

1.5.1read使用方式

public static void main(String[] args) {
	try(FileReader fr=new FileReader("./src/com/IO_01_FileInputStream_01.java")){
		int temp=0;
		while((temp=fr.read())!=-1){
			System.out.println((char)temp);
		}
	}catch(Exception e){
		e.printStackTrace();
	}
}

1.5.2read重载使用方式

1.6OutputStream

1.6.1概述

 

 

 1.6.2使用方式

1.7Writer

1.7.1概述

 

1.7.2使用方式

public static void main(String[] args) {
    try(
            FileWriter fw=new FileWriter("D:/a.txt");
            ){
    
        //写出字符串
        fw.write("你好吗?\n");
        char[]chars={'a','b','c','d'};
        //写出char数组
        fw.write(chars);
        //刷缓存
        fw.flush();
        
    }
catch(Exception e){
    e.printStackTrace();

}
}

 

 1.8缓冲流

1.8.1概述

1.8.2BufferedInputStream

 

/**
 * 字节输入缓冲流
 * @author 18370
 *
 */
public class BufferedInputStream_01 {
public static void main(String[] args) {
//	long startTime=System.currentTimeMillis();
//	try(
		//	FileInputStream fis=new FileInputStream("D;/a.txt");
			
		//	){
		//byte[]bytes=new byte[1024];
		//int temp=0;
	//	while((temp=fis.read(bytes))!=-1){
			//System.out.println(new String(bytes,0,temp));
		//}
		//long endTime=System.currentTimeMillis();
	//	System.out.println("读取完成,耗时:"+(endTime-startTime));
	long startTime=System.currentTimeMillis();
	try(
			FileInputStream fis=new FileInputStream("D:/a.txt");
			BufferedInputStream bis=new BufferedInputStream(fis);
			){
		byte[]bytes=new byte[1024];
		int temp=0;
		while((temp=bis.read(bytes))!=-1){
			 System.out.println(new String(bytes,0,temp));
		}
		long endTime=System.currentTimeMillis();
		System.out.println("读取完成,耗时 : "+(endTime-startTime));
		//字节流:120
	}catch(Exception e){
		e.printStackTrace();
	}
}

1.8.3BufferedOutputStream

1.8.4BufferedReader

/**
 * 字符输入缓冲流
 * 
 * 新增方法String readline():读取一行代码,返回值就是读到的数据,到达文件末尾返回null
 * @author 18370
 *
 */
public class BufferedReader_03 {
public static void main(String[] args) {
	try(
			FileReader fr=new FileReader("D:/a.txt");
			BufferedReader br=new BufferedReader(fr);){
		String temp=null;
		//读取一行
		while ((temp=br.readLine())!=null){
			System.out.println(temp);
		}
	}catch(Exception e){
		e.printStackTrace();
	}
			
}
}

 1.8.5BufferedWriter

/**
 * 字符输出缓冲流
 * @author 18370
 *
 */
public class BufferedWriter_04 {
public static void main(String[] args) {
	//因为下边finally里边要使用对象bw,所以要将它扩大作用域
	BufferedWriter bw=null;
	try{
			bw=new BufferedWriter(new FileWriter("D:/a.txt"));
			bw.write("你好吗");
			//换行
			bw.newLine();
		    bw.write("我很好");
			bw.flush();
			
}catch(Exception e){
	e.printStackTrace();
}finally{
	try{
		if(bw!=null){
			bw.close();
		}
		
	}catch(IOException e){
		e.printStackTrace();
	}
}
}}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值