栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

IO流

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

IO流

IO流的概述
  • IO:输入/输出(Input/Output)

  • 流:是一种抽象概念,是对数据传输的总称。也就是说数据在设备间的传输称为流,流的本质就是数据传输

  • IO流就是用来处理设备间数据传输问题的
    常用的应用:文件复制、文件上传、文件下载

以游戏程序为中心读取文件就是输入,写入文件是输出

IO流的分类

  • IO流在java中从输入输出角度分类:
    1.输入流
    2.输出流
  • IO流在java中从数据的角度来分类:
    1.字符流
    文本,我们能读的懂的都可以认为是字符流。比如:文章,java文件等等
    2.字节流
    二进制的数据,这种数据一般用文本打开我们读不懂。比如,图片文件,mp3文件,等等。
一、字节流 字节流基类
  • InputStream:这个抽象类是表示字符输入流的所有类的超类
  • OutputStream:这个抽象类是表示字节输出流的所有类的超类
  • 子类名特点:子类名称都是以其父类名作为子类名的后缀
(一)字节流写数据 OutputStream

常用子类 FileOutputStream

1.构造方法

2.三种写入方法

1.void write(byte[] b) 
将 b.length字节从指定的字节数组写入此输出流 (一次写一个字节数组)

2.void write(byte[] b, int off, int len) 
从指定的字节数组写入 len个字节,从偏移 off开始输出到此输出流 
(一次写一个字节数组的部分数据)

3.void write(int b) 
将指定的字节写入此输出流 (一次写一个字节数据)

3.两个小问题

字节写入流如何实现换行?

  • 写完数据后,加换行符
    windows:rn
    linux:n
    mac:r
  
public class IOTest5 {

    public static void main(String[] args) {

        //创建一个文件
        File file = new File("test.txt");
       OutputStream writer = null;

        try {
            //IO流是需要关闭的,如果不这样设计就会不能关闭资源
            writer = new FileOutputStream(file);
            for (int i = 0; i < 100; i++) {
                writer.write("HelloWorldrn");
                //每次写入10个helloworld的时候做一次flush
                if(i % 10 == 0){
                    writer.flush();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //判断writer不是空防止空指针异常
            if(writer != null) {
                try {
                    //在关闭前会做flush的事情
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

字节写入流如何实现追加?

  • FileOutputStream(String name, boolean append)
  • 创建文件输出流以指定的名称写入文件,如果第二个参数为ture,则将字节写入文件的末尾而不是开头
 1.创建文件
  2.创建输出流对象
   3.把流指向指定的文件
	4.释放资源
public class IOTest4 {

    public static void main(String[] args) {


        OutputStream writer = null;

        try {
            //IO流是需要关闭的,如果不这样设计就会不能关闭资源
            //writer = new FileOutputStream("test1.txt", true);
            writer = new FileOutputStream(new File("test1.txt"), true);
            writer.write("liangliang");

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //判断writer不是空防止空指针异常
            if(writer != null) {
                try {
                    //在关闭前会做flush的事情
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

(二)字节流读数据 InputStream

常用子类 FileInputStream

1.构造器

2.方法

1.int read() 
从该输入流读取一个字节的数据
  
2.int read(byte[] b) 
从该输入流读取最多 b.length个字节的数据为字节数组。
 
3.int read(byte[] b, int off, int len) 
从该输入流读取最多 len字节的数据为字节数组。 

案例

将“d:/jiaoben5735.rar”进行读取,并且复制到 “f:/jiaoben5735.rar” 中

public class CopyDemo {    
	public static void main(String[] args) { 
	       
		InputStream in = null;        
		OutputStream out = null;        
		try {            
		//创建字节输入流            
			in = new FileInputStream("d:/jiaoben5735.rar");
			 //创建字节输出流            
		    out = new FileOutputStream("f:/jiaoben5735.rar");
		    //定义一个字节数组,输入流读取写入数组,输出流读取数组中的内容写入字节输出流对象            
		    byte[] bs = new byte[1024];            
		    int len = -1;            
		    while((len = in.read(bs)) != -1){                
		    //把读取的数据写入字节输出流                
		    out.write(bs, 0, len);            
		    }        
		  } catch (Exception e) {            
		   e.printStackTrace();        
		   } finally {            
		   try {                
		   if(out != null){                    
		   out.close();                
		   }                
		   if(in != null){                    
		   in.close();                
			}            
		} catch (IOException e) {               
		    e.printStackTrace();            
		    }        
		}    
	}
}
(三)字节流 高效缓冲区

1.写数据缓冲流

构造器

 1.BufferedOutputStream(OutputStream out) 
 创建一个新的缓冲输出流,以将数据写入指定的底层输出流
   
 2.BufferedOutputStream(OutputStream out, int size) 
 创建一个新的缓冲输出流,以便以指定的缓冲区大小将数据写入指定的底层输出流 

方法

1.void write(byte[] b, int off, int len) 
从指定的字节数组写入 len个字节,从偏移 off开始到缓冲的输出流。
 
2.void write(int b) 
将指定的字节写入缓冲的输出流 

2.读数据缓冲口

构造器

1.BufferedInputStream(InputStream in) 
创建一个 BufferedInputStream并保存其参数,输入流 in ,供以后使用

2.BufferedInputStream(InputStream in, int size) 
创建 BufferedInputStream具有指定缓冲区大小,并保存其参数,输入流 in ,供以后使用 

方法

1.int read() 
从该输入流读取一个字节的数据

2.int read(byte[] b, int off, int len) 
从给定的偏移开始,将字节输入流中的字节读入指定的字节数组 

案例
利用高校缓冲区将图片 D:/feiji.jpg 进行读取并且复制到 plane.jpg 中

public class BufferCopy {    
	public static void main(String[] args) {        
	//定义一个高效缓存字节流        
	BufferedInputStream in = null;        
	BufferedOutputStream out = null;        
	try {            
	//创建一个高效缓存字节流对象            
	in = new BufferedInputStream(new FileInputStream("D:/feiji.jpg"));
	out = new BufferedOutputStream(new FileOutputStream("plane.jpg"));
	//定义一个字节数组            
	byte[] bs = new byte[1024];            
	//定义一个标志            
	int len = -1;            
	while((len = in.read(bs)) != -1){                
		out.write(bs, 0, len);            
		}        
	} catch (Exception e) {            
	e.printStackTrace();        
	} finally {            
	try {                
	if(out != null){                   
	 out.close();                
	 }                
	 if(in != null){                    
	 in.close();                
	 }           
	  } catch (IOException e) {               
	   e.printStackTrace();            
	   }       
    }   
 }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/424813.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号