IO流
1.java.io包下
File类:java程序中的此类的一个对象,就对应着硬盘中的一个文件或网络中的一个资源.
File file1 = new File("d:\\io||helloworld.txt");
File file2 = new File("d:\\io\\io1");
>1.File即可以表示一个文件(.doc .xls .mps .avi .jpg .dat),也可以表示一个文件目录!
>2.File类的对象是与平台无关的
>3.File类针对于文件或文件目录,只能进行新建、删除、重命名、上层目录等等的操作.如果涉及到访问文件的内容,File是无能为例的,只能使用IO流下提供的相应的输入输出流来实现
>4.常把File类的对象作为形参传递给相应的输入输出流的构造器中!
2.IO流的结构
3.IO流的划分
1)按照流的流向的不同:输入流 输出流(站位于程序的角度)
2)按照流中的数据单位的不同:字节流 字符流 (纯文本文件使用字符流,除此之外使用字节流)
3)按照流的角色的不同:节点流 处理流 (流直接作用于文件上的节点流(4个),除此之外都是处理流)
4.重点掌握
抽象基类 节点流(文件流) 缓冲流(处理流的一种,可以提升文件操作的效率)
InputStream FileInputStream(int read(byte[] b)) BufferedInputStream
OutputStream FileOutputStream(void write(b,0,len)) BufferedOutputStream (flush())
Reader FileReader(int read(char[] c)) BufferedReader(readLine())
Writer FileWriter (void write(c,0,len)) BufferedWriter (flush())
注:1.从硬盘中读入一个文件,要求此文件一定得存在,若不存在,报FileNotFoundException的异常
2.从程序中输出一个文件到硬盘,此文件可以不存在,若不存在,就创建一个实现输出.若存在,则将已存在的文件覆盖
3.真正开发时,就使用缓冲流来代替节点流
4.主要最后要关闭相应的流,先关闭输出流,再关闭输入流,将此操作放入finally
FileInputStream的使用:
File file = new File("E:\\io\\test.txt");
InputStream is = new FileInputStream(file);
int b;
//读取方法一:
while((b=is.read())!=-1){
System.out.print((char)b);
}
//读取方法二:
byte[] bt = new byte[2];
while((b=is.read(bt))!=-1){
String str = new String(bt,0,b);
System.out.print(str);
}
FileOutputStream的使用:
//1.创建一个File对象,表明要写入的文件位置。
//输出的物理文件可以不存在,当执行过程中,若不存在,会自动的创建。若存在,会将原有的文件覆盖
File file = new File("hello2.txt");
//2.创建一个FileOutputStream的对象,将file的对象作为形参传递给FileOutputStream的构造器中
FileOutputStream fos = null;
try{
fos= new FileOutputStream(file);
//3.写入的操作
fos.write(newString("I love China!").getBytes());
}catch (Exception e) {
e.printStackTrace();
}finally {
//4.关闭输出流
if(fos != null) {
try{
fos.close();
}catch (IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
使用FileInputStream和FileOutputStream实现复制文件:
publicvoid copyFile(String src, String dest) {
//1.提供读入、写出的文件
File file1 = new File(src);
File file2 = new File(dest);
//2.提供相应的流
FileInputStream fis = null;
FileOutputStream fos = null;
try{
fis= new FileInputStream(file1);
fos= new FileOutputStream(file2);
//3.实现文件的复制
byte[] b = new byte[1024];
int len;
while((len = fis.read(b)) != -1) {
//fos.write(b);//错误的写法两种: fos.write(b,0,b.length);
fos.write(b,0, len);
}
}catch (Exception e) {
e.printStackTrace();
}finally {
if(fos != null) {
try{
fos.close();
}catch (IOException e) {
e.printStackTrace();
}
}
if(fis != null) {
try{
fis.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
}
FileReader的使用:
@Test
publicvoid testFileReader(){
FileReader fr = null;
try{
File file = new File("dbcp.txt");
fr= new FileReader(file);
char[] c = new char[24];
int len;
while((len= fr.read(c)) != -1){
String str = new String(c, 0, len);
System.out.print(str);
}
}catch(IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(fr!= null){
try{
fr.close();
}catch (IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/*
* 使用FileReader、FileWriter 可以实现文本文件的复制。
* 对于非文本文件(视频文件、音频文件、图片),只能使用字节流!
*/
public class TestFileReaderWriter {
@Test
public void testFileReaderWriter(){
//1.输入流对应的文件src一定要存在,否则抛异常。输出流对应的文件dest可以不存在,执行过程中会自动创建
FileReader fr = null;
FileWriter fw = null;
try{
//不能实现非文本文件的复制
// File src = new File("C:\\Users\\shkstart\\Desktop\\1.jpg");
// File dest = new File("C:\\Users\\shkstart\\Desktop\\3.jpg");
File src = new File("dbcp.txt");
File dest = new File("dbcp1.txt");
//2.
fr= new FileReader(src);
fw= new FileWriter(dest);
//3.
char[] c = new char[24];
int len;
while((len= fr.read(c)) != -1){
fw.write(c,0, len);
}
}catch(Exceptione){
e.printStackTrace();
}finally{
if(fw!= null){
try{
fw.close();
}catch (IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
if(fr!= null){
try{
fr.close();
}catch (IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
处理流:
BufferedInputStream和BufferedOutputStream的使用:
//使用缓冲流实现文件的复制的方法
public void copyFile(String src,String dest){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try{
//1.提供读入、写出的文件
File file1 = new File(src);
File file2 = new File(dest);
//2.想创建相应的节点流:FileInputStream、FileOutputStream
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
//3.将创建的节点流的对象作为形参传递给缓冲流的构造器中
bis= new BufferedInputStream(fis);
bos= new BufferedOutputStream(fos);
//4.具体的实现文件复制的操作
byte[] b = new byte[1024];
int len;
while((len= bis.read(b)) != -1){
bos.write(b,0, len);
bos.flush();
}
}catch(IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}finally{
//5.关闭相应的流
if(bos!= null){
try{
bos.close();
}catch (IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
if(bis!= null){
try{
bis.close();
}catch (IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
BufferedReader和BufferedWriterd的使用:
@Test
publicvoid testBufferedReader(){
BufferedReader br = null;
BufferedWriter bw = null;
try{
File file = new File("dbcp.txt");
File file1 = new File("dbcp3.txt");
FileReader fr = new FileReader(file);
FileWriter fw = new FileWriter(file1);
br= new BufferedReader(fr);
bw= new BufferedWriter(fw);
// char[] c = new char[1024];
// int len;
// while((len= br.read(c))!= -1){
// String str = new String(c, 0, len);
// System.out.print(str);
// }
String str;
while((str= br.readLine()) != null){
// System.out.println(str);
bw.write(str+ "\n");
// bw.newLine();
bw.flush();
}
}catch(IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(bw!= null){
try{
bw.close();
}catch (IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
if(br!= null){
try{
br.close();
}catch (IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
5.其他的流
5.1转换流:实现字节流与字符流之间的转换
InputStreamReader:输入时,实现字节流到字符流的转换,提高操作的效率(前提是,数据是文本文件)===>解码:字节数组--->字符串
OutputStreamWriter:输出时,实现字符流到字节流的转换.===>编码:字符串--->字节数组
例子:从键盘输入字符串,要求将读取到的整行字符串转换成大写输出,然后继续进行输入操作,直至输入"e"或者"exit"时,退出程序
5.2标准的输入输出流
System.in:The"standard" input stream:从键盘输入数据
System.out:The" standard" output stream:从显示器输出数据
5.3打印流(都是输出流)PrintStream(处理字节) PrintWriter(处理字符)
可以使用System.setOut(PrintStreamp)重新设置一下输出的位置
PrintStream p = new PrintStream(new FileOutputStream("hello.txt"),true);
5.4数据流 (处理基本数据类型、String类、字节数组)
DataInputStream DataOutputStream
5.5对象流
>对象的序列化机制:允许把内存中的java对象转换成平台无关的二进制流,从而允许把这种二进制流持久地保存在磁盘上,或通过网络将这种二进制流传输到另一个网络节点.当其他程序获取了这种二进制流,就可以恢复成原来的java对象
ObjectInputStream(Object readObject()): ObjectOutputStream(void writeObject(Objectobj));
如何创建流的对象:
ObjectOutputStream oo = newObjectOutputStream(new FileOutputStream(new File("son.txt")));
ObjectInputStream oi = new ObjectInputStream(newFileInputStream(new File("son.txt")));
实例序列化机制的对象对应的类的要求: a.要求类要实现Serializable接口 b.同样要求类的所有属性也必须实现Serializable接口 c.要求给类提供一个序列版本号:private static final long serialVersionUID; d.属性声明为static或transient的,不可以实现序列化
5.6随机存取文件流:RandomAccessFile
a.既可以充当一个输入流,又可以充当一个输出流:publicRandomAccessFile(File file,String mode)
b.支持 从文件的开头读取、写入.若输出的文件不存在,直接创建.若存在,则是对原有文件内容的覆盖.
c.支持任意位置的"插入".