java-(IO)之InputStream,FileInputStream,,Reader,PipedInputStream,ByteArrayInputStream

流的概念:
流是字节序列的抽象概念,例如文件输入输出设备,内存,内部通讯管道,或者TCP/Ip套接字等设备传输的数据序列,网络流提供了一种用统一的方式从各种输入输出设备中读取和写入字节数据的方法。
流和文件的区别:
文件是数据的静态
流是指传输是的形态
文件只是流可操作的一种,数据流是数据连续
流分为两类:节点流类和过滤流类


InputStream

InputStream描述了各种流的各种通用方法
InputStream类的方法
int read() //数据以整数形式返回,如果没有东西可读返回-1如果流没有结束又没有数据可读,流将会阻塞。
int类型占4个字节
int read(byte[]b,int off,int len)//b为待读取的数据,len为待读的长度,off是开始读的角标
long skip(long n)// 跳过输入流中的n个字节(包装类)
int available() // 返回当前输入流的可读的字节数,如果程序只是偶尔看是否有数据可读。坏处:浪费资源
void mark(int readlimit) //在输入流中建立标记,从建立标记位置开始你最多还可以读取多少个字节 (包装类)
void reset()  // 是和mark()配合使用的
boolean markSupported()  //返回当前流对象是否支持mark()和reset()操作
void  close()  // 当不对流对象操作时

问题:有了垃圾回收器,为什么还要调用close方法

java垃圾回收器只会管理程序中类实例对象,不会管理系统产生的资源,所以用close()通知系统关闭关闭对象产生的流资源

OutputStream

OutputStream  程序可以向其中连续吸入字节的对象叫输出流,在java中OutputStream类来描述所有输出流的抽象概念
OutputStream类的方法
void write(int b)//将一个整数的最低字节内容写到输出流,最高的被舍弃。
void write(byte[]b)//将字节数组b中的内容写入输出流对象中
void write(byte[]b,int off,int len)//讲字节数组中的从off开始到len写入输出流中
void flush()   //将内存缓冲区的内容清空并且输出到io设备中
void close()        //关闭输出流对象

使用内存缓冲区的好处:提高计算机系统的效率,但是会降低单个程序的效率,数据没有直接写入到外部设备,在网络中会造成数据后置降低了。
java中有的类实现缓冲区有的不实现


FileInputStream与FileOutputStream类

FileInputStream与FileOutputStream类分别用来创建磁盘文件的输入流和输出流对象,通过它们的构造函数来指定文件路径和文件名。
对同一个磁盘文件创建FileInputStream对象的两种方法:
(1)FileInputStream inOne=new FileInputStream("hello.test");
(2) File f=new File("hello.test");
FileInputStream inTwo =new FileInputStream(f);

思考:要将A文件的内容写入B文件,在程序代码中,是用输出类对象,还是用输入类对象来连接A文件并完成A文件的操作?

 

代码如下:

package IO;

import java.io.*;

public class FileStream {
	public static void main(String[] args) throws Exception {
		FileOutputStream out = new FileOutputStream("1.txt");
		out.write("www.it315.org".getBytes());
		out.close();
		byte buf[] = new byte[1024];
		File f = new File("1.txt");
		FileInputStream in = new FileInputStream(f);
		int len = in.read(buf);
		System.out.println(new String(buf, 0, len));

	}
}

Reader与Writer类

Reader与Writer类是所有字符流类的抽象基类,用于简化对字符串的输入输出编程,即用与读写文本数据。

编程实例:

用FileWrite类向文件中写入一个串字符,然后用FileReader读出写入的内容。

 FileStream2.java
package IO;

import java.io.*;

public class FileStream2 {
	public static void main(String[] args) throws Exception {
		FileWriter out = new FileWriter("hello2.txt");
		out.write("www.it315.org");
		out.close();
		char[] buf = new char[1204];
		FileReader in = new FileReader("hello2.txt");
		int len = in.read(buf);
		System.out.println(new String(buf, 0, len));
	}
PipedInputStream类与PipedOutputStream用于在应用程序中创建管道通信
使用管道流类的好处:可以实现各个程序模块之间松耦合通信
实例:
Sender.java

package IO;

//lesson06
import java.io.PipedOutputStream;

public class Sender extends Thread {
//用于发送数据的类
	private PipedOutputStream out = new PipedOutputStream();//创建对象向外部写数据
/*返回PipeOutputStream对象以便和PipeInputStream进行相连*/
	public PipedOutputStream getOutputStream() {
		return out;

	}
/*run()用于向PipeOutputStream写入一串数据*/
	public void run() {
		String strInfo = new String("hello Sender");
		try {
			out.write(strInfo.getBytes());
			out.close();
		} catch (Exception e) {
			e.printStackTrace();

		}
	}

}

Receive.java

package IO;
//lesson06
import java.io.PipedInputStream;

public class Receive extends Thread{
//用于接受数据的类
	private PipedInputStream in=new PipedInputStream();
	public PipedInputStream getIntputStream(){
		return in;
		
	}
	public void run(){
		
	
		byte[]buf=new byte[1024];
		try{
			int len=in.read(buf);
			System.out.println("the following message is"+new String(buf,0,len));
		in.close();
		}catch(Exception e){
			e.printStackTrace();
			
		}
	}

}

PipedStreamTest.java

package IO;

import java.io.*;

//lesson06
public class PipedStreamTest {

public static void main(String[]args)throws Exception{
	Sender t1=new Sender();
	Receive t2=new Receive();
	PipedOutputStream out=t1.getOutputStream();
	PipedInputStream in=t2.getIntputStream();
	out.connect(in);//管道之间进行连接
	t1.start();//开始写数据
	t2.start();//开始读数据
	
}
}


ByteArrayInputStream和ByteArrayOutputStream,用于以IO流的方式来完成对字节数组内容中的读写。实现内存虚拟文件或者内存映像文件的功能。

ByteArrayInputStream构造函数
ByteArrayInputStream(byte[]buf)//使用字节数组中所有的数据作为数据源
ByteArrayInputStream(byte[]buf,int offset,int length)//从数组buf中的offset开始取出length字节作为数据源
ByteArrayOutputStream构造函数
ByteArrayOutputStream()
ByteArrayInputStream(int)
编程实例:编写一个把输入流中所有的英文字母变成大写字母,然后将结果写入到一个输出流对象,用这个函数来将一个字符串中的所有字符转换成大写
ByteArrayTest.java

package IO;

import java.io.*;

public class ByteArrayTest {
	public static void main(String args[]) {
		String tmp = "agsdfsf dsfs";
		byte[] src = tmp.getBytes();
		ByteArrayInputStream input = new ByteArrayInputStream(src);
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		transform(input, output);
		byte[] result = output.toByteArray();
		System.out.println(new String(result));
	}

	public static void transform(InputStream in, OutputStream out) {
		/* 将输入流中的数据读取出来然后进行转换 */
		int ch = 0;
		try {
			while ((ch = in.read()) != -1) {

				int upperCh = Character.toUpperCase((char) ch);
				out.write(upperCh);// 输出流中写入转换后的字节
			}
		} catch (Exception e) {
			e.printStackTrace();

		}

	}
}












}


 

 


 


 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值