77范文网 - 专业文章范例文档资料分享平台

Java中IO整理完整版(5)

来源:网络收集 时间:2019-04-02 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

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;

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库Java中IO整理完整版(5)在线全文阅读。

Java中IO整理完整版(5).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/566030.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: