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的更多相关文章

  1. java io读书笔记(2)什么是stream

    什么是stream?stream就是一个长度不确定的有序字节序列. Input streams move bytes of data into a Java program from some gen ...

  2. java.io.IOException: Stream closed

    今天在做SSH项目的时候,出现了这个错误.百思不得其解,网上的答案都不能解决我的问题-.. 后来,一气之下就重新写,写了之后发现在JSP遍历集合的时候出错了. <s:iterator value ...

  3. Java笔记:Java 流(Stream)、文件(File)和IO

    更新时间:2018-1-7 12:27:21 更多请查看在线文集:http://android.52fhy.com/java/index.html java.io 包几乎包含了所有操作输入.输出需要的 ...

  4. java.io.IOException: Attempted read from closed stream

    前言: 代码如下,执行的时候提示"java.io.IOException: Attempted read from closed stream." public static JS ...

  5. Java - 17 Java 流(Stream)、文件(File)和IO

    Java 流(Stream).文件(File)和IO Java.io包几乎包含了所有操作输入.输出需要的类.所有这些流类代表了输入源和输出目标. Java.io包中的流支持很多种格式,比如:基本类型. ...

  6. java.io.IOException: Stream closed解决办法

    1.出现这个bug的大体逻辑代码如下: private static void findMovieId() throws Exception { File resultFile = new File( ...

  7. java.io.StreamCorruptedException: invalid stream header: EFBFBDEF 问题解决

    错误方式 @Test public void testDeserializeTest() throws IOException, ClassNotFoundException { ByteArrayO ...

  8. java.io.IOException: Attempted read from closed stream解决

    在HttpClient请求的时候,返回结果解析时出现java.io.IOException: Attempted read from closed stream. 异常,解决 原因是EntityUti ...

  9. 【Java】IO Stream详细解读

    成鹏致远 | 2013年12月31日 什么是IO Java中I/O操作主要是指使用Java进行输入,输出操作. Java所有的I/O机制都是基于数据流进行输入输出,这些数据流表示了字符或者字节数据的流 ...

随机推荐

  1. struts2 基本用法

    Struts2必需库: commons-fileupload.jar.commons-io-1.3.2.jar.freemarker-2.3.16.jar.javassist-3.7.ga.jar.o ...

  2. LeetCode题解-----First Missing Positive

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  3. 边工作边刷题:70天一遍leetcode: day 88-5

    coins in a line I/II/III: check above 1. recursion的返回和dp[left][right]表示什么?假设game是[left,right],那么play ...

  4. POJ 2318 TOYS【叉积+二分】

    今天开始学习计算几何,百度了两篇文章,与君共勉! 计算几何入门题推荐 计算几何基础知识 题意:有一个盒子,被n块木板分成n+1个区域,每个木板从左到右出现,并且不交叉. 有m个玩具(可以看成点)放在这 ...

  5. Android配置----Android开发环境搭建

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/3 ...

  6. Unity3D 多平台 预编译 宏定义

    平台定义 UNITY_EDITOR 编辑器调用. UNITY_STANDALONE_OSX 专门为Mac OS(包括Universal,PPC和Intelarchitectures)平台的定义. UN ...

  7. 查询各个商品分类中各有多少商品的SQL语句

    SELECT goods_category_id ,count(*) FROM `sw_goods` group by goods_category_id

  8. 正则基础之——环视(Lookaround)

    环视基础 环视只进行子表达式的匹配,不占有字符,匹配到的内容不保存到最终的匹配结果,是零宽度的.环视匹配的最终结果就是一个位置. 环视的作用相当于对所在位置加了一个附加条件,只有满足这个条件,环视子表 ...

  9. 013医疗项目-模块一:加入工具类ResultUtil

    这篇文章要做的就是优化,封装.把之前的代码尽量封装进类,并且不要硬编码. 在UserServiceimpl中的insertSysuser()函数之前是这么写的: ResultInfo resultIn ...

  10. Docker / CI / CD

    CI Weekly #6 | 再谈 Docker / CI / CD 实践经验   CI Weekly 围绕『 软件工程效率提升』 进行一系列技术内容分享,包括国内外持续集成.持续交付,持续部署.自动 ...