java I/O流

Java I/O流简介:

为使程序设计简单明了,Java把程序的输入\输出抽象为流(Stream),而其中输入或输出的数据称为数据流.简单地说:流就是指在计算机与输入\输出之间流动的数据序列.序列中的数据可以是二进制数据,也可以是其他符合某种格式规定的特定数据,如字符流等。

按照数据的流动方向:

输入流
输出流

按照处理数据的类型:

字节流
字符流

按照流是否直接与特定的地方相连:

节点流
处理流

输入流与输出流:

按照数据流相对于处理程序的流动方向来划分。








字节流与字符流:

按处理数据的类型,流可以分为字节流与字符流,它们处理信息的基本单位分别是字节(byte)与字符(char)。四个抽象类:

InputStream 字节输入流

OutputStream字节输出流

Reader 字符输入流

Writer 字符输出流


InputStream 字节输入流:

三个基本的读方法
abstract int read() :读取一个字节数据,并返回读到的数据,如果返回-1,表示读到了输入流的末尾。
int read(byte[]b) :将数据读入一个字节数组,同时返回实际读取的字节数。如果返回-1,表示读到了输入流的末尾。
int read(byte[]b, intoff, intlen) :将数据读入一个字节数组,同时返回实际读取的字节数。如果返回-1,表示读到了输入流的末尾。off指定在数组b中存放数据的起始偏移位置;len指定读取的最大字节数。
其它方法
long skip(longn) :在输入流中跳过n个字节,返回实际跳过的字节数。
int available() :返回在不发生阻塞的情况下,可读取的字节数。
void close() :关闭输入流,释放和这个流相关的系统资源。


OutputStream字节输出流:

三个基本的写方法
abstract void write(intb) :往输出流中写入一个字节。
void write(byte[]b) :往输出流中写入数组b中的所有字节。
void write(byte[]b, intoff, intlen) :往输出流中写入数组b中从偏移量off开始的len个字节的数据。
其它方法
void flush() :刷新输出流,强制缓冲区中的输出字节被写出。
void close() :关闭输出流,释放和这个流相关的系统资源。


Reader 字符输入流:

public int read( ) 读入的字符转为int返回
public int read(char b[])
public int read(char b[], int off, int len)
public long skip(long n )
public void mark(int readlimit)
public void reset( )
public boolean markSupport( )
public void close( )


Writer 字符输出流:

public void write(int b) b的低两字节写到输出流
public void write(char b[])
public void write(char b[], int off, int len)
public void write(String s)
public void write(String s, int off, int len)
public void flush( )
public void close( )


节点流与处理流:

按照流是否直接与特定的地方(如磁盘、内存、设备等)相连,分为节点流与处理流两类。
节点流(Node Stream):可以从或向一个特定的节点直接读写数据,如FileReader。
处理流(Processing Stream):又称过滤流,是对一个已存在流的连接和封装,进而实现功能更为强大的数据读写功能。如BufferedReader

节点流:从特定的地方读写的流类,例如:磁盘或一块内存区域。
处理流:使用节点流作为输入或输出。处理流是使用一个已经存在的输入流或输出流连接创建的。

处理流的构造方法总是要带一个其他流对象做参数
BufferedReader in1 = new BufferedReader(new FileReader(file));
BufferedReader in2 = new BufferedReader(new
InputStreamReader(new FileInputStream(file)));

I/O流的链接:

Input Stream Chain

Output Stream Chain



import java.io.*;
public class IOTestProcess {

	public static void main(String[] args) 
	{

      try {
    	  FileOutputStream fos=new FileOutputStream("cde.txt");
    	  BufferedOutputStream bos=new BufferedOutputStream(fos);
    	  DataOutputStream dos=new DataOutputStream(bos);
    	  int a=90;
    	  boolean b=false;
    	  char c='c';
    	  double d=48.796;
    	  
    	  dos.writeInt(a);
    	  dos.writeBoolean(b);
    	  dos.writeChar(c);
    	  dos.writeDouble(d);
    	  dos.flush();
    	  dos.close();
    	  
    	  
    	  FileInputStream fis=new FileInputStream("cde.txt");
    	  DataInputStream dis=new DataInputStream(fis);
    	  
    	  int e=dis.readInt();
    	  boolean g=dis.readBoolean();
    	  char f=dis.readChar(); 
    	  double h=dis.readDouble();
    	  System.out.println(e);
    	  System.out.println(f);
    	  System.out.println(g);
    	  System.out.println(h);
		
	} catch (Exception e) {
		e.printStackTrace();
	}

	}

}

import java.io.*;
public class IOTestChar {
	
	/*public static void main(String[] args) {
		try {
			FileReader fr=new FileReader("src\\IOTestChar.java");
			char[] cbuf=new char[1024];
			int len=fr.read(cbuf);
			System.out.println(new String(cbuf,0,len));
			fr.close();
			FileWriter fos=new FileWriter("abc.txt");
			fos.write(cbuf, 0, len);
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/

	public static void main(String[] args) {
		try {
			FileInputStream fis=new FileInputStream("e:\\android\\Earth.bmp");
			byte[] buf=new byte[fis.available()];
			int len=fis.read(buf);
			System.out.println(new String(buf,0,len));
			fis.close();
			FileOutputStream fos=new FileOutputStream("1.bmp");
			fos.write(buf, 0, len);
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值