您的位置:首页 > 职场人生

黑马程序员--java基础--io流

2014-12-07 00:09 459 查看
------- android培训java培训、期待与您交流! ---------



io流有四个基类:

分别为

字符流:

Reader

Writer

字节流

InputStream

OutputStream

由这四个类派生出来的子类都以父类名作为其名字的后缀。

在java的io操作都有相应的步骤:

1.使用File类打开一个文件。

2.通过字符流或字节流来指定读取的位置。

3.进行读取操作。

4.关闭输入流、输出流。

常用的字符读取FileReader,FileWriter操作:

package com.itheima;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class IODemo {

/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
File file = new File("d:\\1.txt");//1.使用File类打开一个文件。
//2.通过字符流或字节流来指定读取的位置。
FileReader fileReader = new FileReader(file);
File file2 = new File("D:\\coyp1.txt");
FileWriter fileWriter = new FileWriter(file2);
//3.进行读取操作。
int len = 0;
while ((len = fileReader.read())!= -1) {
fileWriter.write(len);
}
//
fileReader.close();
fileWriter.close();
}

}


常用的字节读取FileInputStream,FileOutStream操作

操作:

package com.itheima;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;

public class IODemo {

/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
File file = new File("d:\\My Medicine.mp3");//1.使用File类打开一个文件。
//2.通过字符流或字节流来指定读取的位置。
FileInputStream fileInputStream = new FileInputStream(file);
File file2 = new File("D:\\coypMy Medicine.mp3");
FileOutputStream fileOutputStream = new FileOutputStream(file2);
//3.进行读取操作。
byte[] buf = new byte[1024*4];
int len = 0;
while ((len = fileInputStream.read(buf))!= -1) {
fileOutputStream.write(buf, 0, len);
}
//
fileInputStream.close();
fileOutputStream.close();
}

}
若是想提高io流的操作效率,我们可以创建缓冲区。

例子:

package com.itheima;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;

public class IODemo {

/**
* 缓冲区出现是为了提高流的操作效率。所以在创建缓冲区钱,必须要有流对象
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
File file = new File("d:\\My Medicine.mp3");//1.使用File类打开一个文件。
//2.通过字符流或字节流来指定读取的位置。
FileInputStream fileInputStream = new FileInputStream(file);
File file2 = new File("D:\\coypMy Medicine.mp3");
FileOutputStream fileOutputStream = new FileOutputStream(file2);
//只要将需要被提高效率的流对象加入缓冲区的构造函数
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
//3.进行读取操作。
byte[] buf = new byte[1024*4];
int len = 0;
////只要用到缓冲区,就要记得刷新
while ((len = bufferedInputStream.read(buf))!= -1) {
bufferedOutputStream.write(buf, 0, len);
bufferedOutputStream.flush();
}
fileInputStream.close();
fileOutputStream.close();
}

}


io流一定会产生异常,我们就要对异常进行处理,抛出异常的操作是不负责任的。

package InputOutput;

import java.io.FileWriter;
import java.io.IOException;

public class FileWriterDemo2 {

/**
*io异常的处理方式
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
FileWriter fw= null;
try {
fw = new FileWriter("C:\\Users\\F4\\Desktop\\demo.txt");
fw.write("啊不错的风格");

} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(e.toString());
} finally{
try {
//关闭输入流输出流都应该在finally里执行、
if(fw != null)
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

}
总结流操作的规律

1.明确源和目的

源:输入流 Reader InputStream

目的:输出流Write OutputStream

2.明确数据是什么.

纯文本:Reader Write

不是:

InputStream OutputStream

io流对象流:

ObjectInputStream 

ObjectOutputStream

被操作的对象需要实现Serializable接口

向一个文件中写入一个对象,可以使用对象流来实现,称为序列化。

package com.itheima;

import java.io.Serializable;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
class Person implements Serializable{
public static final long serialVersionUID = 42L;
private String name;
private transient int age;
public static String contry ="cn";
Person(String name,int age,String contry){
this.name=name;
this.age=age;
this.contry = contry;
}
public String toString(){
return name + ":" + age + " :" + contry;
}
}

public class IODemo{
public static void main(String[] args) throws Exception{
writeObj();
readObj();
}

public static void readObj() throws Exception{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("C:\\1.java"));
Person p = (Person)ois.readObject();
System.out.println(p);
ois.close();
}

public static void writeObj() throws IOException{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("C:\\1.java"));
oos.writeObject(new Person("黑马程序员",20,"HZ"));
oos.close();
}

}


------- android培训java培训、期待与您交流! ---------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  黑马程序员