1.内存操作流,ByteArrayInputStream和 ByteArrayOutputStream

案例:将小写转化为大写

/*
* 内存操作流,将大写字母转化为小写字母(ByteArrayInputStream和 ByteArrayOutputStream)
*/
public static void ChangeLowLetterToBigLetter() throws IOException{
String str="he who has a dream is a true man";
//直接 将字符串转化为字节数组,放到内存中
ByteArrayInputStream in=new ByteArrayInputStream(str.getBytes());
//out是直接取得内存中的字节数组
ByteArrayOutputStream out=new ByteArrayOutputStream();
int count=0;
int temp=0;
//从内存中一个一个的读字节
while((temp=in.read())!=(-1))
{
char ch=(char)temp;
//转化为大写,直接写到内存中
out.write(Character.toUpperCase(ch));
}
System.out.println(out.toString()); }

注意:这里一个ByteArrayInputStream对应一个ByteArryOutputStream ,通过每次读取一个字节的方式,能够很好的控制输入输出

内容操作流一般使用来生成一些临时信息采用的,这样可以避免删除的麻烦。

/**
* @param args
* 2.管道流 管道流主要可以进行两个线程之间的通信。
* PipedOutputStream 管道输出流 起点
* PipedInputStream 管道输入流 终点
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Send send=new Send();
Recive revice=new Recive(); try{
//管道接受流实例和管道发送流实例连接
revice.getIn().connect(send.getOut());
}
catch(Exception e)
{
e.printStackTrace();
}
//开启两个线程
new Thread(send).start();
new Thread(revice).start();
} } //发送管道流
class Send implements Runnable {
private PipedOutputStream out = null; public Send() {
this.out = new PipedOutputStream();
}
public PipedOutputStream getOut()
{
return this.out;
} @Override
public void run() {
// TODO Auto-generated method stub
String str=" he who has a dream is a true man";
try
{
out.write(str.getBytes());
}
catch(Exception ex)
{
ex.printStackTrace();
}
try{
out.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
} } /*
* 接受管道
*/
class Recive implements Runnable
{
private PipedInputStream in=null;
public Recive()
{
in =new PipedInputStream();
} public PipedInputStream getIn() {
return this.in;
}
@Override
public void run() {
// TODO Auto-generated method stub
try
{
int len;
byte[] all=new byte[1024];
len=in.read(all);
System.out.println("success:"+new String(all,0,len));
}
catch(Exception e)
{
e.printStackTrace();
}
} }

二.打印流

/*
*1 .打印流
* Printstream
* 直接打印到对应的文件,格式化输出
*/
public static void PrintStreamTest() throws IOException
{
String filePath="G:"+File.separator+"JAVA"+File.separator+"test"+File.separator+"aaa.txt";
PrintStream printStram=new PrintStream(filePath);
//下面的方法也能创建一个打印流实例
//PrintStream printS=new PrintStream(new FileOutputStream(filePath));
//直接打印到文件
printStram.println("nihaoma");
printStram.println("你妹没的");
//下面是格式化输出
String name="jackvin";
String age="12";
printStram.printf("name:%s \t age:%s",name,age); printStram.close(); }

结果:
nihaoma
你妹没的
name:jackvin   age:12

二:system.out,system.in,system.err 重定向

