java IO流基础总结一

流的划分

按流的方向分类:

  1. 输入流:数据流向是数据源到程序(以InputStream、Reader结尾的流)。
  2. 输出流:数据流向是程序到目的地(以OutPutStream、Writer结尾的流)。

这里需要注意的是在javaIO流中输入输出都是相对于程序而言的

按处理的数据单元分类:

  1. 字节流:以字节为单位获取数据,命名上以Stream结尾的流一般是字节流,如FileInputStream、FileOutputStream。支持字符,图片 ,视频,音频等文件。
  2. 字符流:以字符为单位获取数据,命名上以Reader/Writer结尾的流一般是字符流,如FileReader、FileWriter。底层还是基于字节流。仅仅支持字符文件。

按处理对象不同分类:

  1. 节点流:可以直接从数据源或目的地读写数据,如FileInputStream、FileReader、DataInputStream等。以字节为单位直接操作“文件”。
  2. 处理流:不直接连接到数据源或目的地,是”处理流的流”。通过对其他流的处理提高程序的性能,如BufferedInputStream、BufferedReader等。处理流也叫包装流。

##四大IO流抽象类

InputStream/OutputStream和Reader/writer类是所有IO流类的抽象父类,我们有必要简单了解一下这个四个抽象类的作用。然后,通过它们具体的子类熟悉相关的用法。下面这个表展示了这四个抽象类和常用方法。
[外链图片转存失败(img-ZMFFm4fH-1564729829739)(https://i.postimg.cc/gJz43VD9/image.png)]
具体的思维导图:
抽象的子类

IO标准步骤:

  1. 创建源,
  2. 选择流
  3. 操作:读或写
  4. 释放资源,释放系统资源

以下是一个最简单的IO输入流:一个一个地读取文件中的内容。

public class IOTest1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        //创建数据源 
		File file = new File("a.txt");
		InputStream in = null;
		try {
            //选择流:这里是FileInputStream
			in = new FileInputStream(file);
            int temp;
			while((temp=in.read()) != -1)
			{
				System.out.println((char)temp);
			}
			//释放系统资源
			in.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 

	}

}

对以上代码改进,可以一段一段地读取:

public class IOTest3 {

	public static void main(String[] args) {

		File src = new File("a.txt");
		
		InputStream in = null;
		
		try {
		
			in = new BufferedInputStream( new FileInputStream(src));
			//分段读取,每次读取三个字节.
			byte[] flush = new byte[3];
			int len = -1;//接收长度
			while((len=in.read(flush)) != -1)
			{
				String string = new String(flush, 0, len);
				System.out.println(string);
			}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			try {
				if (null != in) {
					in.close();
				}
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

}

文件字节输出流:

public class IOTest4 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		File dest = new File("test.txt");
		OutputStream out = null;
		
		try {
			out = new FileOutputStream(dest,true);//true表示追加,fale表示覆盖。
			String string = "Io is so easy";
			byte[] datas = string.getBytes();//解码。
			out.write(datas, 0, datas.length);
			out.flush();//习惯写法,写操作之后加上flush().
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			if(out != null)
			{
				try {
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
	}

}

Reader的简单代码:

public class ReaderTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		File src = new File("a.txt");
		
		Reader reader = null;
		
		try {
			reader =new BufferedReader(new FileReader(src));
		
			char[] flush = new char[1024];//这里是char数组。
			int len = -1;//接收长度
			while((len=reader.read(flush)) != -1)
			{
				String string = new String(flush, 0, len);
				System.out.println(string);
			}
		
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			try {
				if (null != reader) {
					reader.close();
				}
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}


}

Writer的简单代码:

public class WriterTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		File dest = new File("test.txt");
		Writer writer = null;
		
		try {
			writer = new FileWriter(dest);
			
			//写法一:
//			String string = "你好 世界";
//			char[] datas = string.toCharArray();
//			writer.write(datas, 0, datas.length);
			
//			写法二:
//			String string1 = "曾经沧海难为水";
//			writer.write(string1);
//			writer.flush();//习惯写法,写操作之后加上flush().

			//写法三:
			writer.append("你好 世界");
			writer.flush();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
				try {
					if(writer != null)
					writer.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			
		}
		
	}
	

}

未完待续。。。。。。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值