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机制都是基于数据流进行输入输出,这些数据流表示了字符或者字节数据的流 ...
随机推荐
- CentOS7.2安装总结
第一次自己写文章,想想还有点小激动呢.折腾了大半天,终于在一个没用的台式机上面装了个mini版的CentOS7.2.写这篇文章也是做个记载,要是以后再装要注意了. 一.安装过程 采用U盘安装.最初是准 ...
- POJ 2407 Relatives 【欧拉函数】
裸欧拉函数. #include<stdio.h> #include<string.h> ; int p[N],pr[N],cnt; void init(){ ;i<N;i ...
- 安全框架 - Shiro与springMVC整合的注解以及JSP标签
Shiro想必大家都知道了,之前的文章我也有提过,是目前使用率要比spring security都要多的一个权限框架,本身spring自己都在用shiro,之前的文章有兴趣可以去扒一下 最近正好用到s ...
- 关闭log4j 输出 DEBUG org.apache.commons.beanutils.*
2016-03-23 10:52:26,860 DEBUG org.apache.commons.beanutils.MethodUtils - Matching name=getEPort on c ...
- TIF、JPG图片手动添加地理坐标的方法(转载)
题目:为TIF.JPG图片添加地理坐标/平面直角坐标. 图片来源:GOOGLE EARTH.(当然也可以是其他知道四角点坐标的图片) 截图工具:GEtscreen(此软件截图时可以自动生成图片四角点坐 ...
- 1017. A除以B (20)
本题要求计算A/B,其中A是不超过1000位的正整数,B是1位正整数.你需要输出商数Q和余数R,使得A = B * Q + R成立. 输入格式: 输入在1行中依次给出A和B,中间以1空格分隔. 输出格 ...
- 爱奇艺招聘uwp开发
招聘链接:https://job.cnblogs.com/offer/53380/ 工作地点:北京-海淀 工作年限:1年 学历要求:本科 招聘分类:移动开发工程师 工资范围:面议 职位要求 1.扎实的 ...
- [Bug]redis问题解决(MISCONF Redis is configured to save RDB snapshots)
redis问题解决(MISCONF Redis is configured to save RDB snapshots) (error) MISCONF Redis is configured t ...
- &10 基本数据结构——指针和对象的实现,有根树的表示
#1,指针和对象的实现 如果所用的语言或者环境不支持指针和对象,那我们该怎么用数组来将其转化呢?实质上可以将这个问题的本质转化为数组和链表这两种数据结构的转换,准确来说,是将链表表示的数据用数组表示. ...
- python 转 exe -- py2exe库实录
本文基于windows 7 + python 3.4 把python程序打包成exe,比较好用的库是py2exe 其操作步骤是: --> 编写python程序 --> 再额外编写一个导入了 ...