java io 流

流:
按方向分:
1 输入流:(往内存里流的) 以内存为基础
2 输出流:(从内存里往外流的) 以内存为基础

按性质分:
1 字节流:以字节的形式存储的 (看不懂的,或者不需要看)
2 字符流:以字符的形式存储的(字符流是有编码的,人能看懂,需要看)

如:文本文件:是字符的,如果是字节的,就看不懂咯.凡是能用记事本看的,都是字符流
txt,bat,java 用字符流
jpg,mp3,class 等格式,用记事本打开,根本看不懂,也不需要看,一般使用字节流

按方向加性质分:
1.以Stream结尾的都是字节流
2.以Reader或Writer结尾的都是字符流

输入字节流:InputStream
输出字节流:OutputStream

输入字符流:Reader
输出字符流:Writer

1 读取一个文本文件从控制台显示:(方向:从磁盘到内存)使用输入字符流
2 复制一个mp3文件: (方向:先从磁盘到内存,再从内存到磁盘)使用输入字节流,输出字节流(不需要看懂)
3 复制一个java文件:(方向:先从磁盘到内存,再从内存到磁盘)使用输入字节流,输出字节流(能看懂,但不需要看)
4 复制一个java文件,并在控制台显示:使用输入字符流,输出字符流(能看懂,需要看)

例:读取一个文本文件 分析使用字符流Reader
物理文件-->File (JVM)-->FileReader --->BufferedReader


File-->FileInputStream -->BufferedInputStream --> ObjectInputStream
字节流转成字符流FileInputStream --> InputStreamReader-->BufferedReader
File --> FileReader -->BufferedReader

File-->FileOutputStream --> BufferedOutputStream -->ObjectOutputStream
字节流转成字符流FileOutputStream --> OutputStreamWriter-->BufferedWriter
File --> FileWriter -->BufferedWriter-->PrintWriter

注:使用缓冲,效率会更高.

序列化(持久化):把对象写到磁盘或数据库,或者到网络里.
反序列化:从磁盘或数据库或网络里读对象

DataInputStream ,DataOutputStream:用于在网络中的传输

package com.inetpsa.kor.flowmodeler.partnerintegration.batch;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
/**
*
* @author e371575
* @version $Rev$
* @since Mar 21, 2011
* 流(Stream)
*/
public class ReadFile {
//读取文本文件夹
public static void readFile(String fileName){
//定义缓冲字符输入流
BufferedReader in = null;
try {
//将字节流转为 字符流:inputStreamReader,outputStreamWriter,网络传输都是字节流
in = new BufferedReader(new InputStreamReader(new FileInputStream(new File(fileName)), "gbk"));
// in = new BufferedReader(new FileReader(new File(fileName)));//这样读会产生中文乱码
//定义一行,用于存储每行读到的内容
String line = null;
//读文件
while((line = in.readLine()) != null){
//输出到控制台
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
finally{
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
//new ReadFile().readFile("c:/zhanghy/光盘使用说明.txt");
new ReadFile().writeFile("c:/zhanghy/光盘使用说明2.txt");
//new ReadFile().copyFile("c:/zhanghy/music/韩超-葬爱.mp3","c:/zhanghy/韩超-葬爱.mp3");
}
//将文本写到文件
public static void writeFile(String fileName){
//定义缓冲字符输出流 printwriter的方法比bufferedWriter更多点.所以一般可以使用它
PrintWriter out = null;
try {
//true表示追加文件
out = new PrintWriter(new BufferedWriter(new FileWriter(fileName,true)));
//输出到文件
out.println("hello world 你好");
out.flush();//将管道中剩下的内容写入目标
} catch (IOException e) {
e.printStackTrace();
}finally{
out.close();//自动调用flush()
}
}
//copy file 使用输入字节,输出字节流
public static void copyFile(String sourceFile,String targetFile){
File source = new File(sourceFile);
File target = new File(targetFile);
BufferedInputStream in = null; //定义缓冲字节输入流
BufferedOutputStream out = null;//定义缓冲字节输出流
try{
//输入,字节数组 输出
in = new BufferedInputStream(new FileInputStream(source));
out = new BufferedOutputStream(new FileOutputStream(target));
byte[] buffer = new byte[1024];//定义一个byte[]数组,用于存储文件中的内容
//用于存储临时读到的个数
int readCount = 0;
//读:往buffer里装载,得到一个装载量readCount
while((readCount = in.read(buffer, 0, buffer.length))!= -1){
//写:读多少,写多少
out.write(buffer, 0, readCount);
}
out.flush();//把缓冲区内容都写到目标

}catch(IOException e){
e.printStackTrace();
}finally{
try {//先关out,再关in
if(out != null){
out.close();
}
if(in != null){
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

package com.inetpsa.kor.flowmodeler.partnerintegration.batch;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
* @author e371575
* @version $Rev$
* @since Mar 22, 2011
* 读写对象
*/
public class ReadWriterObject {
public static void main(String[] args) {
// new ReadWriterObject().writeObjectToFile();
new ReadWriterObject().ReadObjectFromFile();
}
//将一个动物对象写到文件里去,没有被序列化的对象不能写到文件里
public static void writeObjectToFile(){
Animal an = new Animal();
an.setAge(10);
an.setName("xiaobai");
//创建对象输出流 ,
try {
//对象一定要实现序列化接口
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(new File("c:/zhanghy/animal"))));
out.writeObject(an);//写入对象
out.flush();//清空管道
} catch (IOException e) {
e.printStackTrace();
System.out.println("write object error");
}
}
//读从文件中对象
public static void ReadObjectFromFile(){
try {
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(new File("c:/zhanghy/animal"))));
Object object = in.readObject();
System.out.println(object);
Animal an = (Animal)object;
System.out.println("age: "+an.getAge()+" name:"+an.getName());
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
//动物类
class Animal implements Serializable{
//定义版本号:用于在反序列化(也就是读对象时,判断该对象是否被修改过,如果不一致,则会读取失败)
public static final long serialVersionUID = 6587245110917186266L;
String name;
int age;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return this.age;
}
public void setAge(int age) {
this.age = age;
}
/**
* {@inheritDoc}
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return this.name+" | "+this.age;
}

}
发布了24 篇原创文章 · 获赞 0 · 访问量 2367
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章