java IO(二):内存操作流、管道流、打印流、System对IO的支持、BufferedReader

ByteArrayInputStream与ByteArrayOutputStream

    之前所讲解的程序,输入和输出都是从文件中来的,当然也可以将输入、输出的位置设置在内存之上。这时就要使用ByteInputStream与ByteOutputStream来完成输入、输出的功能了。

   ByteArrayInputStream:将数据从内存之中输入。

 构造方法:

public ByteArrayInputStream(byte[] buf)

   ByteOutputStream:将数据输出到内存之中。(ByteOutputStream内存实现了一个byte数组,用来储存输出到内存的数据)

   输出就是从内存中写数据:

public void write(int b)


范例,一个大小写字母转换的程序:

import java.io.* ;
public class ByteArrayDemo01{
	public static void main(String args[]){
		String str = "HELLOWORLD" ;		// 定义一个字符串,全部由大写字母组成
		ByteArrayInputStream bis = null ;	// 内存输入流
		ByteArrayOutputStream bos = null ;	// 内存输出流
		bis = new ByteArrayInputStream(str.getBytes()) ;	// 向内存中输出内容
		bos = new ByteArrayOutputStream() ;	// 准备从内存ByteArrayInputStream中读取内容
		int temp = 0 ;
		while((temp=bis.read())!=-1){
			char c = (char) temp ;	// 读取的数字变为字符
			bos.write(Character.toLowerCase(c)) ;	// 将字符变为小写
		}
		// 所有的数据就全部都在ByteArrayOutputStream中
		String newStr = bos.toString() ;	// 取出内容
		try{
			bis.close() ;
			bos.close() ;
		}catch(IOException e){
			e.printStackTrace() ;
		}
		System.out.println(newStr) ;
	}
};

管道流

  

范例:

import java.io.* ;
class Send implements Runnable{			// 线程类
	private PipedOutputStream pos = null ;	// 管道输出流
	public Send(){
		this.pos = new PipedOutputStream() ;	// 实例化输出流
	}
	public void run(){
		String str = "Hello World!!!" ;	// 要输出的内容
		try{
			this.pos.write(str.getBytes()) ;
		}catch(IOException e){
			e.printStackTrace() ;
		}
		try{
			this.pos.close() ;
		}catch(IOException e){
			e.printStackTrace() ;
		}
	}
	public PipedOutputStream getPos(){	// 得到此线程的管道输出流
		return this.pos ;	
	}
};
class Receive implements Runnable{
	private PipedInputStream pis = null ;	// 管道输入流
	public Receive(){
		this.pis = new PipedInputStream() ;	// 实例化输入流
	}
	public void run(){
		byte b[] = new byte[1024] ;	// 接收内容
		int len = 0 ;
		try{
			len = this.pis.read(b) ;	// 读取内容
		}catch(IOException e){
			e.printStackTrace() ;
		}
		try{
			this.pis.close() ;	// 关闭
		}catch(IOException e){
			e.printStackTrace() ;
		}
		System.out.println("接收的内容为:" + new String(b,0,len)) ;
	}
	public PipedInputStream getPis(){
		return this.pis ;
	}
};
public class PipedDemo{
	public static void main(String args[]){
		Send s = new Send() ;
		Receive r = new Receive() ;
		try{
			s.getPos().connect(r.getPis()) ;	// 连接管道
		}catch(IOException e){
			e.printStackTrace() ;
		}
		new Thread(s).start() ;	// 启动线程
		new Thread(r).start() ;	// 启动线程
	}
};

打印流

     在整个IO包中,打印流输出信息最方便的类。主要包含字节打印流(PrintStream)和字符打印流(PrintWriter)。打印流提供了非常方便的打印功能,可以打印任何的数据类型,例如:小数、整数、字符串等等。

1 . PrintStream类的常用方法:


   在这个类中定义了很多的print()和println()方法。System.out.println,可以打印任何的数据类型。

范例:

import java.io.* ;
public class PrintDemo01{
	public static void main(String arg[]) throws Exception{
		PrintStream ps = null ;		// 声明打印流对象
		// 如果现在是使用FileOuputStream实例化,意味着所有的输出是向文件之中
		ps = new PrintStream(new FileOutputStream(new File("d:" + File.separator + "test.txt"))) ;
		ps.print("hello ") ;
		ps.println("world!!!") ;
		ps.print("1 + 1 = " + 2) ;
		ps.close() ;
	}
};

