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机制都是基于数据流进行输入输出,这些数据流表示了字符或者字节数据的流 ...
随机推荐
- CUDA入门1
1GPUs can handle thousands of concurrent threads. 2The pieces of code running on the gpu are calle ...
- Codeforces 402B --耻辱的一题
这题昨天晚上花了我1个小时50多分钟来搞,都没有搞定..后来看别人代码,直接暴力枚举第一个数的值来做..最多1000*1000的复杂度.当时怎么就没想到呢?还有为啥我的方法不对呢.. 暴力方法代码: ...
- 分层开发(MySchool总结)
由于分层之间存在各层之间的关系窗体之间的方法跳转,故有需要者可以进行下载本地文件 MySchool.rar 3304KB 5/22/2016 9:43:28 AM ,代码中有注释, 上述代码,属个人所 ...
- 解决Gradle DSL method not found: ‘android()’
最近导入as的项目出了这样的问题 这个问题困扰了我很长时间,好吧,搜了半天全都是runProguard的,最后在stackoverflow上搜到解决办法了: http://stackoverflow. ...
- Android Studio系列教程五--Gradle命令详解与导入第三方包
Android Studio系列教程五--Gradle命令详解与导入第三方包 2015 年 01 月 05 日 DevTools 本文为个人原创,欢迎转载,但请务必在明显位置注明出处!http://s ...
- 第三方登录之qq登录(转载)
iOS QQ第三方登实现 我们经常会见到应用登陆的时候会有QQ,微信,微博等的第三方登陆 如图: 下面我们主要讲一下qq的第三方登陆如何实现 首先,到官网注册: http://wiki.conne ...
- Java NIO 概述
Channel 和 Buffer 标准的Java IO编程接口是面向字节流和字符流的 而 NIO 是面向通道和缓冲区的 数据总是从通道中读到Buffer中,或者从Buffer写入通道中 NIO可以理解 ...
- 挂多个class还是新建class —— 多用组合,少用继承
用css实现下面的效果图. 方案一 <style type="text/css"> .myList1 { border: 1px solid #333; padding ...
- HMAC-MD5算法原理及实现
以下是分析节选,对于更详细的描述可以查阅RFC2104文档. HMAC需要一个加密用散列函数(表示为H)和一个密钥K. 假设H是一个将数据块用一个基本的迭代压缩函数来加密的散列函数. 用B来表 ...
- Android中asset文件夹和raw文件夹区别
res/raw和assets的相同点: 1.两者目录下的文件在打包后会原封不动的保存在apk包中,不会被编译成二进制. res/raw和assets的不同点: 1.res/raw中的文件会被映射到R. ...