1. 数据源
data source.提供原始数据的原始媒介。常见的:数据库、文件、其他程序、内存、网络连接、IO设备
2. 流的概念
数据源就像水箱,流就像水管中流着的水流,程序就是我们最终的用户。流是一个抽象、动态的概念,是一连串连续动态的数据集合。
3. java实现IO流的体系结构
4. 实现特定操作的流
1)对文件进行操作的流 FileOutputStream/FileInputStream , FileWriter/FileReader
FileInputStream用于读取诸如图像数据之类的原始字节流。要读取字符流,请考虑使用FileReader
。
available(): 并不是指定文件还有多少数据没有读,不然,如果是100g的大文件,返回得数就溢出了。不过,对于普通文件的话可以看做是剩余的未读数据。
返回下一次对此输入流调用的方法可以不受阻塞地从此输入流读取(或跳过)的估计剩余字节数。
FileOutputStream
用于写入诸如图像数据之类的原始字节的流。要写入字符流,请考虑使用FileWriter
。也就是说,处理文本的话使用字符流。
下面是一个实现文本文件复制功能的例子:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FileCopyTools {
/**
* @param args
*/
public static void main(String[] args) {
if(fileCopy(args[0],args[1])){
System.out.println("文件拷贝成功");
}else{
System.out.println("文件拷贝失败");
}
}
public static boolean fileCopy(String srcName, String destName){
File srcFile, destFile;
// FileReader reader;
// FileWriter writer;
FileInputStream reader;
FileOutputStream writer;
boolean flag = false;
try{
srcFile = new File(srcName);
destFile = new File(destName);
// reader = new FileReader(srcFile);
// char[] buff = new char[100];
reader = new FileInputStream(srcFile);
byte[] buff = new byte[100];
int len;
StringBuffer strBuffer = new StringBuffer();
while((len = reader.read(buff)) != -1){
strBuffer.append(new String(buff,0,len));
}
// writer = new FileWriter(destFile);
// writer.write(strBuffer.toString());
writer = new FileOutputStream(destFile);
writer.write(strBuffer.toString().getBytes());
flag = true;
writer.flush();
reader.close();
writer.close();
}catch(FileNotFoundException e){
System.out.println("文件读取异常:= " + e.getMessage());
}catch(IOException e){
System.out.println("读取数据异常:= " + e.getMessage());
}
return flag;
}
}
2) 实现对内存虚拟文件功能的流 ByteArrayOutputStream/ByteArrayInputStream, CharArrayWriter/CharArrayReader
ByteArrayInputStream
包含一个内部缓冲区,该缓冲区包含从流中读取的字节。内部计数器跟踪read
方法要提供的下一个字节。
FileInputStream是把文件当做数据源。ByteArrayInputStream则是把内存中的某个数组当做数据源。这个也就是实现了内存虚拟文件功能
ByteArrayOutputStream:
此类实现了一个输出流,其中的数据被写入一个 byte 数组。缓冲区会随着数据的不断写入而自动增长。可使用 toByteArray()
和 toString()
获取数据。 ByteArrayOutputStream将一个输出流指向一个Byte数组,但这个Byte数组是ByteArrayOutputStream内部内置的,不需要我们来定义。
注:不需要关闭流的,但是调用close也没有问题,close不做任何事情。因为ByteArrayOutputStream本身操作的是数组,并没有打开文件描述符之类的,所以不需要关闭。
将输入流的内容读入到一个数组中返回:
byte[] getBytesFromIS(FileInputStream fis){
ByteArrayOutputStream baos = null;
int temp = 0;
try {
baos = new ByteArrayOutputStream();;
while((temp=fis.read())!=-1){
baos.write(temp);
}
return baos.toByteArray(); //从而返回一个包含输入流所有内容的数组
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
3) 实现线程通信功能的流PipedOutputStream/PipedInputStream , PipedWriter/PipedReader
/**
* 生产者线程
* @author
*
*/
class Producer implements Runnable{
private PipedOutputStream out = new PipedOutputStream();
public PipedOutputStream getOut(){
return out;
}
public void run(){
String data = "hello! consumer!";
try {
out.write(data.getBytes());
out.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
/**
* 消费者线程
* @author
*
*/
class Consumer implements Runnable{
private PipedInputStream in = new PipedInputStream();
public PipedInputStream getIn(){
return in;
}
public void run(){
byte[] buff = new byte[100];
int len;
try {
while((len = in.read(buff))!=-1){
String data = new String(buff,0,len);
System.out.println("下列数据来自于生产者线程:= " + data);
}
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class PipedStream {
/**
* @param args
*/
public static void main(String[] args) {
Producer producer = new Producer();
Consumer consumer = new Consumer();
PipedInputStream in = consumer.getIn();
PipedOutputStream out = producer.getOut();
try {
in.connect(out); // 进行线程交互的方法
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
new Thread(producer).start();
new Thread(consumer).start();
}
}
4) 实现各种类型数据流读写功能的流 DataOutputStream/DataInputStream
FileOutputStream fos = null;
DataOutputStream dos = null;
FileInputStream fis = null;
DataInputStream dis = null;
try {
fos = new FileOutputStream("data.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
dos = new DataOutputStream(bos);
dos.writeInt(899);
dos.writeDouble(1000.11);
dos.writeBoolean(false);
// dos.writeBytes("bytes");
// dos.writeChars("chars");
dos.writeUTF("utf");
dos.close();
fis = new FileInputStream("data.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
System.out.println(dis.readInt());
System.out.println(dis.readDouble());
System.out.println(dis.readBoolean());
System.out.println(dis.readUTF());
dis.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
5) 实现缓存功能的流 BufferedOutputStream/BufferedInputStream , BufferedWriter/BufferedReader
6) 对象类型数据读写功能的流
7) 实现打印功能的流
8) 字节与字符转换功能的流 OutputStreamWriter/InputStreamWriter,OutputStreamReader/OutputStreamReader
示例代码如下: //接受用户的输入
BufferedReader bReader = null;
try {
System.out.println("名字:");
bReader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("用户输入:"+bReader.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
9) 实现压缩功能的流ZipOutputStream/ZipInputStream