------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! ------
## InputStream,OutputStream ##
流的四个基类(也就是父类)前面的课程已经讲过,Reader,Writer,字符流,今天学习 InputStream , OutputStream。大家都知道字符流用来读取文本文件,他们的区别,用字节流来读取图片,Writer里面需要flush()或者close()方法刷进去,而OutputStream则不需要,照样能写入文件。
package com.itheima.luoxiaofei;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
*
* 关于字节流的操作
* @author Administrator
*
*字节流不同于字符流的是不用flush()或者close()他都能把内容写入
*/
public class InputStreamluo {
public static void main(String[] args) {
StreamOutput();
StreamInput();
StreamInput_2();
}
//第二种读取方法
private static void StreamInput_2() {
// TODO Auto-generated method stub
FileInputStream fis=null;
try {
fis=new FileInputStream("fos.txt");
try {
//获取文件的长度
int len = fis.available();
System.out.println("len="+len+"个字节");
//得到一个知道长度的byte数组
byte [] bt=new byte[len];
//读到byte数组里面
fis.read(bt);
//把数组转换成字符串
System.out.println(new String(bt));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(fis!=null)
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//第一种读取方法
private static void StreamInput() {
// TODO Auto-generated method stub
//最原始的字节流读取方式
FileInputStream fis=null;
try {
fis=new FileInputStream("fos.txt");
//字节流的读肯定是字节数组】
byte [] bt=new byte[1024];
int len=-1;
try {
while((len=fis.read(bt))!=-1){
System.out.println(new String(bt,0,len));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(fis!=null)
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//用字节流写入文件
public static void StreamOutput(){
//字节流写文件
FileOutputStream fos=null;
try {
fos=new FileOutputStream("fos.txt");
try {
//开始写入内容
fos.write("天赋异丙".getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(fos!=null)
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
用字节流Copy图片
字符流子用来处理文字数据(因为要查编码表),用字节流处理图片数据。。
package com.itheima.luoxiaofei;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
*
* 用字节流拷贝图片
*
* @author MyLuo
*
*/
public class ImageInputStream {
public static void main(String[] args) {
copyImage();
}
private static void copyImage() {
// TODO Auto-generated method stub
FileOutputStream fos=null;
FileInputStream fis=null;
try {
//copy到
fos=new FileOutputStream(" psb.jpg");
//读取图片
fis=new FileInputStream("C:\\Users\\Administrator\\Desktop\\psb.jpg");
byte [] bt= new byte[1024];
int len=-1;
try {
while((len=fis.read(bt))!=-1){
fos.write(bt, 0, len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}finally{
if(fis!=null)
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(fos!=null)
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
----------
## 用BufferedInputStream,BufferedOutputStream ,CopyMP3文件 ##
> 字符流的升级版
package com.itheima.luoxiaofei;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 用字节缓冲流 copy mp3文件
*
* @author MyLuo
*
*/
public class CopyMP3 {
public static void main(String[] args) {
long start = System.currentTimeMillis();
CopyMp3Method();
long end=System.currentTimeMillis();
System.out.println("copy星星点灯—Myluo.mp3用来"+(end-start)+"毫秒");
}
private static void CopyMp3Method() {
// TODO Auto-generated method stub
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try {
bis=new BufferedInputStream(
new FileInputStream("C:\\KuGou\\星星点灯—Myluo.mp3"));
bos=new BufferedOutputStream(new FileOutputStream("D:\\星星点灯—Myluo.mp3"));
int bt=0;
try {
while((bt=bis.read())!=-1){
bos.write(bt);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(bis!=null)
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(bos!=null)
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
“`
一直被模仿,从未被超越。