java IO 流(四)

2014-11-24 11:59:56 ? 作者: ? 浏览: 165
!!") ; // 使用字符流输出
out.close() ;
InputStreamReader
File f = new File("d:" + File.separator + "test.txt") ;
Reader reader = null ;
reader = new InputStreamReader(new FileInputStream(f)) ; // 将字节流变为字符流
char c[] = new char[1024] ;
int len = reader.read(c) ; // 读取
reader.close() ; // 关闭
System.out.println(new String(c,0,len)) ;
五, 内在操作流:ByteArrayInputStream, ByteArrayOutputStream, 主要是程序内容与内存的交换,所以操作对象是以内在为准的,
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) ;
六, 管道流: PipedOutputStream ,PipedInputStream 不同线程中的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() ; // 启动线程
}
};
七, 打印流: PrintWriter(字符), PrintStream(字节)
PrientSteam (OutputSteam out) 属于装饰设计模式,即好看又方便
PrintStream ps = null ; // 声明打印流对象
// 如果现在是使用FileOuputStream实例化,意味着所有的输出是向文件之中
ps = new PrintStream(new FileOutputStream(new File("f:" + File.separator + "test.txt"))) ;
String name = "钟志钢" ; // 定义字符串
int age = 26 ; // 定义整数
float score = 990.356f ; // 定义小数
char sex = 'M' ; // 定义字符
//%s 表示String , %d 表示int , %f 表示小数, %c 表示字符,其实都可以用s,因为所有都可以向String转
ps.printf("姓名:%s;年龄:%s;成绩:%s;性别:%s",name,age,score,sex) ;
ps.close() ;
以前内容的文件内容结果: 姓名:钟志钢;年龄:26;成绩:990.356;性别:M
八, BufferedReader , buffer缓冲区,
接收键盘接收的数据:
BufferedReader buf = null ; // 声明对象
-->

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: