ByteArrayOutputStream 主要将内容从内存输出
使用内存操作流将一个大写字母转化为小写字母
??? /**
??? * 使用内存操作流将一个大写字母转化为小写字母 ??? * */
??? import java.io.*; ??? class hello{
??? public static void main(String[] args) throws IOException { ??? String str=\??? ByteArrayInputStream
ByteArrayInputStream(str.getBytes());
??? ByteArrayOutputStream output=new ByteArrayOutputStream(); ??? int temp=0;
??? while((temp=input.read())!=-1){ ??? char ch=(char)temp;
??? output.write(Character.toLowerCase(ch)); ??? }
??? String outStr=output.toString(); ??? input.close(); ??? output.close();
??? System.out.println(outStr); ??? } ??? }
input=new
【运行结果】: rollenholt
内容操作流一般使用来生成一些临时信息采用的,这样可以避免删除的麻烦。
四
您所在的位置:开发 > Java > 热点推荐 > Java中的IO整理完整版(4)
Java中的IO整理完整版(4)
2011-09-19 16:17 Rollen Holt Rollen Holt 我要评论(0) 字号:T | T
本文章基本覆盖了java IO的全部内容,java新IO没有涉及,因为我想和这个分开,以突出那个的重要性,新IO哪一篇文章还没有开始写,估计很快就能和大家见面。照旧,文章依旧以例子为主,因为讲解内容的java书很多了,我觉的学以致用才是真。代码是写出来的,不是看出来的。
管道流
管道流主要可以进行两个线程之间的通信。 PipedOutputStream 管道输出流
PipedInputStream 管道输入流 验证管道流
? /**
? * 验证管道流 ? * */
? import java.io.*; ? ? /**
? * 消息发送类 ? * */
?
class Send implements Runnable{
?? private PipedOutputStream out=null; ?? public Send() {
?? out=new PipedOutputStream(); ?? }
?? public PipedOutputStream getOut(){ ?? return this.out; ?? }
?? public void run(){
?? String message=\?? try{
?? out.write(message.getBytes()); ?? }catch (Exception e) { ?? e.printStackTrace(); ?? }try{
AD:
?? out.close();
?? }catch (Exception e) { ?? e.printStackTrace(); ?? } ?? } ?? } ?? ?? /**
?? * 接受消息类 ?? * */
?? class Recive implements Runnable{ ?? private PipedInputStream input=null; ?? public Recive(){
?? this.input=new PipedInputStream(); ?? }
?? public PipedInputStream getInput(){ ?? return this.input; ?? }
?? public void run(){
?? byte[] b=new byte[1000]; ?? int len=0; ?? try{
?? len=this.input.read(b); ?? }catch (Exception e) { ?? e.printStackTrace(); ?? }try{
?? input.close(); ?? }catch (Exception e) { ?? e.printStackTrace(); ?? }
?? System.out.println(\接受的内容为 \new String(b,0,len))); ?? } ?? } ?? /** ?? * 测试类 ?? * */
?? class hello{
?? public static void main(String[] args) throws IOException { ?? Send send=new Send(); ?? Recive recive=new Recive(); ?? try{ ?? //管道连接
?? send.getOut().connect(recive.getInput()); ?? }catch (Exception e) {
?? e.printStackTrace(); ?? }
?? new Thread(send).start(); ?? new Thread(recive).start(); ?? } ?? }
【运行结果】:
接受的内容为 hello , Rollen
打印流
?? /**
?? * 使用PrintStream进行输出 ?? * */
?? import java.io.*; ??
?? class hello {
?? public static void main(String[] args) throws IOException { ?? PrintStream print = new PrintStream(new FileOutputStream(new File(\
?? + File.separator + \?? print.println(true); ?? print.println(\?? print.close(); ?? } ?? }
【运行结果】: true
Rollen
当然也可以格式化输出
?? /**
?? * 使用PrintStream进行输出 ?? * 并进行格式化 ?? * */
?? import java.io.*; ?? class hello {
?? public static void main(String[] args) throws IOException { ?? PrintStream print = new PrintStream(new FileOutputStream(new File(\
?? + File.separator + \?? String name=\ ?? int age=20;
?? print.printf(\姓名:%s. 年龄:%d.\ ??? print.close(); ??? } ??? }
【运行结果】:
姓名:Rollen. 年龄:20.
使用OutputStream向屏幕上输出内容
??? /**
??? * 使用OutputStream向屏幕上输出内容 ??? * */
??? import java.io.*; ??? class hello {
??? public static void main(String[] args) throws IOException { ??? OutputStream out=System.out; ??? try{
??? out.write(\??? }catch (Exception e) { ??? e.printStackTrace(); ??? } ??? try{
??? out.close();
??? }catch (Exception e) { ??? e.printStackTrace(); ??? } ??? } ??? }
【运行结果】: hello
输入输出重定向
??? import java.io.File;