java.io.stream
1. package com.io.Stream;
import java.io.*;
public class NyFileInputStream1 { /**
* 读取文件的streamIO
*
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new NyFileInputStream1();
}
public NyFileInputStream1(){
InputStream fis=null;
FileOutputStream fos=null;
try {
fis=new FileInputStream(new File("D:\\Zh.java"));
//fis=new ByteArrayInputStream("wo hao 周海".getBytes());
fos=new FileOutputStream(new File("D:\\Zhouhai.txt"),true);
int len=0;
byte bytes[]=new byte[1024];
while( (len=fis.read(bytes))!=-1){
fos.write(bytes,0,len);
System.out.println(len);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fos.close();
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} } 2. package com.io.Stream; import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
/**向管道输出流 写数据的线程
* @param args
*/
public class PipedSender extends Thread{ private PipedOutputStream out=new PipedOutputStream();
public PipedOutputStream getPipedOutputStream()
{
return out;
} public void run(){
try {
for(int i=-127;i<=128;i++){
out.write(i);
this.yield();
}
} catch (Exception e) {
// TODO: handle exception
}finally{
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} public static void main(String[] args) {
// TODO Auto-generated method stub
PipedSender pipedSender=new PipedSender();
PipedReceiver pipedReceiver=new PipedReceiver(pipedSender);
pipedSender.start();
pipedReceiver.start();
}
} //从管道输入流 读取数据的线程 class PipedReceiver extends Thread{
private PipedInputStream in;
public PipedReceiver(PipedSender pipedSender){
try {
in=new PipedInputStream(pipedSender.getPipedOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public void run(){
try{
int data;
while((data=in.read()) != -1){
System.out.println(data);
}
in.close();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
} 3. package com.io.Stream; import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream; public class Stream { /**
* 怎么向文件中写Strting 又读成String
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new Stream();
} public Stream(){
try {
FileOutputStream out=new FileOutputStream("D:/Stream.txt",true);
//读取我要的String
ByteArrayInputStream in=new ByteArrayInputStream("zhou hai周省劲".getBytes());
int len=in.available(); //获取所有的字节数目
byte[] bytes=new byte[len];
in.read(bytes); //把输入流的写bytes数组中 out.write(bytes, 0, len);
out.close();
in.close(); FileInputStream fis=new FileInputStream("D:/Stream.txt");
int len2=fis.available();
byte byin[]=new byte[len2];
fis.read(byin);
System.out.println(new String(byin)); fis.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} } 4. package com.io.Stream;
import java.io.*;
public class ByteArrayOutputStreamTester { /**
* byteArrayOutputStream的运用 //字节数据输出流
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ByteArrayOutputStream out=new ByteArrayOutputStream();
try {
out.write("周海".getBytes("utf-8")); //用uft-8的编码方式写到数组中 byte[] buff=out.toByteArray(); //获取字节数组
out.close(); ByteArrayInputStream in=new ByteArrayInputStream(buff);
int len=in.available();
byte[] buffin=new byte[len];
in.read(buffin); //把 buff字节数组的数据读入到 buffiin中
in.close();
System.out.println(new String(buffin,"utf-8")); } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } } 5. package com.io.Stream; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream; public class FormatDateIO { /**
* 可以读取基本的数据类型的IO
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
FileOutputStream out1=new FileOutputStream("D:/Zhou.txt");
BufferedOutputStream out2=new BufferedOutputStream(out1);
DataOutputStream out=new DataOutputStream(out2); out.writeByte(-12);
out.writeLong(12);
out.writeChar('1');
out.writeUTF("周"); out.close(); InputStream in1=new FileInputStream("D:/Zhou.txt");
BufferedInputStream in2=new BufferedInputStream(in1);
DataInputStream in=new DataInputStream(in2);
System.out.println(in.read() +" "+in.readLong()+" "+in.readChar()+" " +in.readUTF()); in.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } }
java.io.stream的更多相关文章
- java io读书笔记(2)什么是stream
什么是stream?stream就是一个长度不确定的有序字节序列. Input streams move bytes of data into a Java program from some gen ...
- java.io.IOException: Stream closed
今天在做SSH项目的时候,出现了这个错误.百思不得其解,网上的答案都不能解决我的问题-.. 后来,一气之下就重新写,写了之后发现在JSP遍历集合的时候出错了. <s:iterator value ...
- Java笔记:Java 流(Stream)、文件(File)和IO
更新时间:2018-1-7 12:27:21 更多请查看在线文集:http://android.52fhy.com/java/index.html java.io 包几乎包含了所有操作输入.输出需要的 ...
- java.io.IOException: Attempted read from closed stream
前言: 代码如下,执行的时候提示"java.io.IOException: Attempted read from closed stream." public static JS ...
- Java - 17 Java 流(Stream)、文件(File)和IO
Java 流(Stream).文件(File)和IO Java.io包几乎包含了所有操作输入.输出需要的类.所有这些流类代表了输入源和输出目标. Java.io包中的流支持很多种格式,比如:基本类型. ...
- java.io.IOException: Stream closed解决办法
1.出现这个bug的大体逻辑代码如下: private static void findMovieId() throws Exception { File resultFile = new File( ...
- java.io.StreamCorruptedException: invalid stream header: EFBFBDEF 问题解决
错误方式 @Test public void testDeserializeTest() throws IOException, ClassNotFoundException { ByteArrayO ...
- java.io.IOException: Attempted read from closed stream解决
在HttpClient请求的时候,返回结果解析时出现java.io.IOException: Attempted read from closed stream. 异常,解决 原因是EntityUti ...
- 【Java】IO Stream详细解读
成鹏致远 | 2013年12月31日 什么是IO Java中I/O操作主要是指使用Java进行输入,输出操作. Java所有的I/O机制都是基于数据流进行输入输出,这些数据流表示了字符或者字节数据的流 ...
随机推荐
- [转]MySQL数据库的优化-运维架构师必会高薪技能,笔者近六年来一线城市工作实战经验
本文转自:http://liangweilinux.blog.51cto.com/8340258/1728131 年,嘿,废话不多说,下面开启MySQL优化之旅! 我们究竟应该如何对MySQL数据库进 ...
- CSS3 圆角(border-radius)详解
在做网页的时候,常常需要实现圆角,以前的做法就是切图,现在好了,有了css3的 border-radius 特性之后,实现边框圆角效果就非常简单了,而且其还有多个优点:一是减少网站维护工作量:二是提高 ...
- 台北Unity开发者研讨会 笔记
本文转自:http://ndark.wordpress.com/2013/05/12/20130511-台北unity开发者研讨会-笔记/ (墙外) 说明 本文单纯只是笔记,若有笔误敬请见谅. 相关参 ...
- IL查看委托
查看委托的IL 通过IL来查看委托的原理, 委托示例代码 写一个委托的类如下 using System; namespace MyCollection { //定义一个类,该类包含两个静态方法 c ...
- SQL Server 2005 安装图解教程(Windows)
因工作需要,好久未安装SQL Server2005,今天安装了一下,特此写下安装步骤留下笔记. 安装前准备: 先安装IIS,再安装SQL Server2005 一.安装 点击安装,如下图: 选择操作系 ...
- Android事件分发机制完全解析,带你从源码的角度彻底理解
Android事件构成 在Android中,事件主要包括点按.长按.拖拽.滑动等,点按又包括单击和双击,另外还包括单指操作和多指操作.所有这些都构成了Android中的事件响应.总的来说,所有的事件都 ...
- Javascript中call和apply的区别与详解
在js中call和apply它们的作用都是将函数绑定到另外一个对象上去运行,两者仅在定义参数方式有所区别,下面我来给大家介绍一下call和apply用法: 在web前端开发过程中,我们经常需要改变th ...
- 介绍linux下利用编译bash设置root账号共用的权限审计设置
在日常运维工作中,公司不同人员(一般是运维人员)共用root账号登录linux服务器进行维护管理,在不健全的账户权限审计制度下,一旦出现问题,就很难找出源头,甚是麻烦!在此,介绍下利用编译bash使不 ...
- 基于Nodejs生态圈的TypeScript+React开发入门教程
基于Nodejs生态圈的TypeScript+React开发入门教程 概述 本教程旨在为基于Nodejs npm生态圈的前端程序开发提供入门讲解. Nodejs是什么 Nodejs是一个高性能Ja ...
- JS面向对象的几种写法
JS 中,面向对象有几种写法.归纳下,大概有下面这几种:工厂模式,构造函数模式,原型模式,构造函数与原型模式的混合使用,原型链继承,借用构造函数继承. 一.工厂模式 function person ( ...