转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/54292148
本文出自【赵彦军的博客】
流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作。
字符流的由来: 因为数据编码的不同,而有了对字符进行高效操作的流对象。本质其实就是基于字节流读取时,去查了指定的码表。 字节流和字符流的区别:
设备上的数据无论是图片或者视频,文字,它们都以二进制存储的。二进制的最终都是以一个8位为数据单元进行体现,所以计算机中的最小数据单元就是字节。意味着,字节流可以处理设备上的所有数据,所以字节流一样可以处理字符数据。
结论:只要是处理纯文本数据,就优先考虑使用字符流。 除此之外都使用字节流。
输入流只能进行读操作,输出流只能进行写操作,程序中需要根据待传输数据的不同特性而使用不同的流。
总结:
节点流:直接与数据源相连,读入或读出。
直接使用节点流,读写不方便,为了更快的读写文件,才有了处理流。
处理流和节点流一块使用,在节点流的基础上,再套接一层,套接在节点流上的就是处理流。如BufferedReader.处理流的构造方法总是要带一个其他的流对象做参数。一个流对象经过其他流的多次包装,称为流的链接。
InputStreamReader 、OutputStreamWriter 要InputStream或OutputStream作为参数,实现从字节流到字符流的转换。
构造函数
InputStreamReader(InputStream); //通过构造函数初始化,使用的是本系统默认的编码表GBK。
InputStreamReader(InputStream,String charSet); //通过该构造函数初始化,可以指定编码表。
OutputStreamWriter(OutputStream); //通过该构造函数初始化,使用的是本系统默认的编码表GBK。
OutputStreamwriter(OutputStream,String charSet); //通过该构造函数初始化,可以指定编码表。
package com.app;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class A1 {
public static void main(String[] args) {
A1 a1 = new A1();
//电脑d盘中的abc.txt 文档
String filePath = "D:/abc.txt" ;
String reslut = a1.readFile( filePath ) ;
System.out.println( reslut );
}
/**
* 读取指定文件的内容
* @param filePath : 文件的路径
* @return 返回的结果
*/
public String readFile( String filePath ){
FileInputStream fis=null;
String result = "" ;
try {
// 根据path路径实例化一个输入流的对象
fis = new FileInputStream( filePath );
//2. 返回这个输入流中可以被读的剩下的bytes字节的估计值;
int size = fis.available() ;
//3. 根据输入流中的字节数创建byte数组;
byte[] array = new byte[size];
//4.把数据读取到数组中;
fis.read( array ) ;
//5.根据获取到的Byte数组新建一个字符串,然后输出;
result = new String(array);
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
if ( fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result ;
}
}
package com.app;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class A2 {
public static void main(String[] args) {
A2 a2 = new A2();
//电脑d盘中的abc.txt 文档
String filePath = "D:/abc.txt" ;
//要写入的内容
String content = "今天是2017/1/9,天气很好" ;
a2.writeFile( filePath , content ) ;
}
/**
* 根据文件路径创建输出流
* @param filePath : 文件的路径
* @param content : 需要写入的内容
*/
public void writeFile( String filePath , String content ){
FileOutputStream fos = null ;
try {
//1、根据文件路径创建输出流
fos = new FileOutputStream( filePath );
//2、把string转换为byte数组;
byte[] array = content.getBytes() ;
//3、把byte数组输出;
fos.write( array );
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
if ( fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
注意:
效果图:
package com.app;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class A3 {
public static void main(String[] args) {
A3 a2 = new A3();
//电脑d盘中的cat.png 图片的路径
String filePath1 = "D:/cat.png" ;
//电脑e盘中的cat.png 图片的路径
String filePath2 = "E:/cat.png" ;
//复制文件
a2.copyFile( filePath1 , filePath2 );
}
/**
* 文件复制
* @param filePath_old : 需要复制文件的路径
* @param filePath_new : 复制文件存放的路径
*/
public void copyFile( String filePath_old , String filePath_new){
FileInputStream fis=null ;
FileOutputStream fout = null ;
try {
// 根据path路径实例化一个输入流的对象
fis = new FileInputStream( filePath_old );
//2. 返回这个输入流中可以被读的剩下的bytes字节的估计值;
int size = fis.available() ;
//3. 根据输入流中的字节数创建byte数组;
byte[] array = new byte[size];
//4.把数据读取到数组中;
fis.read( array ) ;
//5、根据文件路径创建输出流
fout = new FileOutputStream( filePath_new ) ;
//5、把byte数组输出;
fout.write( array );
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
if ( fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if ( fout != null ) {
try {
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}