/*
* system.out 重定向,直接写文件
*/
public static void SystemOutReDirector() throws IOException
{
String filePath="G:"+File.separator+"JAVA"+File.separator+"test"+File.separator+"aaa.txt"; System.out.println("show in console");
File file=new File(filePath); PrintStream ps=new PrintStream(new FileOutputStream(file));
//将out定义成PrintStream,直接向文件中写入
System.setOut(ps);
//写文件
System.out.println("this sentence will show in the txt"); } /*
* system.err重定向,直接写文件
*/
public static void SysErrReDirector()
{
String filePath="G:"+File.separator+"JAVA"+File.separator+"test"+File.separator+"aaa.txt";
System.err.println("show in the console");
try
{
PrintStream ps =new PrintStream(filePath);
System.setErr(ps);
}
catch(Exception ex)
{
ex.printStackTrace();
}
System.err.println("show in the txt"); } /*
* system.in重定向,直接从文件中读取
*/
public static void SysInReadFromFile() {
String filePath="G:"+File.separator+"JAVA"+File.separator+"test"+File.separator+"bbb.txt";
try {
//定义一个输入流对象
InputStream in=new FileInputStream(filePath);
//修改system.in的读入方式
System.setIn(in);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
byte[] all=new byte[1024];
int count=0;
int temp=0;
try {
while((temp=System.in.read())!=(-1))
{
all[count++]=(byte)temp;
} } catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
System.out.println(new String(all,0,count)); }

三:Scanner类,直接读取文件,输出到console

/*
* Scanner类,直接读取文件输出到控制台
*/
public static void scannerTest() {
String filePath="G:"+File.separator+"JAVA"+File.separator+"test"+File.separator+"bbb.txt";
try {
Scanner scanner =new Scanner(new File(filePath));
while(scanner.hasNextLine())
{
System.out.println(scanner.nextLine());
} System.out.println("sfsf"); } catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} }

四:DataOutputStream 和DataInputStream

