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. AC日记——斗地主(dfs)

    题目描述 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的A到K加上大小王的共54张牌来进行的扑克牌游戏.在斗地主中,牌的大小关系根据牌的数码表示如下:3<4< ...

  2. 二维背包(钟神想要的)(不是DP)

    [问题描述] 背包是个好东西,希望我也有.给你一个二维的背包,它的体积是? × ?.现在你有一些大小为1× 2和1×3的物品,每个物品有自己的价值.你希望往背包里面装一些物品,使得它们的价值和最大,问 ...

  3. Doxygen Tool For Unity

    一.准备阶段 在之前的一系列文章中,我尝试了不同方法为Unity的C#生成脚本手册(帮助文档) 使用Doxygen生成C#帮助文档 为Unity项目生成文档(一)为Unity项目生成文档(二) 建议的 ...

  4. Android性能测试工具Emmagee介绍

    Emmagee介绍 Emmagee是监控指定被测应用在使用过程中占用机器的CPU.内存.流量资源的性能测试小工具.该工具的优势在于如同windows系统性能监视器类似,它提供的是数据采集的功能,而行为 ...

  5. jQuery Ajax 操作函数及deferred对象

    jQuery Ajax 操作函数 jQuery 库拥有完整的 Ajax 兼容套件.其中的函数和方法允许我们在不刷新浏览器的情况下从服务器加载数据. 函数 描述 jQuery.ajax() 执行异步 H ...

  6. WP老杨解迷:如何获得更多的应用评价和解读内容刷新

    Windows Phone的市场评论功能研究的时间比较长,只是这一功能,估计就能写一篇论文,我曾搞过多款评论数超高的游戏,其中<少林塔防>是重量级的作品,至今稳坐最高评分第一把交椅,如果不 ...

  7. [资料]Nginx做IP访问限制以及正则规则

    nginx配置location总结及rewrite规则写法 Nginx Location配置总结 Nginx 禁止某个IP访问 server { listen 443; root /webroot/; ...

  8. Oracle数据库,数字强制显示2位小数(转)

    Oracle数据库,数字强制显示2位小数 在银行.财务等对数字要求敏感的系统中,数字的显示一般有着严格的要求.今遇到一个需求,如题,要求将数字以两位小数的格式显示,如果没有小数,则强制显示为0.例如: ...

  9. python实现概率分布

    1. 二项分布(离散) import numpy as np from scipy import stats import matplotlib.pyplot as plt ''' # 二项分布 (b ...

  10. [CareerCup] 12.3 Test Move Method in a Chess Game 测试象棋游戏中的移动方法

    12.3 We have the following method used in a chess game: boolean canMoveTo( int x, int y). This metho ...