1,流操作四个大类;
字节 byte
字符 char
字符 到字节 是编码 ,字节到字符 是解码;
有四个类 是主要的api ;
InputStream ,OutputStream,Reader,Writer;
InputStream 字节输入 int read(); void close();
OutputStream 字节输出 void read(int); void close(); void flush();
Reader 字符输入 int read(); void close();
Writer 字符输出 void read(int); void close(); void flush();
2,流的操作步骤 创建源=》选择流=》 操作=》 释放资源
3,输入流 单个字节demo
package y.i.d;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class InputTest {
public static void main(String[] args) {
// 1,创建源头
File src = new File("/Users/wangrong/eclipse-workspace/Diy/src/aaa.txt");
//2, 选择流
InputStream iStream =null;
try {
iStream = new FileInputStream(src);
// System.out.println((char)iStream.read());
// System.out.println((char)iStream.read());
// System.out.println((char)iStream.read());
// System.out.println((char)iStream.read());
// System.out.println((char)iStream.read());
// System.out.println((char)iStream.read());
// System.out.println((char)iStream.read());
// System.out.println((char)iStream.read());
// 3, 操作
int temp; // 如果不赋值 读取顺序 会相隔一个取值 的错误
while((temp=iStream.read())!=-1) { // -1 没有值
System.out.println((char)temp);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(null!=iStream) {
iStream.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
4,输入流 多个字节一组 demo
package y.i.d;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
/**
*
* 利用缓冲区来做一组字节的读取, 但是为了防止数据贮存在内存中不出来 ,需要经常flush;
*
*
* */
public class InputTest {
public static void main(String[] args) {
// 1,创建源头
File src = new File("/Users/wangrong/eclipse-workspace/Diy/src/aaa.txt");
//2, 选择流
InputStream iStream =null;
try {
iStream = new FileInputStream(src);
// 3, 操作
byte[] flush =new byte[2];// 字节数组 长度为2
int len;
while((len=iStream.read(flush))!=-1) { //每次都存2个
// System.out.println(len);
String str=new String(flush,0,len);
System.out.println(str);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally {
try {
//4. 释放资源
if(null!=iStream) {
iStream.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
5, outputStream 输出流 demo
package y.i.d;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
*
* 利用缓冲区来做一组字节的读取, 但是为了防止数据贮存在内存中不出来 ,需要经常flush;
*
*
* */
public class InputTest {
public static void main(String[] args) {
// 1,创建源头
// 如果 是输出流 配合的话 new file会自己创建文件
File src = new File("/Users/wangrong/eclipse-workspace/Diy/src/bbb.txt");
//2, 选择流
OutputStream oStream =null;
try {
oStream = new FileOutputStream(src,true); // true 是开启了append拼接
String msgString ="hello world";
// 3, 操作
byte[] datas =msgString.getBytes(); // 得到字节集合;
oStream.write(datas,0,datas.length);
oStream.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally {
try {
//4. 释放资源
if(null!=oStream) {
oStream.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
6,文件的拷贝
package y.i.d;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
*
* 利用缓冲区来做一组字节的读取, 但是为了防止数据贮存在内存中不出来 ,需要经常flush;
*
*
* */
public class InputTest {
public static void main(String[] args) {
copyFile("/Users/wangrong/eclipse-workspace/Diy/src/cccc.txt",
"/Users/wangrong/eclipse-workspace/Diy/src/bbb.txt");
}
public static void copyFile(String outputSrc,String inputSrc) {
// 1,创建源头 和 目的地
File dest= new File(outputSrc);
File src = new File(inputSrc);
//2, 选择流
OutputStream oStream =null;
InputStream iStream =null;
try {
iStream = new FileInputStream(src);
oStream = new FileOutputStream(dest);
// 3, 操作
byte[] flush =new byte[2]; // 得到字节集合;
int len;
while ((len=iStream.read(flush))!=-1) {
oStream.write(flush,0,len);
}
oStream.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally {
//4. 释放资源 先打开的后关闭
try {
if(null!=oStream) {
oStream.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(null!=iStream) {
iStream.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
7, 以上的 是字节的处理,是适用于 全部的文件包括音视频, 那么 如果是纯文本的话,那么就可以使用
字符处理流 Reader 和Writer , 方法内部对于 字符的处理很到位,不必担心乱码; 而且可以使用链式调用;
以下是demo
文件字符输入流
package y.i.d;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
/**
*
* 利用缓冲区来做一组字节的读取, 但是为了防止数据贮存在内存中不出来 ,需要经常flush;
*
*
* */
public class InputTest {
public static void main(String[] args) {
// 1,创建流
File src=new File("/Users/wangrong/eclipse-workspace/Diy/src/aaa.txt");
// 2,选择流
Reader iReader=null;
try {
// 3,操作
iReader = new FileReader(src);
char[] flush= new char[2];
int len;
while ((len=iReader.read(flush))!=-1) {
// 字符数组直接 变成字符串
String str= new String(flush,0,len);
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}finally {
//4,释放资源
try {
if(null!=iReader) {
iReader.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
文件字符输出流
package y.i.d;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
/**
*
* 利用缓冲区来做一组字节的读取, 但是为了防止数据贮存在内存中不出来 ,需要经常flush;
*
*
* */
public class InputTest {
public static void main(String[] args) {
// 1,创建流
File src=new File("/Users/wangrong/eclipse-workspace/Diy/src/leon.txt");
// 2,选择流
Writer iWriter=null;
try {
// 3,操作
iWriter = new FileWriter(src,true); // 开启append
String msgString="leon is a good man!!!";
// 1, 直接写
// iWriter.write(msgString);
// 2,字符数组
// char[] datas = msgString.toCharArray();// 字符数组
// iWriter.write(datas,0,datas.length);
// 3, append 直接用
iWriter.append("leon is bad man! \r\n").append("holyshit!!!\r\n");
iWriter.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}finally {
//4,释放资源
try {
if(null!=iWriter) {
iWriter.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}