标签:des style blog http color io os java ar
http://download.csdn.net/detail/qq285016127/7963747
Java对文件的操作API一般分为字节流 字符流 其为文件的读写API框架也是通过这个思想去扩展的.另外,在流的流向中也分为源流和宿流.如图(流的主体是根据流向决定,如输入InputStream/输出流OutPutStream)
<1>从整个框架上看,io流的主要知识点分为:
1.字节流(输入FileInputStream/输出FileOutputStream)
1)字节流的构造一般都会通过文件File类,或者文件的绝对路径
2)文件的读取需要FileInputStream类,其read()提供了3个方法,需要我们提供最基本的byte字节数组,该数组作为缓冲流数据默认情况下,没有参数的时候系统会默认一个只有长度1的数组.每次读完,即把数据放在缓冲区中.
3)FileInputStream提供了available()对象方法,返回未阻塞的数据长度.一般数据较小的情况下 ,我们可以声明缓冲区的长度为此长度.一次读入内存;
4)文件的写入需要FileOutputStream类,其write()也提供了上面三种类型的方法.
示例代码如下:
/**
* 字节流简单应用
*
* @author Lean @date:2014-9-22
*/
public class OutputStreamSample {
public static void main(String[] args) {
String filePath = "C:/Users/Administrator/Desktop/aa.txt";
String outFilePath = "C:/Users/Administrator/Desktop/bb.txt";
InputStream fis=null;
OutputStream fos=null;
int readNum=0;
//必须为2的n次方,因为数据可能存在字符
byte[] buff=new byte[2];
try {
fis=new FileInputStream(filePath);
//通过File,File全路径 或者文件描述器构造 如果没有文件 则自动创建
fos=new FileOutputStream(outFilePath);
while ((readNum=(fis.read(buff)))!=-1) {
//根据偏移量写入文件
fos.write(buff,0,readNum);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if (fos!=null) {
fos.close();
}
if (fis!=null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2.字符流,也有称为转换流(InputStreamReader/OutputStreamWriter)
1)字符转换流,沿用上面的思想,也提供了读取操作,只不过该缓冲区的定义数组为char[ ];
2)字符转换流为字节流的装饰器,集成了字节流的优势,效率较高.代码如下:
/**
* 字符流简单应用
*
* @author Lean @date:2014-9-22
*/
public class CharacterIOSample {
public static void main(String[] args) {
final String filePath = "C:/Users/Administrator/Desktop/aa.txt";
final String outFilePath = "C:/Users/Administrator/Desktop/bb.txt";
Writer writer=null;
Reader reader=null;
char[] buff=new char[512];
int readNum=0;
try {
reader=new InputStreamReader(new FileInputStream(filePath));
writer=new OutputStreamWriter(new FileOutputStream(outFilePath));
while ((readNum=reader.read(buff))!=-1) {
writer.write(buff,0,readNum);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if (writer!=null) {
writer.close();
}
if (reader!=null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3.缓冲流(BufferedReader/BufferedWriter)
1)缓冲流为字符转换流的装饰器,集成了字符转换流的优势,效率有所较高,并提供了读取整行,写入整行的方法.
2)缓冲流,顾名思义就是内置了缓冲数据,我们不需要为此再写缓冲数据(char[] / byte[]);
/**
* 内置缓冲流简单应用
*
* @author Lean @date:2014-9-22
*/
public class BufferedIOSample {
public static void main(String[] args) {
final String filePath = "C:/Users/Administrator/Desktop/aa.txt";
final String outFilePath = "C:/Users/Administrator/Desktop/bb.txt";
BufferedWriter writer = null;
BufferedReader reader = null;
String appendStr =null;
try {
reader = new BufferedReader(new InputStreamReader(
new FileInputStream(filePath)));
writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(outFilePath)));
while ((appendStr = reader.readLine()) != null) {
System.out.println(appendStr);
writer.write(appendStr);
}
writer.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if (writer!=null) {
writer.close();
}
if (reader!=null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
4.对象流(ObjectOutputStream/ObjectInputStream)
1)该流可以把数据保存到文件中去,保存的对象需要实现Serializable接口,并实现序列码(修改该类有用)
2)如果保存的对象属性也为对象,也必须实现上面的接口.实现该接口的作用为支持持久化/网络传输
3)保存的对象文件乱码,无法直接打开.但如果想更加安全,必须实现另一个接口Externalizable实现自己的保存策略.
/**
*
*
* @author Lean @date:2014-9-22
*/
public class ObjectIOSample {
public static void main(String[] args) {
saveObject();
readObject();
}
public static void saveObject(){
String filePath="C:\\Users\\Administrator\\Desktop\\cc.txt";
ObjectOutputStream oos=null;
try {
oos=new ObjectOutputStream(new FileOutputStream(filePath));
oos.writeObject(new Student(1, "Lean", 82));
oos.writeObject(new Student(2, "Lucy", 62));
oos.writeObject(new Student(3, "Lout", 85));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if (oos!=null) {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void readObject(){
String filePath="C:\\Users\\Administrator\\Desktop\\cc.txt";
ObjectInputStream ios=null;
try {
ios=new ObjectInputStream(new FileInputStream(filePath));
Object object=null;
for (int i = 0; i < 3; i++) {
System.out.println(ios.readObject());
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if (ios!=null) {
try {
ios.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
/**
* @author Lean @date:2014-9-22
*/
public class Student implements Serializable{
private int id;
private String name;
private int score;
public Student(int id, String name, int score) {
this.id = id;
this.name = name;
this.score = score;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", score=" + score
+ "]";
}
}
标签:des style blog http color io os java ar
原文地址:http://blog.csdn.net/qq285016127/article/details/39501003