2. 格式化输出

 

 如果觉得格式占位符无法记住的话,可以全部使用s%表示。

import java.io.* ;
public class PrintDemo02{
	public static void main(String arg[]) throws Exception{
		PrintStream ps = null ;		// 声明打印流对象
		// 如果现在是使用FileOuputStream实例化,意味着所有的输出是向文件之中
		ps = new PrintStream(new FileOutputStream(new File("d:" + File.separator + "test.txt"))) ;
		String name = "李兴华" ;	// 定义字符串
		int age = 30 ;				// 定义整数
		float score = 990.356f ;	// 定义小数
		char sex = 'M' ;			// 定义字符
		ps.printf("姓名:%s;年龄:%s;成绩:%s;性别:%s",name,age,score,sex) ;
		ps.close() ;
	}
};


System对于IO的支持

    System表示系统类,实际上System对于IO有一定的支持。

System中的常量 :


1.  System.out

   使用System.out输出的位置就是屏幕上。PrintStream是OutputStream的子类。

使用例:

import java.io.OutputStream ;
import java.io.IOException ;
public class SystemDemo01{
	public static void main(String args[]){
		OutputStream out = System.out ;		// 此时的输出流是向屏幕上输出
		try{
			out.write("hello world!!!".getBytes()) ;	// 向屏幕上输出
		}catch(IOException e){
			e.printStackTrace() ;	// 打印异常
		}
		try{
			out.close() ;	// 关闭输出流
		}catch(IOException e){
			e.printStackTrace() ;
		}
	}
};
2. System.err

   表示的是错误的标准输出,如果程序中出了错误,直接使用System.err打印输出即可。

范例:

public class SystemDemo02{
	public static void main(String args[]){
		String str = "hello" ;		// 声明一个非数字的字符串
		try{
			System.out.println(Integer.parseInt(str)) ;	// 转型
		}catch(Exception e){
			System.err.println(e) ;
		}
	}
};
在使用上,err与out使用方式完全相同。

err与out的区别:    

     一般来讲,System.out是将信息显示给用户看,是正常的信息显示,而System.err的信息正好相反是不希望用户看到的,会直接在后台打印,是专门显示错误的。

3. System.in

   System.in实际上是一个键盘的输入流,其本身是InputStream类型的对象。那么,此时就可以利用此方式完成从键盘读取数据的功能。

范例:

import java.io.InputStream ;
public class SystemDemo04{
	public static void main(String args[]) throws Exception {	// 所有异常抛出
		InputStream input = System.in ;	// 从键盘接收数据
		byte b[] = new byte[1024] ;	// 开辟空间,接收数据
		System.out.print("请输入内容:") ;	// 提示信息
		int len = input.read(b) ;	// 接收数据
		System.out.println("输入的内容为:" + new String(b,0,len)) ;
		input.close() ;	// 关闭输入流
	}
};


4. 输入、输出重定向


    可以使用重定向完成特定的输入、输出。

   例如:System.out是用户希望看到的信息,一旦出现错误,最好保存起来

import java.io.File ;
import java.io.FileOutputStream ;
import java.io.PrintStream ;
public class SystemDemo07{
	public static void main(String args[]){
		String str = "hello" ;		// 声明一个非数字的字符串
		try{
			System.out.println(Integer.parseInt(str)) ;	// 转型
		}catch(Exception e){
			try{
				System.setOut(
					new PrintStream(
						new FileOutputStream("d:"
							+ File.separator + "err.log"))) ;	// 输出重定向
			}catch(Exception e1){
			
			}
			System.out.println(e) ;
		}
	}
};

 在in和err上很少使用重定向。


BufferedReader 

 

  System.in本身是InputStream(字节流)  ,现在要求接收的是一个字符流,需要将字节流变为字符流才可以,InputStreamReader。

范例:

import java.io.* ;
public class BufferedReaderDemo01{
	public static void main(String args[]){
		BufferedReader buf = null ;		// 声明对象
		buf = new BufferedReader(new InputStreamReader(System.in)) ;	// 将字节流变为字符流
		String str = null ;	// 接收输入内容
		System.out.print("请输入内容:") ;
		try{
			str = buf.readLine() ;	// 读取一行数据
		}catch(IOException e){
			e.printStackTrace() ;	// 输出信息
		}
		System.out.println("输入的内容为:" + str) ;
	}
};
 没有任何的长度限制,输入以回车结束。
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值