IO流基础知识点

一、定义

输入流:从文件或者网络中读取内容到内存中

输出流:从内存中写入内容到文件或者网络中

 

二、分类

流可以分为字节流、字符流

字节流:以字节从流中读取数据或者往流中写入数据,通常用来处理二进制文件,比如图像和声音。比较常用,也推荐使用。

              基类为InputStream和OutputStream。

字符流:以字符从流中读取数据或者往流中写入数据,通常用来处理字符或者字符串数据。

              基类为Reader和Writer。

字节流转换为字符流:java中提供了两个适配器,分别是InputStreamReader和OutputStreamWriter。

三、InputStream和OutputStream详解

InputStream:一个抽象类,不能实例化

InputStream is = new InputStream();//写法错误,因为抽象类无法实例化

 正确写法如下:

File file = new File("D:\input.txt");
InputStream is = new FileInputStream(file);

主要功能:将数据一个字节一个字节的读到内存中

主要方法:

  1.  int read(byte[] b):从当前位置从输入流中读取b.length个字节的数据存放到b中,实际读取到的数据要看输入流中的字节数,最多读取的数量就是b.length,返回值为实际读取的字节数。
  2. int read(byte[] b,int off,int len):从当前位置从输入流中读取len个字节的数据存放到b中,第一个字节存放的位置为b[off],第二个为b[off+1],依次类推,实际中读取的数据可能并没有len个字节,具体看输入流中还存在多少数据,返回值为实际读取的字节数。
  3. int  read():从输入流中读取数据的下一个字节到java程序内部中,返回值为0到255的int类型的值,返回值为字符的ACSII值(如a就返回97,n就返回110),如果没有可用的字节,因为已经到达流的末尾,-1返回的值,运行一次只读一个字节,所以,它就经常与while((len =inputstream.read()) != -1)一起使用。
  4.  int close():关闭此输入流并释放此输入流中占用的所有系统资源。

OutputStream:一个抽象类,不能实例化

OutputStream os = new OutputStream();//写法错误,因为抽象类无法实例化
正确写法如下:

 File file = new File("D:\output.txt");

OutputStream os = new FileOutputStream(file);

     
主要功能:将数据一个字节,一个字节的写入到文件或者网络中

主要方法:

  1.  void write(byte[] b):将字节数组b中的数据全部写入到输出流中,注意,仅仅是写入到流中,要写入到文件还需要调用flush()函数
  2.  void write(byte[] b,int off,int len):将字节数组b中的数据,从off位置开始算起,写入len个字节到输出流,即将b[off],   b[off+1]...b[off+len-1]这些字节写入到输出流中。
  3.  void flush():刷新此输出流,并将该流中缓冲的字节数据全部写入到文件或者网络中
  4. void close():关闭输出流并释放该输出流占用的所有系统资源。

四、具体实例如下:

从指定文件中读取内容,并显示在控制台上

public static void read(){
	String pathName = "D:/input.txt";
	File file = new File(pathName);
	InputStream is = null;
	try{
		is = new FileInputStream(file);
		byte[] b = new byte[1024];
                is.read(b);//测试输入输出流(后面还有很多空格)
		System.out.println("读取的内容为:"+new String(b));//由于b字节数组长度为1024,如果文件较小,则会有大量填充空格
	}catch(Exception e){
		e.printStackTrace();
	}finally{
	    if(is!=null){
		    try {
		         is.close();
		    } catch (IOException e) {
			    e.printStackTrace();
		    }
		}
	}
}

从指定文件中读取内容,并显示指定长度的内容到控制台,不包括多余的空格

public static void readLength(){
	String pathName = "D:/input.txt";
	File file  = new File(pathName);
	InputStream is = null;
	try{
		is = new FileInputStream(file);
		byte[] b = new byte[1024];
		int len = is.read(b);
		System.out.println(new String(b,0,len));//测试输入输出流(不包含空格)
    }catch(Exception e){
		e.printStackTrace();
	}finally{
		if(is!=null){
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

从指定文件中逐字节的读取文件内容,显示在控制台

public static void readByByte(){
		InputStream is = null;
		try{
			File file = new File("D:/input.txt");
			byte[] b = new byte[(int)file.length()];
			is = new FileInputStream(file);
			 for (int i = 0; i < b.length; i++) {
				 b[i]=(byte)is.read();
			 }
			System.out.println(new String(b));//测试输出输入流
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

向指定的文件中写入指定的字符串

public static void write(){
		String pathName = "D:/output.txt";
		File file = new File(pathName);
		FileOutputStream fos = null;
		try{
			fos = new FileOutputStream(file);
			String str = "Hello World!";
			byte[] b = str.getBytes();
			for(int i=0;i<b.length;i++){
				fos.write(b[i]);
			}
			fos.close();
		}catch(Exception e){
			e.printStackTrace();
		}
}

向指定的文件中写入某个文件中的内容

public static void writeFromFile(){
		byte[] buffer = new byte[512];
		int numberRead = 0;
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try{
			fis = new FileInputStream("D:/input.txt");
			fos = new FileOutputStream("D:/output.txt");
			while((numberRead = fis.read(buffer))!=-1){
				fos.write(buffer, 0, numberRead);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				fis.close();
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

参考文章:

https://www.cnblogs.com/QQ846300233/p/6046388.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值