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

最新文章

  1. 集合3--毕向东java基础教程视频学习笔记
  2. Python’s SQLAlchemy vs Other ORMs[转发 6]SQLAlchemy
  3. Java的加密与解密
  4. Maven Source jar
  5. ibatis 开发中的经验 (一)ibatis 和hibernate 在开发中的理解
  6. 20个 Unix/Linux 命令技巧
  7. 解决在IE浏览器下 boder边框出现断裂或虚线的问题
  8. DSP连接不上CCS3.3的问题讨论
  9. play framework2.5.
  10. c#中 uint--byte[]--char[]--string相互转换汇总
  11. hibernate里的generator中class =value介绍
  12. Python3基础 定义有参数有返回值函数 对传入的参数加1
  13. ios-Ineligible Devices 不被识别的设备
  14. 与我们息息相关的internet服务(1)---域名服务
  15. Java技术分享:如何编写servlet程序
  16. [AI开发]将深度学习技术应用到实际项目
  17. re 模块 分组特别说明
  18. python基础—购物车小程序练习
  19. css,响应鼠标事件,文字变色
  20. [转载]linux下core文件设置与查看

热门文章

  1. 安装Elastix-2.4版本
  2. 使用wifi网卡笔记1----网卡选型、开发环境搭建、内核配置
  3. 将 .NET 任务作为 WinRT 异步操作公开
  4. array,vector对象 数组越界检测
  5. [Z]图灵奖获得者Richard Karp讲述Berkeley CS的发展史
  6. 【Oracle】Oracle 10g利用闪回挽救误删的数据
  7. 「小程序JAVA实战」Springboot版mybatis逆向生成工具(32)
  8. 关于BigDecimal类型在jsp页面中进行除法运算问题
  9. Asp.Net 自定义 httpmodel 中间件 管道
  10. jq 遍历 each方法