/*
* DataOutputStream 输出流
*/
public static void dataOutputStream()
{
String filePath="G:"+File.separator+"JAVA"+File.separator+"test"+File.separator+"bbb.txt";
try {
//定义一个DataOutputStream 对象
DataOutputStream out=new DataOutputStream(new FileOutputStream(filePath)); String str="hi,this is DataOutputStream write";
out.write(str.getBytes());
System.out.println("dataoutput sucess!"); } catch (Exception e) {
// TODO: handle exception
}
}
/*
* DataInputStream 输入流,将DataOutStream 输入到文件的内容,写到控制台
*/
public static void dataInputStreamTest() {
String filePath="G:"+File.separator+"JAVA"+File.separator+"test"+File.separator+"bbb.txt";
try {
DataInputStream in=new DataInputStream(new FileInputStream(filePath));
byte[] all=new byte[1024];
int count=0;
int temp=0; while((temp=in.read())!=(-1))
{
all[count++]=(byte)temp;
}
System.out.println(new String(all,0,count)); } catch (Exception e) {
// TODO: handle exception
}

五:合并流 SequenceInputStream

     /*
*合并流 SequenceInputStream
*/
public static void sequenceInputStreamTest()
{
String filePathA="G:"+File.separator+"JAVA"+File.separator+"test"+File.separator+"bbb.txt";
String filePathB="G:"+File.separator+"JAVA"+File.separator+"test"+File.separator+"aaa.txt";
try {
InputStream inA=new FileInputStream(filePathA);
InputStream inB=new FileInputStream(filePathB); SequenceInputStream sequenceInputStream=new SequenceInputStream(inA,inB);
byte [] all=new byte[1024];
int count=0;
int temp=0;
while((temp=sequenceInputStream.read())!=(-1))
{
all[count++]=(byte)temp;
} System.out.println(new String(all,0,count)); // sequenceInputStream.read(all); 如何不是一个一个字节的访问,输出的只是inB读入的文字
// System.out.println(new String(all));
inA.close();
inB.close(); } catch (Exception e) {
// TODO: handle exception
}
}

六:压缩http://snowolf.iteye.com/blog/465433

java IO其他流的更多相关文章

  1. Java Io 字符流

    Java Io 字符流包含: 1. InputStreamReader  它是由byte流解析为char流,并且按照给定的编码解析. 2. OutputStreamWrite  它是char流到byt ...

  2. Java IO 嵌套流、文本的输入输出和存储

    Java IO 嵌套流.文本的输入输出和存储 @author ixenos 1.   组合流过滤器(嵌套流) a)    跨平台文件分割符:常量字符串 java.io.File.seperator 等 ...

  3. Java IO 转换流 字节转字符流

    Java IO 转换流 字节转字符流 @author ixenos 字节流 输入字节流:---------| InputStream 所有输入字节流的基类. 抽象类.------------| Fil ...

  4. Java IO 过滤流 BufferedInput/OutputStream

    Java IO 过滤流 BufferedInput/OutputStream @author ixenos 概念 BufferedInput/OutputStream是实现缓存的过滤流,他们分别是Fi ...

  5. Java IO 节点流 FileInput/OutputStream

    Java IO 节点流 FileInput/OutputStream @author ixenos 节点流之 文件流 文件读写是最常见的I/O操作,通过文件流来连接磁盘文件,读写文件内容 1.文件的读 ...

  6. Java IO 理解流的概念

    Java IO 理解流的概念 @author ixenos 在理解流时首先理解以下概念 1.流的来源和去向一般在构造器指出 2.方法中的形参一般是将流输出到某个位置,读取(INPUT)流从流读出数据( ...

  7. Java IO 节点流 ByteArrayInput/OutputStream

    Java IO 节点流 ByteArrayInput/OutputStream @author ixenos ByteArrayInputStream 包含一个内部缓冲区(字节数组byte[]),该缓 ...

  8. Java IO: 字符流的Buffered和Filter

    作者: Jakob Jenkov  译者: 李璟(jlee381344197@gmail.com) 本章节将简要介绍缓冲与过滤相关的reader和writer,主要涉及BufferedReader.B ...

  9. Java IO: 字符流的Piped和CharArray

    作者: Jakob Jenkov 译者: 李璟(jlee381344197@gmail.com) 本章节将简要介绍管道与字符数组相关的reader和writer,主要涉及PipedReader.Pip ...

  10. Java IO包装流如何关闭?

      问题: (1)JAVA的IO流使用了装饰模式,关闭最外面的流的时候会自动调用被包装的流的close()方吗? (2)如果按顺序关闭流,是从内层流到外层流关闭还是从外层到内存关闭? 问题(1)解释: ...

随机推荐

  1. 一次hadoop集群机器加内存的运维过程

    由于前期的集群规划问题,导致当前Hadoop集群中的硬件并没有完全利用起来.当前机器的内存CPU比例为2G:1core,但一般的MapReduce任务(数据量处理比较大,逻辑较复杂)的MR两端都需要将 ...

  2. 01:Sysbench 基准压测 IO篇

    line:V1.1 mail: gczheng@139.com date: 2017-11-17 一.Sysench测试前准备 1.1.压测环境 配置 信息 主机 Dell PowerEdge R73 ...

  3. questions information

    1. 面向对象 类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 三大特性: 封装: -- 将内容封装到对象中 -- 将方法疯 ...

  4. 【BZOJ】1926: [Sdoi2010]粟粟的书架(暴力+主席树)

    题目 传送门:QWQ 分析 两道题目 第一问暴力预处理 用$ a[i][j][k] $和$ s[i][j][k] $ 表示从$ (1,1) $ 到 $ (i,j) $ 这个矩形中比k大的数的个数和这些 ...

  5. verilog 之数字电路 边沿检测电路

    由代码可知:此边沿检测电路是由两个触发器级联而成,sign_c_r 输出是sign_c_r2的输入.并且有异步复位端没有使能端.最后输出:由触发器的输出取反和直接输出相与.如下的RTL图.

  6. Web端优秀图表控件

    百度echart http://echarts.coding.io/doc/example.html C#+JQuery+.Ashx+百度Echarts 实现全国省市地图和饼状图动态数据图形报表的统计 ...

  7. 3_python之路之商城购物车

    python之路之商城购物车 1.程序说明:Readme.txt 1.程序文件:storeapp_new.py userinfo.py 2.程序文件说明:storeapp_new.py-主程序 use ...

  8. http请求头设置

    Curl curl -i -H 'Content-Type: application/json' -i/--include     输出时包括protocol头信息curl -x 10.12.15.1 ...

  9. 使用被动混合内容的方式来跨越浏览器会阻断HTTPS上的非安全请求(HTTP)请求的安全策略抓包详解

    /*通过传入loginId在token中附加loginId参数,方便后续读取指定缓存中的指定用户信息*/ GET /multitalk/takePhone.php?loginId=4edc153568 ...

  10. malloc()与calloc区别 (转)

    另外说明: 1.分配内存空间函数malloc 调用形式: (类型说明符*) malloc (size) 功能:在内存的动态存储区中分配一块长度为"size" 字节的连续区域.函数的 ...